Improve mojang api rate limit handling

This commit is contained in:
Jason Penilla committed 2023-03-26 11:41:50 -07:00
1 parent 4026d40145
commit c846d51ee9
1 file changed
+11 -11
@@ -59,7 +59,8 @@ public final class MojangProfileResolver implements ProfileResolver {
private final Map<String, CompletableFuture<@Nullable BasicLookupResponse>> pendingUuidLookups = new HashMap<>(); private final Map<String, CompletableFuture<@Nullable BasicLookupResponse>> pendingUuidLookups = new HashMap<>();
private final Map<UUID, CompletableFuture<@Nullable BasicLookupResponse>> pendingUsernameLookups = new HashMap<>(); private final Map<UUID, CompletableFuture<@Nullable BasicLookupResponse>> pendingUsernameLookups = new HashMap<>();
private final ProfileCache cache; private final ProfileCache cache;
private final RateLimiter rateLimiter; private final RateLimiter globalRateLimit;
private final RateLimiter uuidToProfileRateLimit;
@Inject @Inject
private MojangProfileResolver(final Logger logger, final ProfileCache cache) { private MojangProfileResolver(final Logger logger, final ProfileCache cache) {
@@ -69,7 +70,8 @@ public final class MojangProfileResolver implements ProfileResolver {
.create(); .create();
this.executorService = Executors.newFixedThreadPool(2, ConcurrentUtil.carbonThreadFactory(logger, "MojangProfileResolver")); this.executorService = Executors.newFixedThreadPool(2, ConcurrentUtil.carbonThreadFactory(logger, "MojangProfileResolver"));
this.cache = cache; this.cache = cache;
this.rateLimiter = new RateLimiter(); this.globalRateLimit = new RateLimiter(600);
this.uuidToProfileRateLimit = new RateLimiter(200);
} }
@Override @Override
@@ -81,7 +83,7 @@ public final class MojangProfileResolver implements ProfileResolver {
return CompletableFuture.completedFuture(this.cache.cachedId(username)); return CompletableFuture.completedFuture(this.cache.cachedId(username));
} }
return this.pendingUuidLookups.computeIfAbsent(username, $ -> { return this.pendingUuidLookups.computeIfAbsent(username, $ -> {
if (!this.rateLimiter.canSubmit()) { if (!this.globalRateLimit.canSubmit()) {
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }
final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> { final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> {
@@ -117,7 +119,7 @@ public final class MojangProfileResolver implements ProfileResolver {
return CompletableFuture.completedFuture(this.cache.cachedName(uuid)); return CompletableFuture.completedFuture(this.cache.cachedName(uuid));
} }
return this.pendingUsernameLookups.computeIfAbsent(uuid, $ -> { return this.pendingUsernameLookups.computeIfAbsent(uuid, $ -> {
if (!this.rateLimiter.canSubmit()) { if (!this.globalRateLimit.canSubmit() || !this.uuidToProfileRateLimit.canSubmit()) {
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }
final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> { final CompletableFuture<@Nullable BasicLookupResponse> mojangLookup = CompletableFuture.supplyAsync(() -> {
@@ -181,7 +183,7 @@ public final class MojangProfileResolver implements ProfileResolver {
@Override @Override
public void shutdown() { public void shutdown() {
ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.MILLISECONDS, 500); ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.MILLISECONDS, 500);
this.rateLimiter.shutdown(); this.globalRateLimit.shutdown();
} }
private record BasicLookupResponse(UUID id, String name) { private record BasicLookupResponse(UUID id, String name) {
@@ -208,18 +210,16 @@ public final class MojangProfileResolver implements ProfileResolver {
private static final class RateLimiter { private static final class RateLimiter {
// Mojang rate limit is 600 requests per ten minutes private final AtomicInteger available;
private static final int RATE_LIMIT = 600;
private final AtomicInteger available = new AtomicInteger(RATE_LIMIT);
private final Timer timer; private final Timer timer;
private RateLimiter() { private RateLimiter(final int perTenMinutes) {
this.timer = new Timer("CarbonChat " + this); this.timer = new Timer("CarbonChat " + this);
this.available = new AtomicInteger(perTenMinutes);
this.timer.scheduleAtFixedRate(new TimerTask() { this.timer.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run() { public void run() {
RateLimiter.this.available.set(RATE_LIMIT); RateLimiter.this.available.set(perTenMinutes);
} }
}, 0L, Duration.ofMinutes(10).toMillis()); }, 0L, Duration.ofMinutes(10).toMillis());
} }