Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty: Boss Event Loop and Executor

In most of the examples that i have currently using Netty, I see that boss executor is a cached threadpool. From what i understand the event loop should only have a single thread, why exactly do we need a cached threadpool for the boss event loop?

like image 270
Desert Ice Avatar asked Jun 25 '26 16:06

Desert Ice


1 Answers

Netty has the concept of EventLoopGroup which aggregates several EventLoops. An EventLoop is single threaded, the EventLoopGroup is mostly not. Unless you're doing old blocking I/O.

Now, what concerns boss vs. workers. Assuming you're talking about a server implementation, the boss is now called parentGroup and the workers childGroup. The parent handles the I/O for the acceptor channel, i.e. the one which is bounded to the port where your server is accepting new requests. The children will the handling the I/O for the accepted connections. Indeed the acceptor channel should be handled by one and only thread and it will. If you pass a EventLoopGroup with several EventLoops to the parent, it will only use one of the EventLoops anyway, so a good practice is to reuse the same EventLoopGroup for both parent and child.

The ServerBootstrap actually provides two constructors, one that takes a single group and reuses it for both parent and client:

@Override
public ServerBootstrap group(EventLoopGroup group) {
    return group(group, group);
}

and one that allows you to explicitly pass the one for the parent and the one for the child:

public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
    super.group(parentGroup);
    if (childGroup == null) {
        throw new NullPointerException("childGroup");
    }
    if (this.childGroup != null) {
        throw new IllegalStateException("childGroup set already");
    }
    this.childGroup = childGroup;
    return this;
}

You can check it here: https://github.com/netty/netty/blob/master/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java

like image 78
Leo Gomes Avatar answered Jun 28 '26 04:06

Leo Gomes