9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-23 00:29:18 +00:00

fix: high latency redis environments firing data updates twice

This commit is contained in:
William278
2024-01-20 17:30:22 +00:00
parent 66bbde0b5d
commit 6641e11fd9
2 changed files with 11 additions and 1 deletions

View File

@@ -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();