13
0
geforkt von Mirrors/Velocity

Properly use non-blocking DNS in AsyncHttpClient

Dieser Commit ist enthalten in:
Andrew Steinborn 2019-07-28 23:40:23 -04:00
Ursprung 8d5f5e48f2
Commit bf035bce73

Datei anzeigen

@ -22,7 +22,11 @@ import java.util.Map;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.asynchttpclient.AsyncHttpClient; import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Dsl; import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.filter.FilterContext;
import org.asynchttpclient.filter.FilterContext.FilterContextBuilder;
import org.asynchttpclient.filter.FilterException;
import org.asynchttpclient.filter.RequestFilter;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class ConnectionManager { public final class ConnectionManager {
@ -55,15 +59,26 @@ public final class ConnectionManager {
this.workerGroup = this.transportType.createEventLoopGroup(TransportType.Type.WORKER); this.workerGroup = this.transportType.createEventLoopGroup(TransportType.Type.WORKER);
this.serverChannelInitializer = new ServerChannelInitializerHolder( this.serverChannelInitializer = new ServerChannelInitializerHolder(
new ServerChannelInitializer(this.server)); new ServerChannelInitializer(this.server));
this.resolverGroup = new DnsAddressResolverGroup(
new DnsNameResolverBuilder() DnsNameResolverBuilder builder = new DnsNameResolverBuilder()
.channelType(this.transportType.datagramChannelClass) .channelType(this.transportType.datagramChannelClass)
.negativeTtl(15) .negativeTtl(15)
.ndots(1) .ndots(1);
);
this.resolverGroup = new DnsAddressResolverGroup(builder);
this.httpClient = asyncHttpClient(config() this.httpClient = asyncHttpClient(config()
.setEventLoopGroup(this.workerGroup) .setEventLoopGroup(this.workerGroup)
.setUserAgent(server.getVersion().getName() + "/" + server.getVersion().getVersion()) .setUserAgent(server.getVersion().getName() + "/" + server.getVersion().getVersion())
.addRequestFilter(new RequestFilter() {
@Override
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
return new FilterContextBuilder<>(ctx)
.request(new RequestBuilder(ctx.getRequest())
.setNameResolver(builder.eventLoop(workerGroup.next()).build())
.build())
.build();
}
})
.build()); .build());
} }