title: Understanding netty channel buffers and watermarks
body: <p>I am trying to understand netty buffers and watermarks. </p>
  
  <p>As a test case, I have a netty server which writes to a client, the client is blocked (essentially has a sleep of 10 seconds between each read)</p>
  
  <ul>
  <li><p>Under normal I/O TCP sender would be throttled (sending is slowed down because of flow control) if the receiver is blocked, this is not the case here. The sender seems to keep writing and flushing data on each send. Where is this data being written? Is there going to be Flow control in the netty's flush() as well? 
  See: <a href="https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Flow_control" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Flow_control</a></p></li>
  <li><p>Is it is being written to an OS or TCP buffer, does netty channel have an internal buffer as well? If so how can I configure it?</p></li>
  <li><p>I track bytesBeforeUnwritable but they do not seem to be decreasing</p></li>
  <li>What is the default High and Low Watermark? I have not set anything in my application. Is there any way to use this instead?</li>
  </ul>
  
  <p>Code below:</p>
  
  <pre><code>@Override
      public void channelRead(final ChannelHandlerContext ctx, Object msg) {
  
          if (server2OutboundChannel.isActive()) {
              if(server2OutboundChannel.isWritable()) {
                  server2OutboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
                      @Override
                      public void operationComplete(ChannelFuture future) {
                          if (future.isSuccess()) {
                              // was able to flush out data, start to read the next chunk
                              //System.out.println(server2OutboundChannel.bytesBeforeUnwritable());
                              ctx.channel().read();
                          } else {
                              future.channel().close();
                          }
                      }
                  });
              }else{
                  System.out.println("Channel is no longer writeable");
                  System.out.println(server2OutboundChannel.bytesBeforeUnwritable());
                  System.out.println(server2OutboundChannel.bytesBeforeWritable());
              }
          }
      }
  </code></pre>
  
  <p>Detailed steps to re-create with end-to-end source code:</p>
  
  <ul>
  <li><p>Clone <a href="https://github.com/nipunarora/nettyDuplicator" rel="nofollow noreferrer">https://github.com/nipunarora/nettyDuplicator</a> .</p></li>
  <li><p>Specifically, the proxy code is here: </p></li>
  </ul>
  
  <p><code>https://github.com/nipunarora/nettyDuplicator/tree/master/src/main/java/org/columbia/parikshan/proxy</code></p>
  
  <ul>
  <li><p>Compile and Build:</p>
  
  <p><code>mvn package</code></p></li>
  <li><p>Start the server</p>
  
  <p><code>sh scripts/Server.sh 3380</code></p></li>
  <li><p>Start the netty proxy</p>
  
  <p><code>sh scripts/nettyProxy.sh -l 0.0.0.0:3379 -o 127.0.0.1:3380</code></p></li>
  <li><p>Start the client</p>
  
  <p><code>sh scripts/Client.sh 127.0.0.1 3379</code></p></li>
  <li><p>send "hello" as std input in client</p></li>
  <li><p>netty blocks sending after some time and the bytesTillUnwritable do not decrease.</p></li>
  </ul>