Skip to content

Commit cc92c54

Browse files
author
Stephane Landelle
committed
Refactor Netty provider config
1 parent 058f8a7 commit cc92c54

File tree

3 files changed

+104
-150
lines changed

3 files changed

+104
-150
lines changed

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

Lines changed: 94 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.asynchttpclient.providers.netty.response.EagerResponseBodyPart;
2323
import org.asynchttpclient.providers.netty.response.LazyResponseBodyPart;
2424
import org.asynchttpclient.providers.netty.response.NettyResponseBodyPart;
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
2725

2826
import io.netty.buffer.ByteBuf;
2927
import io.netty.channel.Channel;
@@ -38,67 +36,9 @@
3836
/**
3937
* This class can be used to pass Netty's internal configuration options. See Netty documentation for more information.
4038
*/
41-
public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<String, Object> {
39+
public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<ChannelOption<Object>, Object> {
4240

43-
private final static Logger LOGGER = LoggerFactory.getLogger(NettyAsyncHttpProviderConfig.class);
44-
45-
/**
46-
* Allow configuring the Netty's event loop.
47-
*/
48-
private EventLoopGroup eventLoopGroup;
49-
50-
private AdditionalChannelInitializer httpAdditionalChannelInitializer;
51-
private AdditionalChannelInitializer wsAdditionalChannelInitializer;
52-
private AdditionalChannelInitializer httpsAdditionalChannelInitializer;
53-
private AdditionalChannelInitializer wssAdditionalChannelInitializer;
54-
55-
/**
56-
* HttpClientCodec's maxInitialLineLength
57-
*/
58-
private int maxInitialLineLength = 4096;
59-
60-
/**
61-
* HttpClientCodec's maxHeaderSize
62-
*/
63-
private int maxHeaderSize = 8192;
64-
65-
/**
66-
* HttpClientCodec's maxChunkSize
67-
*/
68-
private int maxChunkSize = 8192;
69-
70-
/**
71-
* Use direct {@link java.nio.ByteBuffer}
72-
*/
73-
public final static String USE_DIRECT_BYTEBUFFER = "bufferFactory";
74-
75-
/**
76-
* Allow nested request from any {@link org.asynchttpclient.AsyncHandler}
77-
*/
78-
public final static String DISABLE_NESTED_REQUEST = "disableNestedRequest";
79-
80-
/**
81-
* See {@link java.net.Socket#setReuseAddress(boolean)}
82-
*/
83-
public final static String REUSE_ADDRESS = ChannelOption.SO_REUSEADDR.name();
84-
85-
private final Map<String, Object> properties = new HashMap<String, Object>();
86-
87-
private ResponseBodyPartFactory bodyPartFactory = new EagerResponseBodyPartFactory();
88-
89-
private ChannelPool channelPool;
90-
91-
private boolean disableZeroCopy;
92-
93-
private Timer nettyTimer;
94-
95-
private long handshakeTimeoutInMillis;
96-
97-
private SSLEngineFactory sslEngineFactory;
98-
99-
public NettyAsyncHttpProviderConfig() {
100-
properties.put(REUSE_ADDRESS, Boolean.FALSE);
101-
}
41+
private final Map<ChannelOption<Object>, Object> properties = new HashMap<ChannelOption<Object>, Object>();
10242

10343
/**
10444
* Add a property that will be used when the AsyncHttpClient initialize its
@@ -108,16 +48,14 @@ public NettyAsyncHttpProviderConfig() {
10848
* @param value the value of the property
10949
* @return this instance of AsyncHttpProviderConfig
11050
*/
111-
public NettyAsyncHttpProviderConfig addProperty(String name, Object value) {
112-
113-
if (name.equals(REUSE_ADDRESS)//
114-
&& value == Boolean.TRUE//
115-
&& System.getProperty("os.name").toLowerCase().contains("win")) {
116-
LOGGER.warn("Can't enable {} on Windows", REUSE_ADDRESS);
117-
} else {
118-
properties.put(name, value);
119-
}
51+
public NettyAsyncHttpProviderConfig addProperty(ChannelOption<Object> name, Object value) {
52+
properties.put(name, value);
53+
return this;
54+
}
12055

56+
@SuppressWarnings("unchecked")
57+
public <T> NettyAsyncHttpProviderConfig addChannelOption(ChannelOption<T> name, T value) {
58+
properties.put((ChannelOption<Object>) name, value);
12159
return this;
12260
}
12361

@@ -127,7 +65,7 @@ public NettyAsyncHttpProviderConfig addProperty(String name, Object value) {
12765
* @param name
12866
* @return this instance of AsyncHttpProviderConfig
12967
*/
130-
public Object getProperty(String name) {
68+
public Object getProperty(ChannelOption<Object> name) {
13169
return properties.get(name);
13270
}
13371

@@ -137,7 +75,7 @@ public Object getProperty(String name) {
13775
* @param name
13876
* @return true if removed
13977
*/
140-
public Object removeProperty(String name) {
78+
public Object removeProperty(ChannelOption<Object> name) {
14179
return properties.remove(name);
14280
}
14381

@@ -146,40 +84,79 @@ public Object removeProperty(String name) {
14684
*
14785
* @return a the curent entry set.
14886
*/
149-
public Set<Map.Entry<String, Object>> propertiesSet() {
87+
public Set<Map.Entry<ChannelOption<Object>, Object>> propertiesSet() {
15088
return properties.entrySet();
15189
}
15290

153-
public EventLoopGroup getEventLoopGroup() {
154-
return eventLoopGroup;
155-
}
91+
public static interface AdditionalChannelInitializer {
15692

157-
public void setEventLoopGroup(EventLoopGroup eventLoopGroup) {
158-
this.eventLoopGroup = eventLoopGroup;
93+
void initChannel(Channel ch) throws Exception;
15994
}
16095

161-
public int getMaxInitialLineLength() {
162-
return maxInitialLineLength;
163-
}
96+
public static interface ResponseBodyPartFactory {
16497

165-
public void setMaxInitialLineLength(int maxInitialLineLength) {
166-
this.maxInitialLineLength = maxInitialLineLength;
98+
NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last);
16799
}
168100

169-
public int getMaxHeaderSize() {
170-
return maxHeaderSize;
101+
public static class EagerResponseBodyPartFactory implements ResponseBodyPartFactory {
102+
103+
@Override
104+
public NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last) {
105+
return new EagerResponseBodyPart(buf, last);
106+
}
171107
}
172108

173-
public void setMaxHeaderSize(int maxHeaderSize) {
174-
this.maxHeaderSize = maxHeaderSize;
109+
public static class LazyResponseBodyPartFactory implements ResponseBodyPartFactory {
110+
111+
@Override
112+
public NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last) {
113+
return new LazyResponseBodyPart(buf, last);
114+
}
175115
}
176116

177-
public int getMaxChunkSize() {
178-
return maxChunkSize;
117+
/**
118+
* Allow configuring the Netty's event loop.
119+
*/
120+
private EventLoopGroup eventLoopGroup;
121+
122+
private AdditionalChannelInitializer httpAdditionalChannelInitializer;
123+
private AdditionalChannelInitializer wsAdditionalChannelInitializer;
124+
private AdditionalChannelInitializer httpsAdditionalChannelInitializer;
125+
private AdditionalChannelInitializer wssAdditionalChannelInitializer;
126+
127+
/**
128+
* HttpClientCodec's maxInitialLineLength
129+
*/
130+
private int maxInitialLineLength = 4096;
131+
132+
/**
133+
* HttpClientCodec's maxHeaderSize
134+
*/
135+
private int maxHeaderSize = 8192;
136+
137+
/**
138+
* HttpClientCodec's maxChunkSize
139+
*/
140+
private int maxChunkSize = 8192;
141+
142+
private ResponseBodyPartFactory bodyPartFactory = new EagerResponseBodyPartFactory();
143+
144+
private ChannelPool channelPool;
145+
146+
private boolean disableZeroCopy;
147+
148+
private Timer nettyTimer;
149+
150+
private long handshakeTimeoutInMillis;
151+
152+
private SSLEngineFactory sslEngineFactory;
153+
154+
public EventLoopGroup getEventLoopGroup() {
155+
return eventLoopGroup;
179156
}
180157

181-
public void setMaxChunkSize(int maxChunkSize) {
182-
this.maxChunkSize = maxChunkSize;
158+
public void setEventLoopGroup(EventLoopGroup eventLoopGroup) {
159+
this.eventLoopGroup = eventLoopGroup;
183160
}
184161

185162
public AdditionalChannelInitializer getHttpAdditionalChannelInitializer() {
@@ -214,6 +191,30 @@ public void setWssAdditionalChannelInitializer(AdditionalChannelInitializer wssA
214191
this.wssAdditionalChannelInitializer = wssAdditionalChannelInitializer;
215192
}
216193

194+
public int getMaxInitialLineLength() {
195+
return maxInitialLineLength;
196+
}
197+
198+
public void setMaxInitialLineLength(int maxInitialLineLength) {
199+
this.maxInitialLineLength = maxInitialLineLength;
200+
}
201+
202+
public int getMaxHeaderSize() {
203+
return maxHeaderSize;
204+
}
205+
206+
public void setMaxHeaderSize(int maxHeaderSize) {
207+
this.maxHeaderSize = maxHeaderSize;
208+
}
209+
210+
public int getMaxChunkSize() {
211+
return maxChunkSize;
212+
}
213+
214+
public void setMaxChunkSize(int maxChunkSize) {
215+
this.maxChunkSize = maxChunkSize;
216+
}
217+
217218
public ResponseBodyPartFactory getBodyPartFactory() {
218219
return bodyPartFactory;
219220
}
@@ -253,38 +254,12 @@ public long getHandshakeTimeoutInMillis() {
253254
public void setHandshakeTimeoutInMillis(long handshakeTimeoutInMillis) {
254255
this.handshakeTimeoutInMillis = handshakeTimeoutInMillis;
255256
}
256-
257+
257258
public SSLEngineFactory getSslEngineFactory() {
258259
return sslEngineFactory;
259260
}
260261

261262
public void setSslEngineFactory(SSLEngineFactory sslEngineFactory) {
262263
this.sslEngineFactory = sslEngineFactory;
263264
}
264-
265-
public static interface AdditionalChannelInitializer {
266-
267-
void initChannel(Channel ch) throws Exception;
268-
}
269-
270-
public static interface ResponseBodyPartFactory {
271-
272-
NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last);
273-
}
274-
275-
public static class EagerResponseBodyPartFactory implements ResponseBodyPartFactory {
276-
277-
@Override
278-
public NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last) {
279-
return new EagerResponseBodyPart(buf, last);
280-
}
281-
}
282-
283-
public static class LazyResponseBodyPartFactory implements ResponseBodyPartFactory {
284-
285-
@Override
286-
public NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last) {
287-
return new LazyResponseBodyPart(buf, last);
288-
}
289-
}
290265
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/channel/Channels.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@
6363
import javax.net.ssl.SSLEngine;
6464

6565
import java.io.IOException;
66-
import java.lang.reflect.Field;
6766
import java.security.GeneralSecurityException;
68-
import java.util.HashMap;
69-
import java.util.Map;
7067
import java.util.Map.Entry;
7168
import java.util.concurrent.Semaphore;
7269
import java.util.concurrent.TimeUnit;
@@ -162,31 +159,13 @@ public Channels(final AsyncHttpClientConfig config, NettyAsyncHttpProviderConfig
162159
freeConnections = null;
163160
}
164161

165-
Map<String, ChannelOption<Object>> optionMap = new HashMap<String, ChannelOption<Object>>();
166-
for (Field field : ChannelOption.class.getDeclaredFields()) {
167-
if (field.getType().isAssignableFrom(ChannelOption.class)) {
168-
field.setAccessible(true);
169-
try {
170-
optionMap.put(field.getName(), (ChannelOption<Object>) field.get(null));
171-
} catch (IllegalAccessException ex) {
172-
throw new Error(ex);
173-
}
174-
}
175-
}
176-
177-
if (nettyProviderConfig != null) {
178-
for (Entry<String, Object> entry : nettyProviderConfig.propertiesSet()) {
179-
ChannelOption<Object> key = optionMap.get(entry.getKey());
180-
if (key != null) {
181-
Object value = entry.getValue();
182-
plainBootstrap.option(key, value);
183-
webSocketBootstrap.option(key, value);
184-
secureBootstrap.option(key, value);
185-
secureWebSocketBootstrap.option(key, value);
186-
} else {
187-
throw new IllegalArgumentException("Unknown config property " + entry.getKey());
188-
}
189-
}
162+
for (Entry<ChannelOption<Object>, Object> entry : nettyProviderConfig.propertiesSet()) {
163+
ChannelOption<Object> key = entry.getKey();
164+
Object value = entry.getValue();
165+
plainBootstrap.option(key, value);
166+
webSocketBootstrap.option(key, value);
167+
secureBootstrap.option(key, value);
168+
secureWebSocketBootstrap.option(key, value);
190169
}
191170

192171
int timeOut = config.getConnectionTimeoutInMs() > 0 ? config.getConnectionTimeoutInMs() : Integer.MAX_VALUE;

providers/netty/src/test/java/org/asynchttpclient/providers/netty/NettyAsyncProviderBasicTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.asynchttpclient.AsyncHttpProviderConfig;
1818
import org.asynchttpclient.async.AsyncProvidersBasicTest;
1919

20+
import io.netty.channel.ChannelOption;
21+
2022
public class NettyAsyncProviderBasicTest extends AsyncProvidersBasicTest {
2123

2224
@Override
@@ -26,9 +28,7 @@ public AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
2628

2729
@Override
2830
protected AsyncHttpProviderConfig<?, ?> getProviderConfig() {
29-
final NettyAsyncHttpProviderConfig config = new NettyAsyncHttpProviderConfig();
30-
config.addProperty("TCP_NODELAY", true);
31-
return config;
31+
return new NettyAsyncHttpProviderConfig().addChannelOption(ChannelOption.TCP_NODELAY, Boolean.TRUE);
3232
}
3333

3434
@Override

0 commit comments

Comments
 (0)