1212import org .apache .logging .log4j .Logger ;
1313import org .logstash .netty .SslHandlerProvider ;
1414
15+
16+ import static org .logstash .beats .DaemonThreadFactory .daemonThreadFactory ;
17+
1518public class Server {
1619 private final static Logger logger = LogManager .getLogger (Server .class );
1720
1821 private final int port ;
1922 private final String host ;
20- private final int beatsHeandlerThreadCount ;
23+ private final int beatsHandlerThreadCount ;
24+ private final int maxPendingRequests ;
2125 private NioEventLoopGroup workGroup ;
2226 private IMessageListener messageListener = new MessageListener ();
2327 private SslHandlerProvider sslHandlerProvider ;
2428 private BeatsInitializer beatsInitializer ;
29+ private final int connectionBacklog = 128 ;
2530
2631 private final int clientInactivityTimeoutSeconds ;
2732
28- public Server (String host , int p , int clientInactivityTimeoutSeconds , int threadCount ) {
33+ public Server (String host , int p , int clientInactivityTimeoutSeconds , int threadCount , int maxPendingRequests ) {
2934 this .host = host ;
3035 port = p ;
3136 this .clientInactivityTimeoutSeconds = clientInactivityTimeoutSeconds ;
32- beatsHeandlerThreadCount = threadCount ;
37+ beatsHandlerThreadCount = threadCount ;
38+ this .maxPendingRequests = maxPendingRequests ;
3339 }
3440
3541 public void setSslHandlerProvider (SslHandlerProvider sslHandlerProvider ){
@@ -49,11 +55,12 @@ public Server listen() throws InterruptedException {
4955 try {
5056 logger .info ("Starting server on port: {}" , this .port );
5157
52- beatsInitializer = new BeatsInitializer (messageListener , clientInactivityTimeoutSeconds , beatsHeandlerThreadCount );
58+ beatsInitializer = new BeatsInitializer (messageListener , clientInactivityTimeoutSeconds , beatsHandlerThreadCount , maxPendingRequests );
5359
5460 ServerBootstrap server = new ServerBootstrap ();
5561 server .group (workGroup )
5662 .channel (NioServerSocketChannel .class )
63+ .option (ChannelOption .SO_BACKLOG , connectionBacklog )
5764 .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.
5865 .childHandler (beatsInitializer );
5966
@@ -108,12 +115,13 @@ private class BeatsInitializer extends ChannelInitializer<SocketChannel> {
108115 private final IMessageListener localMessageListener ;
109116 private final int localClientInactivityTimeoutSeconds ;
110117
111- BeatsInitializer (IMessageListener messageListener , int clientInactivityTimeoutSeconds , int beatsHandlerThread ) {
118+ BeatsInitializer (IMessageListener messageListener , int clientInactivityTimeoutSeconds , int beatsHandlerThread , int maxPendingRequests ) {
112119 // Keeps a local copy of Server settings, so they can't be modified once it starts listening
113120 this .localMessageListener = messageListener ;
114121 this .localClientInactivityTimeoutSeconds = clientInactivityTimeoutSeconds ;
115122 idleExecutorGroup = new DefaultEventExecutorGroup (DEFAULT_IDLESTATEHANDLER_THREAD );
116- beatsHandlerExecutorGroup = new DefaultEventExecutorGroup (beatsHandlerThread );
123+ beatsHandlerExecutorGroup = new DefaultEventExecutorGroup (beatsHandlerThread , daemonThreadFactory ("beats-input-handler-executor" ), maxPendingRequests , new CustomRejectedExecutionHandler ());
124+ //beatsHandlerExecutorGroup = new DefaultEventExecutorGroup(beatsHandlerThread);
117125 }
118126
119127 public void initChannel (SocketChannel socket ){
@@ -127,6 +135,7 @@ public void initChannel(SocketChannel socket){
127135 pipeline .addLast (BEATS_ACKER , new AckEncoder ());
128136 pipeline .addLast (CONNECTION_HANDLER , new ConnectionHandler ());
129137 pipeline .addLast (beatsHandlerExecutorGroup , new BeatsParser (), new BeatsHandler (localMessageListener ));
138+
130139 }
131140
132141
0 commit comments