Throttle client-link requests per player

This commit is contained in:
Claude committed 2026-09-30 19:38:49 +00:00
1 parent 2bcdf26351
commit 38c9f249e1
2 files changed
+22 -1

No files matched your search

@@ -22,6 +22,9 @@ public final class ClientLink {
private final Map<UUID, String> lastState = new ConcurrentHashMap<>();
private final Map<UUID, String> lastClaimsKey = new ConcurrentHashMap<>();
private final Set<UUID> clients = ConcurrentHashMap.newKeySet();
private final Map<UUID, Long> lastRequest = new ConcurrentHashMap<>();
/** One request per player at most this often; a modded client can't turn the link into a panel flood. */
static final long MIN_REQUEST_GAP_MS = 400;
public ClientLink(Env env, PlayerCache cache, String serverName, BiConsumer<CorePlayer, byte[]> out) {
this.env = env; this.cache = cache; this.serverName = serverName; this.out = out;
@@ -30,7 +33,7 @@ public final class ClientLink {
public boolean hasClient(UUID uuid) { return clients.contains(uuid); }
public void disconnected(UUID uuid) {
clients.remove(uuid); lastState.remove(uuid); lastClaimsKey.remove(uuid); cache.forget(uuid);
clients.remove(uuid); lastState.remove(uuid); lastClaimsKey.remove(uuid); lastRequest.remove(uuid); cache.forget(uuid);
}
private void send(CorePlayer p, JsonObject message) { out.accept(p, Wire.encode(message.toString())); }
@@ -42,6 +45,9 @@ public final class ClientLink {
catch (RuntimeException e) { return; }
String type = PlayerCache.str(msg, "t", "");
if (!env.features.clientLink()) return;
long now = env.clock.getAsLong();
Long previous = lastRequest.put(p.uuid(), now);
if (previous != null && now - previous < MIN_REQUEST_GAP_MS && !type.equals("hello")) return;
switch (type) {
case "hello" -> hello(p);
case "refresh" -> { if (clients.contains(p.uuid())) { lastState.remove(p.uuid()); cache.refresh(p, info -> pushState(p)); } }