title: Netty concurrency and "Connection reset by peer"
body: <p>I've built the following simple server, and I'm stress testing it using <code>ab</code>.
  If I run <code>ab</code> making 3000 total request (300 concurrent) it works. If I run it again, it shows me: </p>
  
  <pre><code>apr_socket_connect(): Connection reset by peer (54) 
  </code></pre>
  
  <p>And If after this error I try to make a single request with curl without restarting the server, it works. If I run again <code>ab</code> it shows the same error.
  It seems that it can't handle too many concurrent connections. Below the code:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  
          ServerBootstrap bootstrap = new ServerBootstrap(
                  new NioServerSocketChannelFactory(
                          Executors.newCachedThreadPool(),
                          Executors.newCachedThreadPool()));
  
          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
              @Override
              public ChannelPipeline getPipeline() throws Exception {
                  return Channels.pipeline(new StringEncoder(), new MyServerHandler());
              }
          });
  
          bootstrap.bind(new InetSocketAddress(9090));
          System.out.println("Running");
  }
  </code></pre>
  
  <p>Here is the handler:</p>
  
  <pre><code>public class MyServerHandler extends SimpleChannelUpstreamHandler {
  
      private static AtomicLong request = new AtomicLong();
  
      @Override
      public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
              throws Exception {  
          ChannelFuture channelFuture = e.getChannel().write("This is request #" + request.incrementAndGet() + "\n");
          channelFuture.addListener(ChannelFutureListener.CLOSE);
      }
  
      @Override
      public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
              throws Exception {
          System.out.println(e.getCause());
          e.getChannel().close();
      }
  
  }
  </code></pre>
  
  <p>As you see it's very simple, it just shows the total number of requests handled.
  Any tips?</p>
  
  <p>Thanks</p>