title: How to cancel write on timeout in Netty?
body: <p>I am using Netty 3.6.6.Final and trying to implement write timeout for my handler such that on timeout I need to write specific response. In addition I need to cancel another write  response which is currently executing in the pipeline (or will be executing).</p>
  
  <p>Here is my current pipeline:</p>
  
  <pre><code>bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() throws Exception {
           return Channels.pipeline(LOGGER,
              new HttpServerCodec(), 
              new MyHttpContentDecoder(), 
              new IdleStateHandler(timer, 0, 1000, 0, TimeUnit.MILLISECONDS),
              handler.get());
           }
  });
  </code></pre>
  
  <p>Handler extends <code>IdleStateAwareChannelHandler</code> and implementing <code>channelIdle</code> method where I check for write timeout:</p>
  
  <pre><code>if (e.getState() == IdleState.WRITER_IDLE) {
               e.getChannel().write(SOME_RESPONSE).addListener(new ChannelFutureListener() {          
           public void operationComplete(ChannelFuture future)  throws Exception {
              future.getChannel().close();                
          }});
  } 
  </code></pre>
  
  <p>The question is how do I cancel write which I have planned in <code>messageReceived</code> method in case no timeout occurs. Is there something customary in Netty to deal with such a problem?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Cancelling via <code>ChannelFuture</code> does not work. As far as I understand most of the time write will not be cancelled. During my tests it was all the time, i.e. <code>cancel()</code> always returned false. So I guess it is really hard to achieve it this way.</p>
  
  <p>In the end I have updated the code to the latest release - 4.0.9.Final (much nicer API). 
  And all of the sudden, I received responses as a result of the write timeout. That didn't work this way in 3.6.6.Final. </p>
  
  <p>In 4.0.9.Final the code for handling write timeout is a bit different but I always get a second write on timeout (if I comment <code>ctx.writeAndFlush</code> below, then I am getting write from <code>channelRead0</code>):</p>
  
  <pre><code>public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
           if (evt instanceof IdleStateEvent) {
               IdleStateEvent e = (IdleStateEvent) evt;
               if (e.state() == IdleState.WRITER_IDLE) {
  
                     //the following condition was always false 
                     //(channelFuture is a state  variable of my handler for previous write)
                     if (channelFuture != null &amp;&amp; channelFuture.isCancellable()) {
                         System.out.println("Cancel "+channelFuture.cancel(true));
                     }
  
                    ctx.writeAndFlush(SOME_RESPONSE);
  
               }
           }
  }
  </code></pre>
  
  <p>Don't know if it is the right way to "overwrite" first write attempt when timeout occurs, and would be glad if someone can explain why it works and what was changed in the latest release regarding this scenario.</p>