Skip to content

Commit

Permalink
Revert "Create a new HttpClient per connection (PaperMC#1243)"
Browse files Browse the repository at this point in the history
This reverts commit ecf936f.
  • Loading branch information
rybot666 committed May 9, 2024
1 parent 6a791e2 commit 1930334
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,8 @@ public void closeListeners() {
this.cm.closeEndpoints(false);
}

public HttpClient createHttpClient() {
return cm.createHttpClient();
public HttpClient getHttpClient() {
return cm.getHttpClient();
}

public Ratelimiter getIpAttemptLimiter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import io.netty.buffer.ByteBuf;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -215,8 +214,7 @@ public boolean handle(EncryptionResponsePacket packet) {
server.getVersion().getName() + "/" + server.getVersion().getVersion())
.uri(URI.create(url))
.build();
final HttpClient httpClient = server.createHttpClient();
httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
server.getHttpClient().sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
.whenCompleteAsync((response, throwable) -> {
if (mcConnection.isClosed()) {
// The player disconnected after we authenticated them.
Expand Down Expand Up @@ -267,18 +265,7 @@ public boolean handle(EncryptionResponsePacket packet) {
response.statusCode(), login.getUsername(), playerIp);
inbound.disconnect(Component.translatable("multiplayer.disconnect.authservers_down"));
}
}, 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);
}
}
});
}, mcConnection.eventLoop());
} catch (GeneralSecurityException e) {
logger.error("Unable to enable encryption", e);
mcConnection.close(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public final class ConnectionManager {
public final BackendChannelInitializerHolder backendChannelInitializer;

private final SeparatePoolInetNameResolver resolver;
private final HttpClient httpClient;

/**
* Initializes the {@code ConnectionManager}.
* Initalizes the {@code ConnectionManager}.
*
* @param server a reference to the Velocity server
*/
Expand All @@ -78,6 +79,9 @@ public ConnectionManager(VelocityServer server) {
this.backendChannelInitializer = new BackendChannelInitializerHolder(
new BackendChannelInitializer(this.server));
this.resolver = new SeparatePoolInetNameResolver(GlobalEventExecutor.INSTANCE);
this.httpClient = HttpClient.newBuilder()
.executor(this.workerGroup)
.build();
}

public void logChannelInformation() {
Expand Down Expand Up @@ -234,11 +238,8 @@ public ServerChannelInitializerHolder getServerChannelInitializer() {
return this.serverChannelInitializer;
}

@SuppressWarnings("checkstyle:MissingJavadocMethod")
public HttpClient createHttpClient() {
return HttpClient.newBuilder()
.executor(this.workerGroup)
.build();
public HttpClient getHttpClient() {
return this.httpClient;
}

public BackendChannelInitializerHolder getBackendChannelInitializer() {
Expand Down

0 comments on commit 1930334

Please sign in to comment.