Netty connection acceptance flow¶
The Netty connection acceptance flow describes the sequence of operations that occurs when a NioServerSocketChannel receives a new incoming connection request.^[600-developer__big-data__netty__netty-02.md]
Server Initialization¶
The flow begins with the server binding to a port. The AbstractBootstrap#doBind() method calls initAndRegister(), which performs the following actions^[600-developer__big-data__netty__netty-02.md]:
- Channel Creation: A new
NioServerSocketChannelis instantiated, which includes creating an associatedChannelPipeline.^[600-developer__big-data__netty__netty-02.md] - Initialization: The
init(channel)method configures options and attributes. Crucially, it adds aServerBootstrapAcceptorto the pipeline, passing in the child group, child handler, and child options.^[600-developer__big-data__netty__netty-02.md] - Registration: The channel is registered to an
EventLoop.^[600-developer__big-data__netty__netty-02.md] - Binding:
AbstractUnsafe.doBindis called to execute the actualjavaChannel.bind()logic and fire theChannelActiveevent.^[600-developer__big-data__netty__netty-02.md]
Event Loop and IO Processing¶
A NioEventLoop runs on a dedicated FastThreadLocalThread.^[600-developer__big-data__netty__netty-02.md] When the server starts, the I/O readiness state changes, and the EventLoop handles the selection process^[600-developer__big-data__netty__netty-02.md]:
- Accept Trigger: The
NioEventLoopdetects anOP_ACCEPTreadiness event.^[600-developer__big-data__netty__netty-02.md] - Key Processing: The
processSelectedKeymethod is invoked to handle the selected key.^[600-developer__big-data__netty__netty-02.md]
New Connection Establishment¶
Once a connection is accepted, Netty initializes the new client channel, a process largely governed by the ServerBootstrapAcceptor added during server initialization^[600-developer__big-data__netty__netty-02.md]:
- Channel Creation: A new
NioSocketChannelis created with its ownpipelineandunsafeobject.^[600-developer__big-data__netty__netty-02.md] - Event Loop Assignment: The new channel is registered with a selector from the
childGroup(WorkerEventLoop).^[600-developer__big-data__netty__netty-02.md] - Readiness Registration: The channel is registered with an interest in
OP_READevents, enabling it to receive incoming data from the client.^[600-developer__big-data__netty__netty-02.md]
Sources¶
^[600-developer__big-data__netty__netty-02.md]