fix: high latency redis environments firing data updates twice

feat/data-edit-commands
William278 1 year ago
parent 66bbde0b5d
commit 6641e11fd9

@ -27,6 +27,7 @@ import net.william278.husksync.util.Task;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
@ -107,11 +108,18 @@ public abstract class DataSyncer {
protected void listenForRedisData(@NotNull OnlineUser user, @NotNull Supplier<Boolean> completionSupplier) {
final AtomicLong timesRun = new AtomicLong(0L);
final AtomicReference<Task.Repeating> task = new AtomicReference<>();
final AtomicBoolean processing = new AtomicBoolean(false);
final Runnable runnable = () -> {
if (user.isOffline()) {
task.get().cancel();
return;
}
// Ensure only one task is running at a time
if (processing.getAndSet(true)) {
return;
}
// Timeout if the plugin is disabling or the max attempts have been reached
if (plugin.isDisabling() || timesRun.getAndIncrement() > maxListenAttempts) {
task.get().cancel();
plugin.debug(String.format("[%s] Redis timed out after %s attempts; setting from database",
@ -120,9 +128,11 @@ public abstract class DataSyncer {
return;
}
// Fire the completion supplier
if (completionSupplier.get()) {
task.get().cancel();
}
processing.set(false);
};
task.set(plugin.getRepeatingTask(runnable, LISTEN_DELAY));
task.get().run();

@ -3,7 +3,7 @@ org.gradle.jvmargs='-Dfile.encoding=UTF-8'
org.gradle.daemon=true
javaVersion=16
plugin_version=3.2.1
plugin_version=3.2.2
plugin_archive=husksync
plugin_description=A modern, cross-server player data synchronization system

Loading…
Cancel
Save