Skip to content

Commit 944f9ab

Browse files
committed
Add a config prop to control memory pooling
1 parent e6b3cf3 commit 944f9ab

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ public interface AsyncHttpClientConfig {
257257

258258
boolean isValidateResponseHeaders();
259259

260+
boolean isUsePooledMemory();
261+
260262
interface AdditionalChannelInitializer {
261263

262264
void initChannel(Channel channel) throws Exception;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
117117
private final Map<ChannelOption<Object>, Object> channelOptions;
118118
private final EventLoopGroup eventLoopGroup;
119119
private final boolean useNativeTransport;
120+
private final boolean usePooledMemory;
120121
private final Timer nettyTimer;
121122
private final ThreadFactory threadFactory;
122123
private final AdditionalChannelInitializer httpAdditionalChannelInitializer;
@@ -181,6 +182,7 @@ private DefaultAsyncHttpClientConfig(//
181182
Map<ChannelOption<Object>, Object> channelOptions,//
182183
EventLoopGroup eventLoopGroup,//
183184
boolean useNativeTransport,//
185+
boolean usePooledMemory,//
184186
Timer nettyTimer,//
185187
ThreadFactory threadFactory,//
186188
AdditionalChannelInitializer httpAdditionalChannelInitializer,//
@@ -244,6 +246,7 @@ private DefaultAsyncHttpClientConfig(//
244246
this.channelOptions = channelOptions;
245247
this.eventLoopGroup = eventLoopGroup;
246248
this.useNativeTransport = useNativeTransport;
249+
this.usePooledMemory = usePooledMemory;
247250
this.nettyTimer = nettyTimer;
248251
this.threadFactory = threadFactory;
249252
this.httpAdditionalChannelInitializer = httpAdditionalChannelInitializer;
@@ -493,6 +496,11 @@ public boolean isUseNativeTransport() {
493496
return useNativeTransport;
494497
}
495498

499+
@Override
500+
public boolean isUsePooledMemory() {
501+
return usePooledMemory;
502+
}
503+
496504
@Override
497505
public Timer getNettyTimer() {
498506
return nettyTimer;
@@ -580,6 +588,7 @@ public static class Builder {
580588
private int webSocketMaxBufferSize = defaultWebSocketMaxBufferSize();
581589
private int webSocketMaxFrameSize = defaultWebSocketMaxFrameSize();
582590
private boolean useNativeTransport = defaultUseNativeTransport();
591+
private boolean usePooledMemory = defaultUsePooledMemory();
583592
private Map<ChannelOption<Object>, Object> channelOptions = new HashMap<>();
584593
private EventLoopGroup eventLoopGroup;
585594
private Timer nettyTimer;
@@ -647,6 +656,7 @@ public Builder(AsyncHttpClientConfig config) {
647656
channelOptions.putAll(config.getChannelOptions());
648657
eventLoopGroup = config.getEventLoopGroup();
649658
useNativeTransport = config.isUseNativeTransport();
659+
usePooledMemory = config.isUsePooledMemory();
650660
nettyTimer = config.getNettyTimer();
651661
threadFactory = config.getThreadFactory();
652662
httpAdditionalChannelInitializer = config.getHttpAdditionalChannelInitializer();
@@ -1018,6 +1028,7 @@ public DefaultAsyncHttpClientConfig build() {
10181028
channelOptions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(channelOptions),//
10191029
eventLoopGroup, //
10201030
useNativeTransport, //
1031+
usePooledMemory, //
10211032
nettyTimer, //
10221033
threadFactory, //
10231034
httpAdditionalChannelInitializer, //

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static boolean defaultUseProxySelector() {
7878
public static boolean defaultUseProxyProperties() {
7979
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "useProxyProperties");
8080
}
81-
81+
8282
public static boolean defaultValidateResponseHeaders() {
8383
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "validateResponseHeaders");
8484
}
@@ -162,4 +162,8 @@ public static int defaultShutdownTimeout() {
162162
public static boolean defaultUseNativeTransport() {
163163
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "useNativeTransport");
164164
}
165+
166+
public static boolean defaultUsePooledMemory() {
167+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "usePooledMemory");
168+
}
165169
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.asynchttpclient.util.MiscUtils.trimStackTrace;
1717
import io.netty.bootstrap.Bootstrap;
1818
import io.netty.buffer.PooledByteBufAllocator;
19+
import io.netty.buffer.UnpooledByteBufAllocator;
1920
import io.netty.channel.Channel;
2021
import io.netty.channel.ChannelInitializer;
2122
import io.netty.channel.ChannelOption;
@@ -192,7 +193,7 @@ private Bootstrap newBootstrap(Class<? extends Channel> socketChannelClass, Even
192193
@SuppressWarnings("deprecation")
193194
Bootstrap bootstrap = new Bootstrap().channel(socketChannelClass).group(eventLoopGroup)//
194195
// default to PooledByteBufAllocator
195-
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)//
196+
.option(ChannelOption.ALLOCATOR, config.isUsePooledMemory() ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT)//
196197
.option(ChannelOption.TCP_NODELAY, true)//
197198
.option(ChannelOption.AUTO_CLOSE, false);
198199

client/src/main/java/org/asynchttpclient/netty/request/body/BodyChunkedInput.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import static org.asynchttpclient.util.Assertions.assertNotNull;
1717
import io.netty.buffer.ByteBuf;
18-
import io.netty.buffer.ByteBufAllocator;
1918
import io.netty.channel.ChannelHandlerContext;
2019
import io.netty.handler.stream.ChunkedInput;
2120

@@ -48,19 +47,20 @@ public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
4847
if (endOfInput)
4948
return null;
5049

51-
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(chunkSize);
50+
ByteBuf buffer = ctx.alloc().buffer(chunkSize);
51+
5252
Body.BodyState state = body.transferTo(buffer);
5353
switch (state) {
54-
case STOP:
55-
endOfInput = true;
56-
return buffer;
57-
case SUSPEND:
58-
//this will suspend the stream in ChunkedWriteHandler
59-
return null;
60-
case CONTINUE:
61-
return buffer;
62-
default:
63-
throw new IllegalStateException("Unknown state: " + state);
54+
case STOP:
55+
endOfInput = true;
56+
return buffer;
57+
case SUSPEND:
58+
// this will suspend the stream in ChunkedWriteHandler
59+
return null;
60+
case CONTINUE:
61+
return buffer;
62+
default:
63+
throw new IllegalStateException("Unknown state: " + state);
6464
}
6565
}
6666

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ org.asynchttpclient.keepEncodingHeader=false
3636
org.asynchttpclient.shutdownQuietPeriod=2000
3737
org.asynchttpclient.shutdownTimeout=15000
3838
org.asynchttpclient.useNativeTransport=false
39+
org.asynchttpclient.usePooledMemory=true

0 commit comments

Comments
 (0)