title: How to resolve "Too many open files" problems with netty
body: <p><a href="https://i.stack.imgur.com/GDycx.jpg" rel="nofollow noreferrer">too many open files</a></p>
  
  <p>When I using netty based http server in our product env. I find after working for some days the server will encount a very serious error "Too many open files" and this cannot recover automaticly after shut down the incoming requests ,this exception  was thrown out without stopping,I must restart the server manually.</p>
  
  <p>I added a handler to the acceptor thead as below:</p>
  
  <pre><code>    serverBootstrap.channelFactory(new ChannelFactory&lt;ServerChannel&gt;() {
              @Override
              public ServerChannel newChannel() {
  
                  NioServerSocketChannel nioServerSocketChannel = new NioServerSocketChannel();
                  nioServerSocketChannel.pipeline().addLast(new ChannelHandler() {
                      @Override
                      public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
  
                          String name = Thread.currentThread().getName();
                          System.out.println("Thread " + name +" add handler! ");
  
                      }
  
                      @Override
                      public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  
                      }
  
                      @Override
                      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  
                          cause.printStackTrace();
                          LOG.error("Error occurs!",cause);
  
                      }
                  });
  
                  return nioServerSocketChannel;
              }
          });
  </code></pre>
  
  <p>I can catch this exception here but don't know how to process it </p>
  
  <p>if I can refuse this connection or given a friendly response?</p>
  
  <p>how to recover from this problem?</p>
  
  <p>many thanks!</p>