mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
Add cache and better executor handling
This commit is contained in:
1 parent
3eb8f39cd8
commit
fd31de9d9f
1 file changed
+38
-4
@@ -8,8 +8,15 @@ import java.net.URISyntaxException;
|
|||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
@@ -20,14 +27,22 @@ public class MojangProfileResolver implements ProfileResolver {
|
|||||||
private final HttpClient client;
|
private final HttpClient client;
|
||||||
private final Gson gson;
|
private final Gson gson;
|
||||||
|
|
||||||
public MojangProfileResolver() {
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
|
private final Map<String, CompletableFuture<@Nullable UUID>> pendingUuidLookups = new HashMap<>();
|
||||||
|
private final Map<UUID, CompletableFuture<@Nullable String>> pendingUsernameLookups = new HashMap<>();
|
||||||
|
|
||||||
|
public MojangProfileResolver(final Logger logger) {
|
||||||
this.client = HttpClient.newHttpClient();
|
this.client = HttpClient.newHttpClient();
|
||||||
this.gson = new Gson();
|
this.gson = new Gson();
|
||||||
|
|
||||||
|
this.executorService = Executors.newSingleThreadExecutor(ConcurrentUtil.carbonThreadFactory(logger, "MojangProfileResolver"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
public CompletableFuture<@Nullable UUID> resolveUUID(final String username) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return this.pendingUuidLookups.computeIfAbsent(username, $ -> {
|
||||||
|
final CompletableFuture<@Nullable UUID> mojangLookup = CompletableFuture.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
final HttpRequest request = HttpRequest.newBuilder()
|
final HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://api.mojang.com/users/profiles/minecraft/" + username))
|
.uri(new URI("https://api.mojang.com/users/profiles/minecraft/" + username))
|
||||||
@@ -46,12 +61,22 @@ public class MojangProfileResolver implements ProfileResolver {
|
|||||||
} catch (final URISyntaxException | IOException | InterruptedException e) {
|
} catch (final URISyntaxException | IOException | InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
}, this.executorService);
|
||||||
|
|
||||||
|
mojangLookup.whenComplete((result, $$$) -> {
|
||||||
|
synchronized (this) {
|
||||||
|
this.pendingUuidLookups.remove(username);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return mojangLookup;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
public CompletableFuture<@Nullable String> resolveName(final UUID uuid) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return this.pendingUsernameLookups.computeIfAbsent(uuid, $ -> {
|
||||||
|
final CompletableFuture<@Nullable String> mojangLookup = CompletableFuture.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
final HttpRequest request = HttpRequest.newBuilder()
|
final HttpRequest request = HttpRequest.newBuilder()
|
||||||
.uri(new URI("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "")))
|
.uri(new URI("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "")))
|
||||||
@@ -70,12 +95,21 @@ public class MojangProfileResolver implements ProfileResolver {
|
|||||||
} catch (final URISyntaxException | IOException | InterruptedException e) {
|
} catch (final URISyntaxException | IOException | InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
}, this.executorService);
|
||||||
|
|
||||||
|
mojangLookup.whenComplete((result, $$$) -> {
|
||||||
|
synchronized (this) {
|
||||||
|
this.pendingUsernameLookups.remove(uuid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return mojangLookup;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
|
ConcurrentUtil.shutdownExecutor(this.executorService, TimeUnit.MILLISECONDS, 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
private record BasicLookupResponse(UUID id, String name) {
|
private record BasicLookupResponse(UUID id, String name) {
|
||||||
|
|||||||
Reference in new issue
Block a user