3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-25 15:50:19 +01:00

Create a new HttpClient per connection (#1243)

* Create a new HttpClient for each connection

If the instance is using Java 21, the HttpClient resources will be cleaned using its AutoCloseable interface
Dieser Commit ist enthalten in:
Adrian 2024-02-15 09:59:34 -05:00 committet von GitHub
Ursprung ef861819e3
Commit ecf936f356
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
3 geänderte Dateien mit 23 neuen und 11 gelöschten Zeilen

Datei anzeigen

@ -595,8 +595,8 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
this.cm.closeEndpoints(false); this.cm.closeEndpoints(false);
} }
public HttpClient getHttpClient() { public HttpClient createHttpClient() {
return cm.getHttpClient(); return cm.createHttpClient();
} }
public Ratelimiter getIpAttemptLimiter() { public Ratelimiter getIpAttemptLimiter() {

Datei anzeigen

@ -43,6 +43,7 @@ import com.velocitypowered.proxy.protocol.packet.ServerLoginPacket;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
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.security.GeneralSecurityException; import java.security.GeneralSecurityException;
@ -213,7 +214,8 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
server.getVersion().getName() + "/" + server.getVersion().getVersion()) server.getVersion().getName() + "/" + server.getVersion().getVersion())
.uri(URI.create(url)) .uri(URI.create(url))
.build(); .build();
server.getHttpClient().sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()) final HttpClient httpClient = server.createHttpClient();
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
.whenCompleteAsync((response, throwable) -> { .whenCompleteAsync((response, throwable) -> {
if (mcConnection.isClosed()) { if (mcConnection.isClosed()) {
// The player disconnected after we authenticated them. // The player disconnected after we authenticated them.
@ -264,7 +266,18 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
response.statusCode(), login.getUsername(), playerIp); response.statusCode(), login.getUsername(), playerIp);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down")); inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
} }
}, mcConnection.eventLoop()); }, mcConnection.eventLoop())
.thenRun(() -> {
if (httpClient instanceof final AutoCloseable closeable) {
try {
closeable.close();
} catch (Exception e) {
// In Java 21, the HttpClient does not throw any Exception
// when trying to clean its resources, so this should not happen
logger.error("An unknown error occurred while trying to close an HttpClient", e);
}
}
});
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
logger.error("Unable to enable encryption", e); logger.error("Unable to enable encryption", e);
mcConnection.close(true); mcConnection.close(true);

Datei anzeigen

@ -62,10 +62,9 @@ public final class ConnectionManager {
public final BackendChannelInitializerHolder backendChannelInitializer; public final BackendChannelInitializerHolder backendChannelInitializer;
private final SeparatePoolInetNameResolver resolver; private final SeparatePoolInetNameResolver resolver;
private final HttpClient httpClient;
/** /**
* Initalizes the {@code ConnectionManager}. * Initializes the {@code ConnectionManager}.
* *
* @param server a reference to the Velocity server * @param server a reference to the Velocity server
*/ */
@ -79,9 +78,6 @@ public final class ConnectionManager {
this.backendChannelInitializer = new BackendChannelInitializerHolder( this.backendChannelInitializer = new BackendChannelInitializerHolder(
new BackendChannelInitializer(this.server)); new BackendChannelInitializer(this.server));
this.resolver = new SeparatePoolInetNameResolver(GlobalEventExecutor.INSTANCE); this.resolver = new SeparatePoolInetNameResolver(GlobalEventExecutor.INSTANCE);
this.httpClient = HttpClient.newBuilder()
.executor(this.workerGroup)
.build();
} }
public void logChannelInformation() { public void logChannelInformation() {
@ -238,8 +234,11 @@ public final class ConnectionManager {
return this.serverChannelInitializer; return this.serverChannelInitializer;
} }
public HttpClient getHttpClient() { @SuppressWarnings("checkstyle:MissingJavadocMethod")
return this.httpClient; public HttpClient createHttpClient() {
return HttpClient.newBuilder()
.executor(this.workerGroup)
.build();
} }
public BackendChannelInitializerHolder getBackendChannelInitializer() { public BackendChannelInitializerHolder getBackendChannelInitializer() {