1616import java .io .IOException ;
1717import java .security .NoSuchAlgorithmException ;
1818import java .security .cert .CertificateException ;
19+ import java .util .concurrent .ArrayBlockingQueue ;
20+ import java .util .concurrent .ThreadPoolExecutor ;
21+ import java .util .concurrent .TimeUnit ;
22+
23+ import static org .logstash .beats .DaemonThreadFactory .daemonThreadFactory ;
1924
2025public class Server {
2126 private final static Logger logger = LogManager .getLogger (Server .class );
2227
2328 private final int port ;
2429 private final String host ;
25- private final int beatsHeandlerThreadCount ;
30+ private final int beatsHandlerThreadCount ;
31+ private final int maxPendingRequests ;
2632 private NioEventLoopGroup workGroup ;
2733 private IMessageListener messageListener = new MessageListener ();
2834 private SslSimpleBuilder sslBuilder ;
2935 private BeatsInitializer beatsInitializer ;
36+ private final int connectionBacklog = 128 ;
3037
3138 private final int clientInactivityTimeoutSeconds ;
3239
33- public Server (String host , int p , int timeout , int threadCount ) {
40+ public Server (String host , int p , int timeout , int threadCount , int maxPendingRequests ) {
3441 this .host = host ;
3542 port = p ;
3643 clientInactivityTimeoutSeconds = timeout ;
37- beatsHeandlerThreadCount = threadCount ;
44+ beatsHandlerThreadCount = threadCount ;
45+ this .maxPendingRequests = maxPendingRequests ;
3846 }
3947
4048 public void enableSSL (SslSimpleBuilder builder ) {
@@ -54,11 +62,12 @@ public Server listen() throws InterruptedException {
5462 try {
5563 logger .info ("Starting server on port: {}" , this .port );
5664
57- beatsInitializer = new BeatsInitializer (isSslEnable (), messageListener , clientInactivityTimeoutSeconds , beatsHeandlerThreadCount );
65+ beatsInitializer = new BeatsInitializer (isSslEnable (), messageListener , clientInactivityTimeoutSeconds , beatsHandlerThreadCount , maxPendingRequests );
5866
5967 ServerBootstrap server = new ServerBootstrap ();
6068 server .group (workGroup )
6169 .channel (NioServerSocketChannel .class )
70+ .option (ChannelOption .SO_BACKLOG , connectionBacklog )
6271 .childOption (ChannelOption .SO_LINGER , 0 ) // Since the protocol doesn't support yet a remote close from the server and we don't want to have 'unclosed' socket lying around we have to use `SO_LINGER` to force the close of the socket.
6372 .childHandler (beatsInitializer );
6473
@@ -114,14 +123,14 @@ private class BeatsInitializer extends ChannelInitializer<SocketChannel> {
114123 private final int localClientInactivityTimeoutSeconds ;
115124 private final boolean localEnableSSL ;
116125
117- BeatsInitializer (Boolean enableSSL , IMessageListener messageListener , int clientInactivityTimeoutSeconds , int beatsHandlerThread ) {
126+ BeatsInitializer (Boolean enableSSL , IMessageListener messageListener , int clientInactivityTimeoutSeconds , int beatsHandlerThread , int maxPendingRequests ) {
118127 // Keeps a local copy of Server settings, so they can't be modified once it starts listening
119128 this .localEnableSSL = enableSSL ;
120129 this .localMessageListener = messageListener ;
121130 this .localClientInactivityTimeoutSeconds = clientInactivityTimeoutSeconds ;
122131 idleExecutorGroup = new DefaultEventExecutorGroup (DEFAULT_IDLESTATEHANDLER_THREAD );
123- beatsHandlerExecutorGroup = new DefaultEventExecutorGroup (beatsHandlerThread );
124-
132+ beatsHandlerExecutorGroup = new DefaultEventExecutorGroup (beatsHandlerThread , daemonThreadFactory ( "beats-input-handler-executor" ), maxPendingRequests , new CustomRejectedExecutionHandler () );
133+ //beatsHandlerExecutorGroup = new DefaultEventExecutorGroup(beatsHandlerThread);
125134 }
126135
127136 public void initChannel (SocketChannel socket ) throws IOException , NoSuchAlgorithmException , CertificateException {
@@ -136,6 +145,7 @@ public void initChannel(SocketChannel socket) throws IOException, NoSuchAlgorith
136145 pipeline .addLast (BEATS_ACKER , new AckEncoder ());
137146 pipeline .addLast (CONNECTION_HANDLER , new ConnectionHandler ());
138147 pipeline .addLast (beatsHandlerExecutorGroup , new BeatsParser (), new BeatsHandler (localMessageListener ));
148+
139149 }
140150
141151 @ Override
0 commit comments