aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetr Ilin <hevav@hevav.dev>2022-07-10 15:01:37 +0300
committerPetr Ilin <hevav@hevav.dev>2022-07-10 15:01:37 +0300
commit04bc79eb268465eca21c1b681e0cb4497a455a06 (patch)
tree3f8785d3593650dde5e70eff0ed2d1cf636f96fc /src
parent428a1c8e1aa22d2e6df7ccd6e6836925c7be104d (diff)
downloadLimboAuth-04bc79eb268465eca21c1b681e0cb4497a455a06.tar.gz
LimboAuth-04bc79eb268465eca21c1b681e0cb4497a455a06.tar.bz2
LimboAuth-04bc79eb268465eca21c1b681e0cb4497a455a06.zip
In-async-thread scheduler v2
Diffstat (limited to 'src')
-rw-r--r--src/main/java/net/elytrium/limboauth/LimboAuth.java42
-rw-r--r--src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java2
2 files changed, 22 insertions, 22 deletions
diff --git a/src/main/java/net/elytrium/limboauth/LimboAuth.java b/src/main/java/net/elytrium/limboauth/LimboAuth.java
index 618877b..373e96c 100644
--- a/src/main/java/net/elytrium/limboauth/LimboAuth.java
+++ b/src/main/java/net/elytrium/limboauth/LimboAuth.java
@@ -37,6 +37,7 @@ import com.velocitypowered.api.plugin.PluginContainer;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
+import com.velocitypowered.api.scheduler.ScheduledTask;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.IOException;
@@ -60,8 +61,6 @@ import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Pattern;
@@ -142,7 +141,6 @@ public class LimboAuth {
private final File configFile;
private final LimboFactory factory;
private final FloodgateApiHolder floodgateApi;
- private final ThreadLocal<ScheduledExecutorService> scheduler;
@Nullable
private Component loginPremium;
@@ -153,6 +151,8 @@ public class LimboAuth {
@Nullable
private Title loginFloodgateTitle;
private Component nicknameInvalidKick;
+ private ScheduledTask purgeCacheTask;
+ private ScheduledTask purgePremiumCacheTask;
private JdbcPooledConnectionSource connectionSource;
private Dao<RegisteredPlayer, String> playerDao;
@@ -177,8 +177,6 @@ public class LimboAuth {
} else {
this.floodgateApi = null;
}
-
- this.scheduler = ThreadLocal.withInitial(() -> Executors.newSingleThreadScheduledExecutor(task -> new Thread(task, "limboauth-scheduler")));
}
@Subscribe
@@ -370,19 +368,25 @@ public class LimboAuth {
eventManager.unregisterListeners(this);
eventManager.register(this, new AuthListener(this, this.playerDao, this.floodgateApi));
- Executors.newSingleThreadScheduledExecutor(task -> new Thread(task, "limboauth-purge-cache")).scheduleAtFixedRate(
- () -> this.checkCache(this.cachedAuthChecks, Settings.IMP.MAIN.PURGE_CACHE_MILLIS),
- Settings.IMP.MAIN.PURGE_CACHE_MILLIS,
- Settings.IMP.MAIN.PURGE_CACHE_MILLIS,
- TimeUnit.MILLISECONDS
- );
+ if (this.purgeCacheTask != null) {
+ this.purgeCacheTask.cancel();
+ }
- Executors.newSingleThreadScheduledExecutor(task -> new Thread(task, "limboauth-purge-premium-cache")).scheduleAtFixedRate(
- () -> this.checkCache(this.premiumCache, Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS),
- Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS,
- Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS,
- TimeUnit.MILLISECONDS
- );
+ this.purgeCacheTask = this.server.getScheduler()
+ .buildTask(this, () -> this.checkCache(this.cachedAuthChecks, Settings.IMP.MAIN.PURGE_CACHE_MILLIS))
+ .delay(Settings.IMP.MAIN.PURGE_CACHE_MILLIS, TimeUnit.MILLISECONDS)
+ .repeat(Settings.IMP.MAIN.PURGE_CACHE_MILLIS, TimeUnit.MILLISECONDS)
+ .schedule();
+
+ if (this.purgePremiumCacheTask != null) {
+ this.purgePremiumCacheTask.cancel();
+ }
+
+ this.purgePremiumCacheTask = this.server.getScheduler()
+ .buildTask(this, () -> this.checkCache(this.premiumCache, Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS))
+ .delay(Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS, TimeUnit.MILLISECONDS)
+ .repeat(Settings.IMP.MAIN.PURGE_PREMIUM_CACHE_MILLIS, TimeUnit.MILLISECONDS)
+ .schedule();
eventManager.fireAndForget(new AuthPluginReloadEvent());
}
@@ -661,10 +665,6 @@ public class LimboAuth {
return SERIALIZER;
}
- public ScheduledExecutorService getScheduler() {
- return this.scheduler.get();
- }
-
private static class CachedUser {
private final long checkTime;
diff --git a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java
index fbb442a..de50281 100644
--- a/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java
+++ b/src/main/java/net/elytrium/limboauth/handler/AuthSessionHandler.java
@@ -158,7 +158,7 @@ public class AuthSessionHandler implements LimboSessionHandler {
Serializer serializer = LimboAuth.getSerializer();
int authTime = Settings.IMP.MAIN.AUTH_TIME;
float multiplier = 1000.0F / authTime;
- this.authMainTask = this.plugin.getScheduler().scheduleWithFixedDelay(() -> {
+ this.authMainTask = this.player.getScheduledExecutor().scheduleWithFixedDelay(() -> {
if (System.currentTimeMillis() - this.joinTime > authTime) {
this.proxyPlayer.disconnect(timesUp);
} else {