Reputation: 191
I'm writing a TCP server using Netty v4. The server will handle multiple connections from clients, and stream data to them.
I want to be able to detect when a client is consuming data at a slow ratio. I basically want to avoid the TCP buffers to get full just because a client is slow!
It's basically what ZeroMQ does (called "Slow Subscriber Detection (Suicidal Snail Pattern)"). How can that be done using Netty?
My current code is (I'll just show the server setup):
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1000)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new Handler());
}
});
ChannelFuture f = b.bind(8000).sync();
f.channel().closeFuture().sync();
Is that wha the SO_BACKLOG
option does? It says it's for connections being queued, but I'm interested in packets being queued for a specific connection.
Upvotes: 1
Views: 2260
Reputation: 12777
In case your only goal is to avoid buffer overflow (back pressure) this approach would be enough :
if (ctx.channel().isWritable()) {
ctx.writeAndFlush(msg);
}
Upvotes: 0
Reputation: 16056
One way you might do this is to use WriteBufferWaterMarks. Quoting from the javadoc:
WriteBufferWaterMark is used to set low water mark and high water mark for the write buffer. If the number of bytes queued in the write buffer exceeds the high water mark, Channel.isWritable() will start to return false. If the number of bytes queued in the write buffer exceeds the high water mark and then dropped down below the low water mark, Channel.isWritable() will start to return true again.
Setting the appropriate water marks on your child channel configs, a slow consumer will mark the channel not-writable for "long" periods of time versus snappy consumers. Therefore, listening on channel write-ability state changes and tracking the amount of time channels are non-writable should identify slow consumers and the relative severity of the backlog, and then you can disconnect a client if their sluggishness reaches some threshold.
Upvotes: 1
Reputation: 310840
Is that what the SO_BACKLOG option does?
Is what what the SO_BACKLOG option does?
It says it's for connections being queued
Correct.
but I'm interested in packets being queued for a specific connection
So you aren't interested in SO_BACKLOG.
Upvotes: 0