Skip to content

Commit ebff613

Browse files
committed
Let user define ByteBufAllocator implementation, close AsyncHttpClient#1279
1 parent c65f05a commit ebff613

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.asynchttpclient;
1515

1616
import io.netty.buffer.ByteBuf;
17+
import io.netty.buffer.ByteBufAllocator;
1718
import io.netty.channel.Channel;
1819
import io.netty.channel.ChannelOption;
1920
import io.netty.channel.EventLoopGroup;
@@ -285,7 +286,7 @@ public interface AsyncHttpClientConfig {
285286

286287
int getSoRcvBuf();
287288

288-
boolean isUsePooledMemory();
289+
ByteBufAllocator getAllocator();
289290

290291
interface AdditionalChannelInitializer {
291292

client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClientConfig.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.asynchttpclient;
1717

1818
import static org.asynchttpclient.config.AsyncHttpClientConfigDefaults.*;
19+
import io.netty.buffer.ByteBufAllocator;
1920
import io.netty.channel.ChannelOption;
2021
import io.netty.channel.EventLoopGroup;
2122
import io.netty.handler.ssl.SslContext;
@@ -119,7 +120,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
119120
private final Map<ChannelOption<Object>, Object> channelOptions;
120121
private final EventLoopGroup eventLoopGroup;
121122
private final boolean useNativeTransport;
122-
private final boolean usePooledMemory;
123+
private final ByteBufAllocator allocator;
123124
private final boolean tcpNoDelay;
124125
private final boolean soReuseAddress;
125126
private final int soLinger;
@@ -197,7 +198,7 @@ private DefaultAsyncHttpClientConfig(//
197198
Map<ChannelOption<Object>, Object> channelOptions,//
198199
EventLoopGroup eventLoopGroup,//
199200
boolean useNativeTransport,//
200-
boolean usePooledMemory,//
201+
ByteBufAllocator allocator,//
201202
Timer nettyTimer,//
202203
ThreadFactory threadFactory,//
203204
AdditionalChannelInitializer httpAdditionalChannelInitializer,//
@@ -269,7 +270,7 @@ private DefaultAsyncHttpClientConfig(//
269270
this.channelOptions = channelOptions;
270271
this.eventLoopGroup = eventLoopGroup;
271272
this.useNativeTransport = useNativeTransport;
272-
this.usePooledMemory = usePooledMemory;
273+
this.allocator = allocator;
273274
this.nettyTimer = nettyTimer;
274275
this.threadFactory = threadFactory;
275276
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
@@ -551,8 +552,8 @@ public boolean isUseNativeTransport() {
551552
}
552553

553554
@Override
554-
public boolean isUsePooledMemory() {
555-
return usePooledMemory;
555+
public ByteBufAllocator getAllocator() {
556+
return allocator;
556557
}
557558

558559
@Override
@@ -650,7 +651,7 @@ public static class Builder {
650651
private int webSocketMaxBufferSize = defaultWebSocketMaxBufferSize();
651652
private int webSocketMaxFrameSize = defaultWebSocketMaxFrameSize();
652653
private boolean useNativeTransport = defaultUseNativeTransport();
653-
private boolean usePooledMemory = defaultUsePooledMemory();
654+
private ByteBufAllocator allocator;
654655
private Map<ChannelOption<Object>, Object> channelOptions = new HashMap<>();
655656
private EventLoopGroup eventLoopGroup;
656657
private Timer nettyTimer;
@@ -725,7 +726,7 @@ public Builder(AsyncHttpClientConfig config) {
725726
channelOptions.putAll(config.getChannelOptions());
726727
eventLoopGroup = config.getEventLoopGroup();
727728
useNativeTransport = config.isUseNativeTransport();
728-
usePooledMemory = config.isUsePooledMemory();
729+
allocator = config.getAllocator();
729730
nettyTimer = config.getNettyTimer();
730731
threadFactory = config.getThreadFactory();
731732
httpAdditionalChannelInitializer = config.getHttpAdditionalChannelInitializer();
@@ -1035,8 +1036,8 @@ public Builder setUseNativeTransport(boolean useNativeTransport) {
10351036
return this;
10361037
}
10371038

1038-
public Builder setUsePooledMemory(boolean usePooledMemory) {
1039-
this.usePooledMemory = usePooledMemory;
1039+
public Builder setAllocator(ByteBufAllocator allocator) {
1040+
this.allocator = allocator;
10401041
return this;
10411042
}
10421043

@@ -1133,7 +1134,7 @@ public DefaultAsyncHttpClientConfig build() {
11331134
channelOptions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(channelOptions),//
11341135
eventLoopGroup, //
11351136
useNativeTransport, //
1136-
usePooledMemory, //
1137+
allocator, //
11371138
nettyTimer, //
11381139
threadFactory, //
11391140
httpAdditionalChannelInitializer, //

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,4 @@ public static int defaultShutdownTimeout() {
197197
public static boolean defaultUseNativeTransport() {
198198
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "useNativeTransport");
199199
}
200-
201-
public static boolean defaultUsePooledMemory() {
202-
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "usePooledMemory");
203-
}
204200
}

client/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
import static org.asynchttpclient.util.MiscUtils.trimStackTrace;
1717
import io.netty.bootstrap.Bootstrap;
1818
import io.netty.bootstrap.ChannelFactory;
19-
import io.netty.buffer.PooledByteBufAllocator;
20-
import io.netty.buffer.UnpooledByteBufAllocator;
19+
import io.netty.buffer.ByteBufAllocator;
2120
import io.netty.channel.Channel;
2221
import io.netty.channel.ChannelInitializer;
2322
import io.netty.channel.ChannelOption;
@@ -198,7 +197,7 @@ private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory,
198197
@SuppressWarnings("deprecation")
199198
Bootstrap bootstrap = new Bootstrap().channelFactory(channelFactory).group(eventLoopGroup)//
200199
// default to PooledByteBufAllocator
201-
.option(ChannelOption.ALLOCATOR, config.isUsePooledMemory() ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT)//
200+
.option(ChannelOption.ALLOCATOR, config.getAllocator() != null ? config.getAllocator() : ByteBufAllocator.DEFAULT)//
202201
.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())//
203202
.option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())//
204203
.option(ChannelOption.AUTO_CLOSE, false);

client/src/main/resources/ahc-default.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,3 @@ org.asynchttpclient.keepEncodingHeader=false
4242
org.asynchttpclient.shutdownQuietPeriod=2000
4343
org.asynchttpclient.shutdownTimeout=15000
4444
org.asynchttpclient.useNativeTransport=false
45-
org.asynchttpclient.usePooledMemory=true

0 commit comments

Comments
 (0)