Add listener to returned Future rather than passed in Promise

Motivation

It's cleaner to add listeners to returned Futures rather than provided Promises because the latter can have strange side effects in terms of listeners firing and called methods returning. Adding listeners preemtively may yield also to more OPS than necessary when there's an Exception in the to be called method.

Modifications

Add listener to returned ChannelFuture rather than given ChannelPromise

Result

Cleaner completion and exception handling
This commit is contained in:
Roger Kapsi 2017-06-15 11:44:28 -04:00 committed by Norman Maurer
parent a05955d337
commit 1ad4629da0

View File

@ -25,7 +25,6 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOutboundBuffer;
import io.netty.channel.ChannelPromise;
import io.netty.util.concurrent.EventExecutor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@ -300,9 +299,10 @@ public class IdleStateHandler extends ChannelDuplexHandler {
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
// Allow writing with void promise if handler is only configured for read timeout events.
if (writerIdleTimeNanos > 0 || allIdleTimeNanos > 0) {
promise.addListener(writeListener);
ctx.write(msg, promise).addListener(writeListener);
} else {
ctx.write(msg, promise);
}
ctx.write(msg, promise);
}
private void initialize(ChannelHandlerContext ctx) {