netty / netty-incubator-codec-quic

Apache License 2.0
303 stars 72 forks source link

About multiple threads binding to one port using SO_REUESPORT #640

Closed wyingda closed 10 months ago

wyingda commented 10 months ago

I want to use multiple threads to process the connections on the server side, so I try to use SO_REUSEPORT and bind multiple times like this:

ChannelHandler quicServerCodec = new QuicServerCodecBuilder()
                            .activeMigration(true)
                            .build();
EventLoopGroup group = new EpollEventLoopGroup();
Bootstrap bs = new Bootstrap();
bs.group(group)
        .channel(EpollDatagramChannel.class)
        .handler(quicServerCodec)
        .option(EpollChannelOption.SO_REUSEPORT, true);

try {
    for (int i = 0; i < NettyRuntime.availableProcessors()*2; i++) {
        bs.bind(port).await();
    }
} catch (InterruptedException e){

}
  1. It seems like multiple threads will use one QuicheQuicServerCodec. Is QuicheQuicServerCodec sharable and can be added to multiple channels ?
  2. If QuicheQuicServerCodec is sharable, will it cause thread-safe problems when processing the connectionIdToChannel map?
  3. If QuicheQuicServerCodec is not sharable, how to attach different QuicheQuicServerCodec to different EpollDatagramChannel(or thread)? And what happens when a message is received on the port? If I enable active migration, will it be routed to the correct EpollDatagramChannel and processed by the correct QuicheQuicServerCodec, which holds the quic connection information by connectionIdToChannel map?
normanmaurer commented 10 months ago

Hi there,

1) It's not sharable you will need to create a new instance for each Channel. You can do so by using a ChannelInitializer and create the codec in the initChannel method. 2) its not sharable 3) you will need to do this via something like ebpf if you want to support migrations