Skip to content

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]:

  1. Channel Creation: A new NioServerSocketChannel is instantiated, which includes creating an associated ChannelPipeline.^[600-developer__big-data__netty__netty-02.md]
  2. Initialization: The init(channel) method configures options and attributes. Crucially, it adds a ServerBootstrapAcceptor to the pipeline, passing in the child group, child handler, and child options.^[600-developer__big-data__netty__netty-02.md]
  3. Registration: The channel is registered to an EventLoop.^[600-developer__big-data__netty__netty-02.md]
  4. Binding: AbstractUnsafe.doBind is called to execute the actual javaChannel.bind() logic and fire the ChannelActive event.^[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 NioEventLoop detects an OP_ACCEPT readiness event.^[600-developer__big-data__netty__netty-02.md]
  • Key Processing: The processSelectedKey method 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]:

  1. Channel Creation: A new NioSocketChannel is created with its own pipeline and unsafe object.^[600-developer__big-data__netty__netty-02.md]
  2. Event Loop Assignment: The new channel is registered with a selector from the childGroup (Worker EventLoop).^[600-developer__big-data__netty__netty-02.md]
  3. Readiness Registration: The channel is registered with an interest in OP_READ events, 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]