카테고리:

2 분 소요

Nagle 알고리즘?

https://syudal.github.io/post/Network-Nagle알고리즘과-TCPNODELAY/

Nagle 알고리즘 적용 해제하기

아래는 Netty EchoServer 예제에 TCP_NODELAY 옵션을 true로 적용하여 Nagle 알고리즘의 적용을 해제 하는 방법이다.

  • EchoServer.java
public final class EchoServer {

    static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));

    public static void main(String[] args) throws Exception {
        // Configure SSL.
        final SslContext sslCtx = ServerUtil.buildSslContext();

        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        final EchoServerHandler serverHandler = new EchoServerHandler();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .option(ChannelOption.SO_BACKLOG, 100)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childOption(ChannelOption.TCP_NODELAY, true)
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ChannelPipeline p = ch.pipeline();
                     if (sslCtx != null) {
                         p.addLast(sslCtx.newHandler(ch.alloc()));
                     }
                     //p.addLast(new LoggingHandler(LogLevel.INFO));
                     p.addLast(serverHandler);
                 }
             });

            // Start the server.
            ChannelFuture f = b.bind(PORT).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } finally {
            // Shut down all event loops to terminate all threads.
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
.childOption(ChannelOption.TCP_NODELAY, true)

참고

https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/echo/EchoServer.java
https://dlgodks.tistory.com/3

태그: ChannelOption, childOption, EchoServer, Nagle, netty, TCP_NODELAY

업데이트: