Skip to content

Commit c056a6c

Browse files
committed
Don't always recreate the http client
1 parent 476cc46 commit c056a6c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/main/java/net/raphimc/authhook/AuthHookHttpServer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@
3434
import java.net.http.HttpClient;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.concurrent.ExecutorService;
38+
import java.util.concurrent.Executors;
3739
import java.util.concurrent.TimeUnit;
3840

3941
public class AuthHookHttpServer {
4042

4143
private final InetSocketAddress bindAddress;
4244
private final ChannelFuture channelFuture;
4345
private final Map<String, ProxyConnection> pendingConnections = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).<String, ProxyConnection>build().asMap();
46+
private final HttpClient httpClient = HttpClient.newBuilder().executor(Executors.newCachedThreadPool()).build();
4447

4548
public AuthHookHttpServer(final InetSocketAddress bindAddress) {
4649
this.bindAddress = bindAddress;
@@ -100,7 +103,6 @@ protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
100103
}
101104
}
102105

103-
final HttpClient httpClient = HttpClient.newHttpClient();
104106
httpClient.sendAsync(java.net.http.HttpRequest.newBuilder().uri(URI.create("https://sessionserver.mojang.com" + uri)).build(), java.net.http.HttpResponse.BodyHandlers.ofByteArray())
105107
.thenAccept(response -> {
106108
final FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), ctx.alloc().buffer());
@@ -137,6 +139,13 @@ public void stop() {
137139
if (this.channelFuture != null) {
138140
this.channelFuture.channel().close();
139141
}
142+
this.httpClient.executor().map(ExecutorService.class::cast).ifPresent(ExecutorService::shutdown);
143+
if (this.httpClient instanceof AutoCloseable closeable) {
144+
try {
145+
closeable.close();
146+
} catch (Exception ignored) {
147+
}
148+
}
140149
}
141150

142151
public Channel getChannel() {

0 commit comments

Comments
 (0)