Skip to content

Commit 4e8d62e

Browse files
author
Stephane Landelle
committed
Have a config parameter for websocket max buffer size, close AsyncHttpClient#658
1 parent 7a06754 commit 4e8d62e

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ public class NettyAsyncHttpProviderConfig implements AsyncHttpProviderConfig<Cha
4343
* Add a property that will be used when the AsyncHttpClient initialize its
4444
* {@link org.asynchttpclient.AsyncHttpProvider}
4545
*
46-
* @param name
47-
* the name of the property
48-
* @param value
49-
* the value of the property
46+
* @param name the name of the property
47+
* @param value the value of the property
5048
* @return this instance of AsyncHttpProviderConfig
5149
*/
5250
public NettyAsyncHttpProviderConfig addProperty(ChannelOption<Object> name, Object value) {
@@ -116,17 +114,17 @@ public NettyResponseBodyPart newResponseBodyPart(ByteBuf buf, boolean last) {
116114
}
117115

118116
public static interface NettyWebSocketFactory {
119-
NettyWebSocket newNettyWebSocket(Channel channel);
117+
NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig);
120118
}
121119

122120
public class DefaultNettyWebSocketFactory implements NettyWebSocketFactory {
123121

124122
@Override
125-
public NettyWebSocket newNettyWebSocket(Channel channel) {
126-
return new NettyWebSocket(channel);
123+
public NettyWebSocket newNettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig) {
124+
return new NettyWebSocket(channel, nettyConfig);
127125
}
128126
}
129-
127+
130128
/**
131129
* Allow configuring the Netty's event loop.
132130
*/
@@ -166,6 +164,8 @@ public NettyWebSocket newNettyWebSocket(Channel channel) {
166164

167165
private NettyWebSocketFactory nettyWebSocketFactory = new DefaultNettyWebSocketFactory();
168166

167+
private int webSocketMaxBufferSize = 128000000;
168+
169169
public EventLoopGroup getEventLoopGroup() {
170170
return eventLoopGroup;
171171
}
@@ -293,4 +293,12 @@ public NettyWebSocketFactory getNettyWebSocketFactory() {
293293
public void setNettyWebSocketFactory(NettyWebSocketFactory nettyWebSocketFactory) {
294294
this.nettyWebSocketFactory = nettyWebSocketFactory;
295295
}
296+
297+
public int getWebSocketMaxBufferSize() {
298+
return webSocketMaxBufferSize;
299+
}
300+
301+
public void setWebSocketMaxBufferSize(int webSocketMaxBufferSize) {
302+
this.webSocketMaxBufferSize = webSocketMaxBufferSize;
303+
}
296304
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/WebSocketProtocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public WebSocketProtocol(ChannelManager channelManager,//
6161
private void invokeOnSucces(Channel channel, WebSocketUpgradeHandler h) {
6262
if (!h.touchSuccess()) {
6363
try {
64-
h.onSuccess(nettyConfig.getNettyWebSocketFactory().newNettyWebSocket(channel));
64+
h.onSuccess(nettyConfig.getNettyWebSocketFactory().newNettyWebSocket(channel, nettyConfig));
6565
} catch (Exception ex) {
6666
logger.warn("onSuccess unexpected exception", ex);
6767
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/ws/NettyWebSocket.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.ConcurrentLinkedQueue;
3232

3333
import org.asynchttpclient.HttpResponseBodyPart;
34+
import org.asynchttpclient.providers.netty.NettyAsyncHttpProviderConfig;
3435
import org.asynchttpclient.providers.netty.response.NettyResponseBodyPart;
3536
import org.asynchttpclient.websocket.WebSocket;
3637
import org.asynchttpclient.websocket.WebSocketByteFragmentListener;
@@ -50,19 +51,20 @@ public class NettyWebSocket implements WebSocket {
5051

5152
protected final Channel channel;
5253
protected final Collection<WebSocketListener> listeners;
53-
protected int maxBufferSize = 128000000;
54+
protected final int maxBufferSize;
5455
private int bufferSize;
5556
private List<byte[]> _fragments;
5657
private volatile boolean interestedInByteMessages;
5758
private volatile boolean interestedInTextMessages;
5859

59-
public NettyWebSocket(Channel channel) {
60-
this(channel, new ConcurrentLinkedQueue<WebSocketListener>());
60+
public NettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig) {
61+
this(channel, nettyConfig, new ConcurrentLinkedQueue<WebSocketListener>());
6162
}
6263

63-
public NettyWebSocket(Channel channel, Collection<WebSocketListener> listeners) {
64+
public NettyWebSocket(Channel channel, NettyAsyncHttpProviderConfig nettyConfig, Collection<WebSocketListener> listeners) {
6465
this.channel = channel;
6566
this.listeners = listeners;
67+
maxBufferSize = nettyConfig.getWebSocketMaxBufferSize();
6668
}
6769

6870
@Override
@@ -107,14 +109,6 @@ public WebSocket sendPong(byte[] payload) {
107109
return this;
108110
}
109111

110-
public int getMaxBufferSize() {
111-
return maxBufferSize;
112-
}
113-
114-
public void setMaxBufferSize(int maxBufferSize) {
115-
this.maxBufferSize = Math.max(maxBufferSize, 8192);
116-
}
117-
118112
@Override
119113
public boolean isOpen() {
120114
return channel.isOpen();

0 commit comments

Comments
 (0)