@@ -26,6 +26,7 @@ public class NettyHttpServer implements Runnable, Closeable {
26
26
private final int port ;
27
27
private final int connectionBacklog = 128 ;
28
28
29
+ private final EventLoopGroup bossGroup ;
29
30
private final EventLoopGroup processorGroup ;
30
31
private final ThreadPoolExecutor executorGroup ;
31
32
private final HttpResponseStatus responseStatus ;
@@ -37,8 +38,14 @@ public NettyHttpServer(String host, int port, IMessageHandler messageHandler,
37
38
this .host = host ;
38
39
this .port = port ;
39
40
this .responseStatus = HttpResponseStatus .valueOf (responseCode );
41
+
42
+ // boss group is responsible for accepting incoming connections and sending to worker loop
43
+ // process group is channel handler, see the https://github.com/netty/netty/discussions/13305
44
+ // see the https://github.com/netty/netty/discussions/11808#discussioncomment-1610918 for why separation is good
45
+ bossGroup = new NioEventLoopGroup (1 , daemonThreadFactory ("http-input-connector" ));
40
46
processorGroup = new NioEventLoopGroup (threads , daemonThreadFactory ("http-input-processor" ));
41
47
48
+ // event handler group
42
49
executorGroup = new ThreadPoolExecutor (threads , threads , 0 , TimeUnit .MILLISECONDS ,
43
50
new ArrayBlockingQueue <>(maxPendingRequests ), daemonThreadFactory ("http-input-handler-executor" ),
44
51
new CustomRejectedExecutionHandler ());
@@ -51,7 +58,7 @@ public NettyHttpServer(String host, int port, IMessageHandler messageHandler,
51
58
}
52
59
53
60
serverBootstrap = new ServerBootstrap ()
54
- .group (processorGroup )
61
+ .group (bossGroup , processorGroup )
55
62
.channel (NioServerSocketChannel .class )
56
63
.option (ChannelOption .SO_BACKLOG , connectionBacklog )
57
64
.childOption (ChannelOption .SO_KEEPALIVE , true )
@@ -73,7 +80,9 @@ public void run() {
73
80
public void close () {
74
81
try {
75
82
// stop accepting new connections first
76
- processorGroup .shutdownGracefully (0 , 10 , TimeUnit .SECONDS ).sync ();
83
+ bossGroup .shutdownGracefully ().sync ();
84
+ // stop the worker group
85
+ processorGroup .shutdownGracefully ().sync ();
77
86
// then shutdown the message handler executor
78
87
executorGroup .shutdown ();
79
88
try {
0 commit comments