Skip to content

Commit

Permalink
fix: high latency redis environments firing data updates twice
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Jan 20, 2024
1 parent 66bbde0 commit 6641e11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions common/src/main/java/net/william278/husksync/sync/DataSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -107,11 +108,18 @@ protected void setUserFromDatabase(@NotNull OnlineUser user) {
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",
Expand All @@ -120,9 +128,11 @@ protected void listenForRedisData(@NotNull OnlineUser user, @NotNull Supplier<Bo
return;
}

// Fire the completion supplier
if (completionSupplier.get()) {
task.get().cancel();
}
processing.set(false);
};
task.set(plugin.getRepeatingTask(runnable, LISTEN_DELAY));
task.get().run();
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6641e11

Please sign in to comment.