Netty is an asynchronous event-driven network application framework.
Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients.
It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
Spring WebFlux와 Netty?
Spring WebFlux는 다음 두 가지 타입의 서버를 지원
Tomcat 등 Servlet 3.1+ container
WebFlux 서버로 쓰려면 non-blocking I/O API를 지원해야 함.
Servlet 3.1, Tomcat 8.5 부터는 NIO API를 지원.
그러나 다른 Servlet API는 여전히 동기식, Blocking API라서 애매한 구석이 있음. (docs)
Netty 등 non-Servlet runtime
어느 쪽을 쓰든, 프로그래밍 모델은 고수준으로 추상화 되어 있음.
SpringBoot의 WebFlux starter는 Netty를 기본 서버로 사용하고 있음.
EventLoopGroup
NioEventLoopGroup is a multithreaded event loop that handles I/O operation.
server-side application을 구현한다면 두개의 NioEventLoopGroup 가 사용됨.
The first one, often called 'boss', accepts an incoming connection.
스레드 개수 1로 지정하면 하나만 뜬다. 포트가 여러개 열려도, 하나의 스레드가 여러개의 포트를 listen한다.
스레드 개수 2개이고 포트 2개이면 스레드 하나 당 포트 하나를 listen하게 되고.
The second one, often called 'worker', handles the traffic of the accepted connection once the boss accepts the connection and registers the accepted connection to the worker.
주어진 worker 스레드에 accept된 커넥션이 분배된다.
16개의 worker 스레드, 160개의 커넥션이라면 한 스레드 당 대략 10개의 커넥션을 맡아 처리하게 된다.
How many Threads are used and how they are mapped to the created Channels depends on the EventLoopGroup implementation and may be even configurable via a constructor.