I have a client that needs to send alot of small messages to a server (< 8k each messages), now a single thread can push about 1.2 million of messages one after the other without delay in between the reception of those messages.
How would I configure my netty client to "buffer" the write so it flushes automatically at a given buffer size and doesn't create me OOM errors ?
As a simple example, let's say I want to send the content of a file, line by line to simulate the reception of such messages.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
});
// Start the connection attempt.
Channel ch = b.connect("localhost", 6000).sync().channel();
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File(
"data.txt"))));
int i = 0;
while (true) {
// This simulates the incoming data
String line = in.readLine();
if (line == null) {
break;
}
lastWriteFuture = ch.write(line + '\n');
// TODO : Investigate why we need this, otherwise it throws OOM errors...
if (i++ % 10000 == 0) {
ch.flush();
lastWriteFuture.awaitUninterruptibly();
}
}
// This part is here only because it's a simulation, there is no notion of "end of data" in our normal process, it's continuous send of data.
ch.flush();
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
} finally {
// The connection is closed automatically on shutdown.
group.shutdownGracefully();
}
What I'm trying to figure out, is how can I make continuous write without the need to flush at every X messages ?
I've seen Norman's Maurer talk about VoidChannelPromise, however that can't be used here as I'll eventually need SslHandler in the pipeline as well.
Do I need and how to implement some throttling ?
Also, Netty 3 had BufferedWriteHandler which doesn't seem to exist anymore in netty 4.0, am I overlooking some option I could set to "enable" buffering ?
Any example to achieve this ?
Thanks,