Skip to content

Commit

Permalink
update okhttp
Browse files Browse the repository at this point in the history
  • Loading branch information
ham1255 committed Dec 23, 2024
1 parent 1443f54 commit 87947bb
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Constants {
public final static String GIT_COMMIT = "@git_commit@";

public static String getGithubCommitLink() {
return "https://github.com/ProxioDev/RedisBungee/commit/" + GIT_COMMIT;
return "https://github.com/ProxioDev/ValioBungee/commit/" + GIT_COMMIT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,7 @@
import java.util.Calendar;
import java.util.UUID;

public class CachedUUIDEntry {
private final String name;
private final UUID uuid;
private final Calendar expiry;

public CachedUUIDEntry(String name, UUID uuid, Calendar expiry) {
this.name = name;
this.uuid = uuid;
this.expiry = expiry;
}

public String getName() {
return name;
}

public UUID getUuid() {
return uuid;
}

public Calendar getExpiry() {
return expiry;
}
public record CachedUUIDEntry(String name, UUID uuid, Calendar expiry) {

public boolean expired() {
return Calendar.getInstance().after(expiry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.ResponseBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class NameFetcher {
private static OkHttpClient httpClient;
private static final OkHttpClient httpClient = new OkHttpClient();
private static final Gson gson = new Gson();

public static void setHttpClient(OkHttpClient httpClient) {
NameFetcher.httpClient = httpClient;
}

public static List<String> nameHistoryFromUuid(UUID uuid) throws IOException {
String name = getName(uuid);
if (name == null) return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.squareup.okhttp.*;
import okhttp3.*;

import java.util.HashMap;
import java.util.List;
Expand All @@ -28,13 +28,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
private final List<String> names;
private final boolean rateLimiting;
private static final Gson gson = new Gson();


public static void setHttpClient(OkHttpClient httpClient) {
UUIDFetcher.httpClient = httpClient;
}

private static OkHttpClient httpClient;
private static final OkHttpClient httpClient = new OkHttpClient();

private UUIDFetcher(List<String> names, boolean rateLimiting) {
this.names = ImmutableList.copyOf(names);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public UUID getTranslatedUuid(@NonNull String player, boolean expensiveLookups)
CachedUUIDEntry cachedUUIDEntry = nameToUuidMap.get(player.toLowerCase());
if (cachedUUIDEntry != null) {
if (!cachedUUIDEntry.expired())
return cachedUUIDEntry.getUuid();
return cachedUUIDEntry.uuid();
else
nameToUuidMap.remove(player);
}
Expand Down Expand Up @@ -93,11 +93,11 @@ public UUID unifiedJedisTask(UnifiedJedis unifiedJedis) {
if (entry.expired()) {
unifiedJedis.hdel("uuid-cache", player.toLowerCase());
// Doesn't hurt to also remove the UUID entry as well.
unifiedJedis.hdel("uuid-cache", entry.getUuid().toString());
unifiedJedis.hdel("uuid-cache", entry.uuid().toString());
} else {
nameToUuidMap.put(player.toLowerCase(), entry);
uuidToNameMap.put(entry.getUuid(), entry);
return entry.getUuid();
uuidToNameMap.put(entry.uuid(), entry);
return entry.uuid();
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ public String getNameFromUuid(@NonNull UUID player, boolean expensiveLookups) {
CachedUUIDEntry cachedUUIDEntry = uuidToNameMap.get(player);
if (cachedUUIDEntry != null) {
if (!cachedUUIDEntry.expired())
return cachedUUIDEntry.getName();
return cachedUUIDEntry.name();
else
uuidToNameMap.remove(player);
}
Expand All @@ -159,11 +159,11 @@ public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
unifiedJedis.hdel("uuid-cache", player.toString());
// Doesn't hurt to also remove the named entry as well.
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
unifiedJedis.hdel("uuid-cache", entry.getName());
unifiedJedis.hdel("uuid-cache", entry.name());
} else {
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
nameToUuidMap.put(entry.name().toLowerCase(), entry);
uuidToNameMap.put(player, entry);
return entry.getName();
return entry.name();
}
}

Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
guava = "31.1-jre"
jedis = "5.2.0"
okhttp = "2.7.5"
okhttp = "4.12.0"
configurateV3 = "3.7.3"
caffeine = "3.1.8"
adventure = "4.18.0"
Expand All @@ -12,13 +12,13 @@ velocity = "3.4.0-SNAPSHOT"

[plugins]
blossom = { id = "net.kyori.blossom", version = "1.2.0" }
shadow = { id = "com.github.johnrengelman.shadow", version = "8.1.1" }
run-velocity = { id = "xyz.jpenilla.run-velocity", version = "2.0.0" }
shadow = { id = "com.gradleup.shadow", version = "8.3.5" }
run-velocity = { id = "xyz.jpenilla.run-velocity", version = "2.3.1" }

[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
jedis = { module = "redis.clients:jedis", version.ref = "jedis" }
okhttp = { module = "com.squareup.okhttp:okhttp", version.ref = "okhttp" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
configurateV3 = { module = "org.spongepowered:configurate-yaml", version.ref = "configurateV3" }
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" }

Expand Down
2 changes: 1 addition & 1 deletion proxies/bungeecord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tasks {
relocate("redis.clients.jedis", "com.imaginarycode.minecraft.redisbungee.internal.jedis")
relocate("redis.clients.util", "com.imaginarycode.minecraft.redisbungee.internal.jedisutil")
relocate("org.apache.commons.pool", "com.imaginarycode.minecraft.redisbungee.internal.commonspool")
relocate("com.squareup.okhttp", "com.imaginarycode.minecraft.redisbungee.internal.okhttp")
relocate("com.squareup.okhttp3", "com.imaginarycode.minecraft.redisbungee.internal.okhttp3")
relocate("okio", "com.imaginarycode.minecraft.redisbungee.internal.okio")
relocate("org.json", "com.imaginarycode.minecraft.redisbungee.internal.json")
// configurate shade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.InitialUtils;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.NameFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDFetcher;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
import com.imaginarycode.minecraft.redisbungee.commands.CommandLoader;
import com.imaginarycode.minecraft.redisbungee.commands.utils.CommandPlatformHelper;
import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.squareup.okhttp.Dispatcher;
import com.squareup.okhttp.OkHttpClient;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.md_5.bungee.api.ProxyServer;
Expand Down Expand Up @@ -72,7 +68,6 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin<ProxiedPlay
private UUIDTranslator uuidTranslator;
private RedisBungeeConfiguration configuration;
private LangConfiguration langConfiguration;
private OkHttpClient httpClient;
private BungeeCommandManager commandManager;

private final Logger logger = LoggerFactory.getLogger("RedisBungee");
Expand Down Expand Up @@ -241,11 +236,6 @@ protected void handlePlatformCommandExecution(String command) {
// cleanup
this.cleanupTask = getProxy().getScheduler().schedule(this, () -> this.proxyDataManager.correctionTask(), 0, 60, TimeUnit.SECONDS);
// init the http lib
httpClient = new OkHttpClient();
Dispatcher dispatcher = new Dispatcher(getExecutorService());
httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
InitialUtils.checkRedisVersion(this);
uuidTranslator = new UUIDTranslator(this);

Expand Down
4 changes: 2 additions & 2 deletions proxies/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ java {

tasks {
runVelocity {
velocityVersion("3.4.0-SNAPSHOT")
velocityVersion(libs.versions.velocity.get())
environment["REDISBUNGEE_PROXY_ID"] = "velocity-1"
environment["REDISBUNGEE_NETWORK_ID"] = "dev"
}
Expand All @@ -36,7 +36,7 @@ tasks {
relocate("redis.clients.jedis", "com.imaginarycode.minecraft.redisbungee.internal.jedis")
relocate("redis.clients.util", "com.imaginarycode.minecraft.redisbungee.internal.jedisutil")
relocate("org.apache.commons.pool", "com.imaginarycode.minecraft.redisbungee.internal.commonspool")
relocate("com.squareup.okhttp", "com.imaginarycode.minecraft.redisbungee.internal.okhttp")
relocate("com.squareup.okhttp3", "com.imaginarycode.minecraft.redisbungee.internal.okhttp3")
relocate("okio", "com.imaginarycode.minecraft.redisbungee.internal.okio")
relocate("org.json", "com.imaginarycode.minecraft.redisbungee.internal.json")
// acf shade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.squareup.okhttp.Dispatcher;
import com.squareup.okhttp.OkHttpClient;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
Expand Down Expand Up @@ -78,7 +76,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player>, Con
private final UUIDTranslator uuidTranslator;
private RedisBungeeConfiguration configuration;
private LangConfiguration langConfiguration;
private final OkHttpClient httpClient;

private final ProxyDataManager proxyDataManager;

Expand Down Expand Up @@ -127,11 +124,6 @@ protected void handlePlatformCommandExecution(String command) {
};
this.playerDataManager = new VelocityPlayerDataManager(this);
uuidTranslator = new UUIDTranslator(this);
this.httpClient = new OkHttpClient();
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(6));
this.httpClient.setDispatcher(dispatcher);
NameFetcher.setHttpClient(httpClient);
UUIDFetcher.setHttpClient(httpClient);
}


Expand Down Expand Up @@ -300,22 +292,13 @@ public void stop() {
if (heartbeatTask != null) {
heartbeatTask.cancel();
}


try {
this.proxyDataManager.close();
this.jedisSummoner.close();
} catch (Exception e) {
throw new RuntimeException(e);
}

this.httpClient.getDispatcher().getExecutorService().shutdown();
try {
logInfo("waiting for httpclient thread-pool termination.....");
this.httpClient.getDispatcher().getExecutorService().awaitTermination(20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (commandManager != null) commandManager.unregisterCommands();
logInfo("RedisBungee shutdown complete");
}
Expand Down

0 comments on commit 87947bb

Please sign in to comment.