Skip to content

Commit 5cb66f7

Browse files
committed
Share common provider configuration
1 parent 7b2023a commit 5cb66f7

30 files changed

+443
-455
lines changed

api/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfig.java

Lines changed: 267 additions & 102 deletions
Large diffs are not rendered by default.

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.asynchttpclient.config;
1414

15-
1615
public final class AsyncHttpClientConfigDefaults {
1716

1817
private AsyncHttpClientConfigDefaults() {
@@ -109,10 +108,46 @@ public static boolean defaultAcceptAnyCertificate() {
109108
}
110109

111110
public static Integer defaultSslSessionCacheSize() {
112-
return Integer.getInteger(ASYNC_CLIENT_CONFIG_ROOT + "sslSessionCacheSize");
111+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInteger(ASYNC_CLIENT_CONFIG_ROOT + "sslSessionCacheSize");
113112
}
114113

115114
public static Integer defaultSslSessionTimeout() {
116-
return Integer.getInteger(ASYNC_CLIENT_CONFIG_ROOT + "sslSessionTimeout");
115+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInteger(ASYNC_CLIENT_CONFIG_ROOT + "sslSessionTimeout");
116+
}
117+
118+
public static int defaultHttpClientCodecMaxInitialLineLength() {
119+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "httpClientCodecMaxInitialLineLength");
120+
}
121+
122+
public static int defaultHttpClientCodecMaxHeaderSize() {
123+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "httpClientCodecMaxHeaderSize");
124+
}
125+
126+
public static int defaultHttpClientCodecMaxChunkSize() {
127+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "httpClientCodecMaxChunkSize");
128+
}
129+
130+
public static boolean defaultDisableZeroCopy() {
131+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "disableZeroCopy");
132+
}
133+
134+
public static long defaultHandshakeTimeout() {
135+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getLong(ASYNC_CLIENT_CONFIG_ROOT + "handshakeTimeout");
136+
}
137+
138+
public static int defaultChunkedFileChunkSize() {
139+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "chunkedFileChunkSize");
140+
}
141+
142+
public static int defaultWebSocketMaxBufferSize() {
143+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "webSocketMaxBufferSize");
144+
}
145+
146+
public static int defaultWebSocketMaxFrameSize() {
147+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + "webSocketMaxFrameSize");
148+
}
149+
150+
public static boolean defaultKeepEncodingHeader() {
151+
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + "keepEncodingHeader");
117152
}
118153
}

api/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ public int getInt(String key) {
7777
return Integer.parseInt(getString(key));
7878
}
7979

