diff --git a/api/src/main/java/net/draycia/carbon/api/CarbonServer.java b/api/src/main/java/net/draycia/carbon/api/CarbonServer.java index b4e6fd0a..af27e871 100644 --- a/api/src/main/java/net/draycia/carbon/api/CarbonServer.java +++ b/api/src/main/java/net/draycia/carbon/api/CarbonServer.java @@ -28,6 +28,7 @@ import net.kyori.adventure.audience.Audience; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; +import org.jetbrains.annotations.ApiStatus; /** * The server that carbon is running on. @@ -68,7 +69,8 @@ public interface CarbonServer extends Audience { * @return the user's UUID * @since 2.0.0 */ - CompletableFuture<@Nullable UUID> resolveUUID(final String username); + @ApiStatus.Internal + CompletableFuture<@Nullable UUID> resolveUUID(String username, boolean cacheOnly); /** * Obtains the desired player's name. @@ -77,6 +79,12 @@ public interface CarbonServer extends Audience { * @return the user's name * @since 2.0.0 */ - CompletableFuture<@Nullable String> resolveName(final UUID uuid); + @ApiStatus.Internal + CompletableFuture<@Nullable String> resolveName(UUID uuid, boolean cacheOnly); + + @ApiStatus.Internal + default CompletableFuture<@Nullable String> resolveName(final UUID uuid) { + return this.resolveName(uuid, false); + } } diff --git a/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java b/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java index 487a6b04..568fe898 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java +++ b/common/src/main/java/net/draycia/carbon/common/command/argument/CarbonPlayerArgument.java @@ -165,7 +165,7 @@ public final class CarbonPlayerArgument extends CommandArgument { + final @Nullable CarbonPlayer join = CarbonChatProvider.carbonChat().server().resolveUUID(input, commandContext.isSuggestions()).thenCompose(uuid -> { if (uuid == null) { return CompletableFuture.completedFuture(null); } diff --git a/common/src/main/java/net/draycia/carbon/common/users/MojangProfileResolver.java b/common/src/main/java/net/draycia/carbon/common/users/MojangProfileResolver.java index cbdc61e0..c3cc6c3d 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/MojangProfileResolver.java +++ b/common/src/main/java/net/draycia/carbon/common/users/MojangProfileResolver.java @@ -32,12 +32,16 @@ import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import java.time.Duration; import java.util.HashMap; import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import net.draycia.carbon.common.util.ConcurrentUtil; import org.apache.logging.log4j.Logger; @@ -45,7 +49,6 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; -// todo rate limit handling @DefaultQualifier(NonNull.class) public final class MojangProfileResolver implements ProfileResolver { @@ -55,6 +58,7 @@ public final class MojangProfileResolver implements ProfileResolver { private final Map> pendingUuidLookups = new HashMap<>(); private final Map> pendingUsernameLookups = new HashMap<>(); private final ProfileCache cache; + private final RateLimiter rateLimiter; @Inject private MojangProfileResolver(final Logger logger, final ProfileCache cache) { @@ -64,17 +68,21 @@ public final class MojangProfileResolver implements ProfileResolver { .create(); this.executorService = Executors.newFixedThreadPool(2, ConcurrentUtil.carbonThreadFactory(logger, "MojangProfileResolver")); this.cache = cache; + this.rateLimiter = new RateLimiter(); } @Override - public synchronized CompletableFuture<@Nullable UUID> resolveUUID(final String username) { + public synchronized CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) { if (username.length() > 25 || username.length() < 1) { // Invalid names return CompletableFuture.completedFuture(null); } - if (this.cache.hasCachedEntry(username)) { + if (cacheOnly || this.cache.hasCachedEntry(username)) { return CompletableFuture.completedFuture(this.cache.cachedId(username)); } return this.pendingUuidLookups.computeIfAbsent(username, $ -> { + if (!this.rateLimiter.canSubmit()) { + return CompletableFuture.completedFuture(null); + } final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> { try { final HttpRequest request = createRequest( @@ -103,11 +111,14 @@ public final class MojangProfileResolver implements ProfileResolver { } @Override - public synchronized CompletableFuture<@Nullable String> resolveName(final UUID uuid) { - if (this.cache.hasCachedEntry(uuid)) { + public synchronized CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) { + if (cacheOnly || this.cache.hasCachedEntry(uuid)) { return CompletableFuture.completedFuture(this.cache.cachedName(uuid)); } return this.pendingUsernameLookups.computeIfAbsent(uuid, $ -> { + if (!this.rateLimiter.canSubmit()) { + return CompletableFuture.completedFuture(null); + } final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> { try { final HttpRequest request = createRequest( @@ -148,8 +159,7 @@ public final class MojangProfileResolver implements ProfileResolver { if (response == null) { throw new RuntimeException("Null response for request " + request); } else if (response.statusCode() == 429) { - // todo - throw new IllegalStateException("Rate limited"); + throw new RuntimeException("Got rate-limited by Mojang, could not fulfill request: " + request); } else if (response.statusCode() == 404) { // No such profile return null; @@ -170,6 +180,7 @@ public final class MojangProfileResolver implements ProfileResolver { @Override public void shutdown() { ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.MILLISECONDS, 500); + this.rateLimiter.shutdown(); } private record BasicLookupResponse(UUID id, String name) { @@ -203,4 +214,37 @@ public final class MojangProfileResolver implements ProfileResolver { } + private static final class RateLimiter { + + // Mojang rate limit is 600 requests per ten minutes + private static final int RATE_LIMIT = 600; + + private final Semaphore semaphore = new Semaphore(RATE_LIMIT); + private final Timer timer; + + private RateLimiter() { + this.timer = new Timer("MojangProfileResolver.RateLimiter"); + this.timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + synchronized (RateLimiter.this) { + final int release = RATE_LIMIT - RateLimiter.this.semaphore.availablePermits(); + if (release > 0) { + RateLimiter.this.semaphore.release(release); + } + } + } + }, 0L, Duration.ofMinutes(10).toMillis()); + } + + synchronized boolean canSubmit() { + return this.semaphore.tryAcquire(); + } + + void shutdown() { + this.timer.cancel(); + } + + } + } diff --git a/common/src/main/java/net/draycia/carbon/common/users/ProfileResolver.java b/common/src/main/java/net/draycia/carbon/common/users/ProfileResolver.java index 36a5f09e..cb60b4c4 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/ProfileResolver.java +++ b/common/src/main/java/net/draycia/carbon/common/users/ProfileResolver.java @@ -28,9 +28,17 @@ import org.checkerframework.framework.qual.DefaultQualifier; @DefaultQualifier(NonNull.class) public interface ProfileResolver { - CompletableFuture<@Nullable UUID> resolveUUID(String username); + CompletableFuture<@Nullable UUID> resolveUUID(String username, boolean cacheOnly); - CompletableFuture<@Nullable String> resolveName(UUID uuid); + default CompletableFuture<@Nullable UUID> resolveUUID(final String username) { + return this.resolveUUID(username, false); + } + + CompletableFuture<@Nullable String> resolveName(UUID uuid, boolean cacheOnly); + + default CompletableFuture<@Nullable String> resolveName(final UUID uuid) { + return this.resolveName(uuid, false); + } void shutdown(); diff --git a/fabric/src/main/java/net/draycia/carbon/fabric/CarbonServerFabric.java b/fabric/src/main/java/net/draycia/carbon/fabric/CarbonServerFabric.java index 5961088c..17b15e7d 100644 --- a/fabric/src/main/java/net/draycia/carbon/fabric/CarbonServerFabric.java +++ b/fabric/src/main/java/net/draycia/carbon/fabric/CarbonServerFabric.java @@ -76,7 +76,7 @@ public final class CarbonServerFabric implements CarbonServer, ForwardingAudienc } @Override - public CompletableFuture<@Nullable UUID> resolveUUID(final String username) { + public CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) { final @Nullable ServerPlayer serverPlayer = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayerByName(username); if (serverPlayer == null) { return CompletableFuture.completedFuture(null); @@ -85,7 +85,7 @@ public final class CarbonServerFabric implements CarbonServer, ForwardingAudienc } @Override - public CompletableFuture<@Nullable String> resolveName(final UUID uuid) { + public CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) { final @Nullable ServerPlayer serverPlayer = this.carbonChatFabric.minecraftServer().getPlayerList().getPlayer(uuid); if (serverPlayer == null) { return CompletableFuture.completedFuture(null); diff --git a/paper/src/main/java/net/draycia/carbon/paper/CarbonServerPaper.java b/paper/src/main/java/net/draycia/carbon/paper/CarbonServerPaper.java index 01a87ffe..6b1c6274 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/CarbonServerPaper.java +++ b/paper/src/main/java/net/draycia/carbon/paper/CarbonServerPaper.java @@ -77,13 +77,13 @@ public final class CarbonServerPaper implements CarbonServer, ForwardingAudience } @Override - public CompletableFuture<@Nullable UUID> resolveUUID(final String username) { - return this.profileResolver.resolveUUID(username); + public CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) { + return this.profileResolver.resolveUUID(username, cacheOnly); } @Override - public CompletableFuture<@Nullable String> resolveName(final UUID uuid) { - return this.profileResolver.resolveName(uuid); + public CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) { + return this.profileResolver.resolveName(uuid, cacheOnly); } } diff --git a/paper/src/main/java/net/draycia/carbon/paper/users/PaperProfileResolver.java b/paper/src/main/java/net/draycia/carbon/paper/users/PaperProfileResolver.java index b1a0d6e2..b658411b 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/users/PaperProfileResolver.java +++ b/paper/src/main/java/net/draycia/carbon/paper/users/PaperProfileResolver.java @@ -45,23 +45,23 @@ public final class PaperProfileResolver implements ProfileResolver { } @Override - public CompletableFuture<@Nullable UUID> resolveUUID(final String username) { + public CompletableFuture<@Nullable UUID> resolveUUID(final String username, final boolean cacheOnly) { final @Nullable Player online = this.server.getPlayer(username); if (online != null) { return CompletableFuture.completedFuture(online.getUniqueId()); } - return this.mojang.resolveUUID(username); + return this.mojang.resolveUUID(username, cacheOnly); } @Override - public CompletableFuture<@Nullable String> resolveName(final UUID uuid) { + public CompletableFuture<@Nullable String> resolveName(final UUID uuid, final boolean cacheOnly) { final @Nullable Player online = this.server.getPlayer(uuid); if (online != null) { return CompletableFuture.completedFuture(online.getName()); } - return this.mojang.resolveName(uuid); + return this.mojang.resolveName(uuid, cacheOnly); } @Override