Class NettyTransport
Epoll gives better throughput and lower latency on Linux, and is required for
transparent-proxy SO_ORIGINAL_DST resolution (which needs
EpollSocketChannel children). The selection is determined once at
class-load time and cached; all methods are safe to call from any thread.
On non-Linux platforms (macOS, Windows) or when the opt-out flag
useNativeTransport=false is set, everything transparently falls back
to NIO with no behaviour change.
The class is placed in mockserver-core so both the server bootstrap
(mockserver-netty) and the outbound HTTP client
(NettyHttpClient in mockserver-core) can use it.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<? extends io.netty.channel.socket.DatagramChannel>datagramChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the datagram channel class that is compatible with the given group.static booleanReturnstrueif the epoll native library is available on this platform, independent of the configuration opt-out flag.static io.netty.channel.EventLoopGroupnewEventLoopGroup(int nThreads, ThreadFactory threadFactory, boolean useNativeTransport) Creates a newEventLoopGroupof the appropriate transport type.static Class<? extends io.netty.channel.ServerChannel>serverSocketChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the server socket channel class that is compatible with the given group.static Class<? extends io.netty.channel.Channel>socketChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the client socket channel class that is compatible with the given group.static booleanuseNativeTransport(boolean useNativeTransport) Returnstrueif the native epoll transport should be used, considering both platform availability and the user's opt-out configuration flag.
-
Method Details
-
useNativeTransport
public static boolean useNativeTransport(boolean useNativeTransport) Returnstrueif the native epoll transport should be used, considering both platform availability and the user's opt-out configuration flag.- Parameters:
useNativeTransport- the configuration flag (true= prefer native when available;false= force NIO)- Returns:
trueto use epoll,falseto use NIO
-
isEpollAvailable
public static boolean isEpollAvailable()Returnstrueif the epoll native library is available on this platform, independent of the configuration opt-out flag. -
newEventLoopGroup
public static io.netty.channel.EventLoopGroup newEventLoopGroup(int nThreads, ThreadFactory threadFactory, boolean useNativeTransport) Creates a newEventLoopGroupof the appropriate transport type. If epoll is selected (per the config flag and platform availability), returns anEpollEventLoopGroup; otherwise returns aNioEventLoopGroup.If epoll group creation fails at runtime (e.g. native library load race), falls back to NIO gracefully.
- Parameters:
nThreads- number of event loop threadsthreadFactory- thread factory for naming threadsuseNativeTransport- the configuration flag- Returns:
- an event loop group — pass it to
serverSocketChannelClassFor(io.netty.channel.EventLoopGroup),socketChannelClassFor(io.netty.channel.EventLoopGroup), ordatagramChannelClassFor(io.netty.channel.EventLoopGroup)to obtain a channel class that is guaranteed to be compatible
-
serverSocketChannelClassFor
public static Class<? extends io.netty.channel.ServerChannel> serverSocketChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the server socket channel class that is compatible with the given group.Unlike the removed flag-based methods, this derives the channel class from the actual
EventLoopGroupinstance — so the two can never desync even whennewEventLoopGroup(int, java.util.concurrent.ThreadFactory, boolean)fell back from epoll to NIO at runtime.- Parameters:
group- theEventLoopGroupthe bootstrap will use- Returns:
EpollServerSocketChannel.classwhen the group is anEpollEventLoopGroup,NioServerSocketChannel.classotherwise
-
socketChannelClassFor
public static Class<? extends io.netty.channel.Channel> socketChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the client socket channel class that is compatible with the given group.This guarantees group/channel compatibility regardless of how the group was created (config-driven, caller-supplied NIO, or epoll fallback to NIO).
- Parameters:
group- theEventLoopGroupthe bootstrap will use- Returns:
EpollSocketChannel.classwhen the group is anEpollEventLoopGroup,NioSocketChannel.classotherwise
-
datagramChannelClassFor
public static Class<? extends io.netty.channel.socket.DatagramChannel> datagramChannelClassFor(io.netty.channel.EventLoopGroup group) Returns the datagram channel class that is compatible with the given group.Used by the DNS mock server bootstrap to pick the correct datagram channel for whatever event loop group it is registered with.
- Parameters:
group- theEventLoopGroupthe bootstrap will use- Returns:
EpollDatagramChannel.classwhen the group is anEpollEventLoopGroup,NioDatagramChannel.classotherwise
-