80+
public long getLong(String key) {
81+
return Long.parseLong(getString(key));
82+
}
83+
84+
public int getInteger(String key) {
85+
String s = getString(key);
86+
return s != null ? Integer.valueOf(s) : null;
87+
}
88+
8089
public boolean getBoolean(String key) {
8190
return Boolean.parseBoolean(getString(key));
8291
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ org.asynchttpclient.maxRequestRetry=5
2121
org.asynchttpclient.allowPoolingSslConnections=true
2222
org.asynchttpclient.disableUrlEncodingForBoundRequests=false
2323
org.asynchttpclient.removeQueryParamOnRedirect=true
24-
org.asynchttpclient.acceptAnyCertificate=false
24+
org.asynchttpclient.acceptAnyCertificate=false
25+
org.asynchttpclient.httpClientCodecMaxInitialLineLength=4096
26+
org.asynchttpclient.httpClientCodecMaxHeaderSize=8192
27+
org.asynchttpclient.httpClientCodecMaxChunkSize=8192
28+
org.asynchttpclient.disableZeroCopy=false
29+
org.asynchttpclient.handshakeTimeout=10000
30+
org.asynchttpclient.chunkedFileChunkSize=8192
31+
org.asynchttpclient.webSocketMaxBufferSize=128000000
32+
org.asynchttpclient.webSocketMaxFrameSize=10240
33+
org.asynchttpclient.keepEncodingHeader=false

providers/netty3/src/main/java/org/asynchttpclient/netty/NettyAsyncHttpProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
3838
private final AsyncHttpClientConfig config;
3939
private final AtomicBoolean closed = new AtomicBoolean(false);
4040
private final ChannelManager channelManager;
41-
private final NettyAsyncHttpProviderConfig nettyConfig;
4241
private final boolean allowStopNettyTimer;
4342
private final Timer nettyTimer;
4443

@@ -47,15 +46,15 @@ public class NettyAsyncHttpProvider extends SimpleChannelUpstreamHandler impleme
4746
public NettyAsyncHttpProvider(AsyncHttpClientConfig config) {
4847

4948
this.config = config;
50-
nettyConfig = config.getAsyncHttpProviderConfig() instanceof NettyAsyncHttpProviderConfig ? //
49+
NettyAsyncHttpProviderConfig nettyConfig = config.getAsyncHttpProviderConfig() instanceof NettyAsyncHttpProviderConfig ? //
5150
(NettyAsyncHttpProviderConfig) config.getAsyncHttpProviderConfig()
5251
: new NettyAsyncHttpProviderConfig();
5352

5453
allowStopNettyTimer = nettyConfig.getNettyTimer() == null;
5554
nettyTimer = allowStopNettyTimer ? newNettyTimer() : nettyConfig.getNettyTimer();
5655

5756
channelManager = new ChannelManager(config, nettyConfig, nettyTimer);
58-
requestSender = new NettyRequestSender(config, nettyConfig, channelManager, nettyTimer, closed);
57+
requestSender = new NettyRequestSender(config, channelManager, nettyTimer, closed);
5958
channelManager.configureBootstraps(requestSender, closed);
6059
}
6160

providers/netty3/src/main/java/org/asynchttpclient/netty/NettyAsyncHttpProviderConfig.java

Lines changed: 11 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import java.util.concurrent.ExecutorService;
2020

2121
import org.asynchttpclient.AsyncHttpProviderConfig;
22-
import org.asynchttpclient.channel.SSLEngineFactory;
22+
import org.asynchttpclient.config.AsyncHttpClientConfig;
2323
import org.asynchttpclient.netty.channel.pool.ChannelPool;
2424
import org.asynchttpclient.netty.handler.ConnectionStrategy;
2525
import org.asynchttpclient.netty.handler.Http1Point1ConnectionStrategy;
2626
import org.asynchttpclient.netty.ws.NettyWebSocket;
27-
import org.asynchttpclient.netty.NettyAsyncHttpProviderConfig;
2827
import org.jboss.netty.channel.Channel;
2928
import org.jboss.netty.channel.ChannelPipeline;
3029
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
@@ -33,19 +32,19 @@
3332
import org.jboss.netty.util.Timer;
3433

3534
/**
36-
* This class can be used to pass Netty's internal configuration options. See Netty documentation for more information.
35+
* This class can be used to pass Netty's internal configuration options. See
36+
* Netty documentation for more information.
3737
*/
3838
public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<String, Object> {
3939

4040
private final ConcurrentHashMap<String, Object> properties = new ConcurrentHashMap<>();
4141

4242
/**
43-
* Add a property that will be used when the AsyncHttpClient initialize its {@link com.ning.http.client.AsyncHttpProvider}
43+
* Add a property that will be used when the AsyncHttpClient initialize its
44+
* {@link com.ning.http.client.AsyncHttpProvider}
4445
*
45-
* @param name
46-
* the name of the property
47-
* @param value
48-
* the value of the property
46+
* @param name the name of the property
47+
* @param value the value of the property
4948
* @return this instance of AsyncHttpProviderConfig
5049
*/
5150
public NettyAsyncHttpProviderConfig addProperty(String name, Object value) {
@@ -111,44 +110,17 @@ public Set<Map.Entry<String, Object>> propertiesSet() {
111110
private AdditionalPipelineInitializer httpsAdditionalPipelineInitializer;
112111
private AdditionalPipelineInitializer wssAdditionalPipelineInitializer;
113112

114-
/**
115-
* Allow configuring Netty's HttpClientCodecs.
116-
*/
117-
private int httpClientCodecMaxInitialLineLength = 4096;
118-
private int httpClientCodecMaxHeaderSize = 8192;
119-
private int httpClientCodecMaxChunkSize = 8192;
120-
121113
/**
122114
* Allow configuring the Netty's socket channel factory.
123115
*/
124116
private NioClientSocketChannelFactory socketChannelFactory;
125117

126118
private ChannelPool channelPool;
127119

128-
/**
129-
* Allow one to disable zero copy for bodies and use chunking instead
130-
*/
131-
private boolean disableZeroCopy;
132-
133120
private Timer nettyTimer;
134121

135-
private long handshakeTimeout = 10000L;
136-
137-
private SSLEngineFactory sslEngineFactory;
138-
139-
/**
140-
* chunkedFileChunkSize
141-
*/
142-
private int chunkedFileChunkSize = 8192;
143-
144122
private NettyWebSocketFactory nettyWebSocketFactory = new DefaultNettyWebSocketFactory();
145123

146-
private int webSocketMaxBufferSize = 128000000;
147-
148-
private int webSocketMaxFrameSize = 10 * 1024;
149-
150-
private boolean keepEncodingHeader = false;
151-
152124
private ConnectionStrategy<HttpRequest, HttpResponse> connectionStrategy = new Http1Point1ConnectionStrategy();
153125

154126
public boolean isUseDeadLockChecker() {
@@ -166,7 +138,7 @@ public ExecutorService getBossExecutorService() {
166138
public void setBossExecutorService(ExecutorService bossExecutorService) {
167139
this.bossExecutorService = bossExecutorService;
168140
}
169-
141+
170142
public AdditionalPipelineInitializer getHttpAdditionalPipelineInitializer() {
171143
return httpAdditionalPipelineInitializer;
172144
}
@@ -199,30 +171,6 @@ public void setWssAdditionalPipelineInitializer(AdditionalPipelineInitializer ws
199171
this.wssAdditionalPipelineInitializer = wssAdditionalPipelineInitializer;
200172
}
201173

202-
public int getHttpClientCodecMaxInitialLineLength() {
203-
return httpClientCodecMaxInitialLineLength;
204-
}
205-
206-
public void setHttpClientCodecMaxInitialLineLength(int httpClientCodecMaxInitialLineLength) {
207-
this.httpClientCodecMaxInitialLineLength = httpClientCodecMaxInitialLineLength;
208-
}
209-
210-
public int getHttpClientCodecMaxHeaderSize() {
211-
return httpClientCodecMaxHeaderSize;
212-
}
213-
214-
public void setHttpClientCodecMaxHeaderSize(int httpClientCodecMaxHeaderSize) {
215-
this.httpClientCodecMaxHeaderSize = httpClientCodecMaxHeaderSize;
216-
}
217-
218-
public int getHttpClientCodecMaxChunkSize() {
219-
return httpClientCodecMaxChunkSize;
220-
}
221-
222-
public void setHttpClientCodecMaxChunkSize(int httpClientCodecMaxChunkSize) {
223-
this.httpClientCodecMaxChunkSize = httpClientCodecMaxChunkSize;
224-
}
225-
226174
public NioClientSocketChannelFactory getSocketChannelFactory() {
227175
return socketChannelFactory;
228176
}
@@ -231,14 +179,6 @@ public void setSocketChannelFactory(NioClientSocketChannelFactory socketChannelF
231179
this.socketChannelFactory = socketChannelFactory;
232180
}
233181

234-
public void setDisableZeroCopy(boolean disableZeroCopy) {
235-
this.disableZeroCopy = disableZeroCopy;
236-
}
237-
238-
public boolean isDisableZeroCopy() {
239-
return disableZeroCopy;
240-
}
241-
242182
public Timer getNettyTimer() {
243183
return nettyTimer;
244184
}
@@ -247,14 +187,6 @@ public void setNettyTimer(Timer nettyTimer) {
247187
this.nettyTimer = nettyTimer;
248188
}
249189

250-
public long getHandshakeTimeout() {
251-
return handshakeTimeout;
252-
}
253-
254-
public void setHandshakeTimeout(long handshakeTimeout) {
255-
this.handshakeTimeout = handshakeTimeout;
256-
}
257-
258190
public ChannelPool getChannelPool() {
259191
return channelPool;
260192
}
@@ -263,22 +195,6 @@ public void setChannelPool(ChannelPool channelPool) {
263195
this.channelPool = channelPool;
264196
}
265197

266-
public SSLEngineFactory getSslEngineFactory() {
267-
return sslEngineFactory;
268-
}
269-
270-
public void setSslEngineFactory(SSLEngineFactory sslEngineFactory) {
271-
this.sslEngineFactory = sslEngineFactory;
272-
}
273-
274-
public int getChunkedFileChunkSize() {
275-
return chunkedFileChunkSize;
276-
}
277-
278-
public void setChunkedFileChunkSize(int chunkedFileChunkSize) {
279-
this.chunkedFileChunkSize = chunkedFileChunkSize;
280-
}
281-
282198
public NettyWebSocketFactory getNettyWebSocketFactory() {
283199
return nettyWebSocketFactory;
284200
}
@@ -287,30 +203,6 @@ public void setNettyWebSocketFactory(NettyWebSocketFactory nettyWebSocketFactory
287203
this.nettyWebSocketFactory = nettyWebSocketFactory;
288204
}
289205

290-
public int getWebSocketMaxBufferSize() {
291-
return webSocketMaxBufferSize;
292-
}
293-
294-
public void setWebSocketMaxBufferSize(int webSocketMaxBufferSize) {
295-
this.webSocketMaxBufferSize = webSocketMaxBufferSize;
296-
}
297-
298-
public int getWebSocketMaxFrameSize() {
299-
return webSocketMaxFrameSize;
300-
}
301-
302-
public void setWebSocketMaxFrameSize(int webSocketMaxFrameSize) {
303-
this.webSocketMaxFrameSize = webSocketMaxFrameSize;
304-
}
305-
306-
public boolean isKeepEncodingHeader() {
307-
return keepEncodingHeader;
308-
}
309-
310-
public void setKeepEncodingHeader(boolean keepEncodingHeader) {
311-
this.keepEncodingHeader = keepEncodingHeader;
312-
}
313-
314206
public ConnectionStrategy<HttpRequest, HttpResponse> getConnectionStrategy() {
315207
return connectionStrategy;
316208
}
@@ -320,7 +212,7 @@ public void setConnectionStrategy(ConnectionStrategy<HttpRequest, HttpResponse>
320212
}
321213

322214
public static interface NettyWebSocketFactory {
323-
NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig);
215+
NettyWebSocket newNettyWebSocket(Channel channel, AsyncHttpClientConfig config);
324216
}
325217

326218
public static interface AdditionalPipelineInitializer {
@@ -331,8 +223,8 @@ public static interface AdditionalPipelineInitializer {
331223
public class DefaultNettyWebSocketFactory implements NettyWebSocketFactory {
332224

333225
@Override
334-
public NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig) {
335-
return new NettyWebSocket(channel, nettyConfig);
226+
public NettyWebSocket newNettyWebSocket(Channel channel, AsyncHttpClientConfig config) {
227+
return new NettyWebSocket(channel, config);
336228
}
337229
}
338230
}

providers/netty3/src/main/java/org/asynchttpclient/netty/channel/ChannelManager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public ChannelManager(final AsyncHttpClientConfig config, NettyAsyncHttpProvider
113113
this.config = config;
114114
this.nettyConfig = nettyConfig;
115115
this.nettyTimer = nettyTimer;
116-
this.sslEngineFactory = nettyConfig.getSslEngineFactory() != null ? nettyConfig.getSslEngineFactory() : new SSLEngineFactory.DefaultSSLEngineFactory(config);
116+
this.sslEngineFactory = config.getSslEngineFactory() != null ? config.getSslEngineFactory() : new SSLEngineFactory.DefaultSSLEngineFactory(config);
117117

118118
ChannelPool channelPool = nettyConfig.getChannelPool();
119119
if (channelPool == null && config.isAllowPoolingConnections()) {
@@ -169,7 +169,7 @@ public Semaphore apply(Object partitionKey) {
169169
semaphoreComputer = null;
170170
}
171171

172-
handshakeTimeout = nettyConfig.getHandshakeTimeout();
172+
handshakeTimeout = config.getHandshakeTimeout();
173173

174174
if (nettyConfig.getSocketChannelFactory() != null) {
175175
socketChannelFactory = nettyConfig.getSocketChannelFactory();
@@ -278,7 +278,7 @@ public ChannelPipeline getPipeline() throws Exception {
278278
}
279279

280280
private HttpContentDecompressor newHttpContentDecompressor() {
281-
if (nettyConfig.isKeepEncodingHeader())
281+
if (config.isKeepEncodingHeader())
282282
return new HttpContentDecompressor() {
283283
@Override
284284
protected String getTargetContentEncoding(String contentEncoding) throws Exception {
@@ -390,9 +390,9 @@ public void registerOpenChannel(Channel channel, Object partitionKey) {
390390

391391
private HttpClientCodec newHttpClientCodec() {
392392
return new HttpClientCodec(//
393-
nettyConfig.getHttpClientCodecMaxInitialLineLength(),//
394-
nettyConfig.getHttpClientCodecMaxHeaderSize(),//
395-
nettyConfig.getHttpClientCodecMaxChunkSize());
393+
config.getHttpClientCodecMaxInitialLineLength(),//
394+
config.getHttpClientCodecMaxHeaderSize(),//
395+
config.getHttpClientCodecMaxChunkSize());
396396
}
397397

398398
public SslHandler createSslHandler(String peerHost, int peerPort) throws GeneralSecurityException, IOException {
@@ -450,8 +450,8 @@ public ClientBootstrap getBootstrap(String scheme, boolean useProxy, boolean use
450450
public void upgradePipelineForWebSockets(ChannelPipeline pipeline) {
451451
pipeline.addAfter(HTTP_HANDLER, WS_ENCODER_HANDLER, new WebSocket08FrameEncoder(true));
452452
pipeline.remove(HTTP_HANDLER);
453-
pipeline.addBefore(WS_PROCESSOR, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, false, nettyConfig.getWebSocketMaxFrameSize()));
454-
pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(nettyConfig.getWebSocketMaxBufferSize()));
453+
pipeline.addBefore(WS_PROCESSOR, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, false, config.getWebSocketMaxFrameSize()));
454+
pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(config.getWebSocketMaxBufferSize()));
455455
}
456456

457457
public final Callback newDrainCallback(final NettyResponseFuture<?> future, final Channel channel, final boolean keepAlive, final Object partitionKey) {

0 commit comments

Comments
 (0)