We are using the https://github.com/mrniko/netty-socketio.
Application scenario : Server is pushing messages via websocket to the client at a high rate.
Recently we encountered an issue where the client was unable to process all the event messages from the socket.io server. The websocket messages started piling up and the client became unresponsive.In Chrome developer tools, we could see a gradual increase in the memory of the client.
Analyzing the tcpdump using wireshark shows that there are lot of retransmissions happening and the client is unable to cope up.
We are planning to have a throttling mechanism at the server side.Is there any event/callback that can be used to detect slow clients so that we can start throttling at the server side for this particular websocket?
Yes. Channel.isWritable()
is a good place to start. To simplify a little bit, it returns false
when you have too much pending outbound bytes in the outbound buffer of the channel. You could keep sending until Channel.isWritable()
returns false
. When the writability of the channel changes, your handler is notified with channelWritabilityChanged()
event. Then, you can resume your transfer.
public void generateTraffic(ChannelHandlerContext ctx) {
while (ctx.channel().isWritable()) {
ctx.write(...);
}
}
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
generateTraffic(ctx);
}
In a real application, you'll need to maintain some stateful information to determine what message should be sent when.
Also, please take a look into ChannelConfig.writeBufferHigh/LowWatermark
properties to configure when isWritable()
should start to return false
or true
.