# Class NettyUtils

java.lang.Object
org.apache.zookeeper.common.NettyUtils

public class NettyUtils extends Object
Helper methods for netty code.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`THREAD_POOL_NAME_PREFIX`

## Constructor Summary

Constructors
Constructor
Description
`NettyUtils()`

## Method Summary

Modifier and Type
Method
Description
`static int`
`getClientReachableLocalInetAddressCount()`
Attempts to detect and return the number of local network addresses that could be used by a client to reach this server.
`static io.netty.channel.EventLoopGroup`
`newNioOrEpollEventLoopGroup()`
If `Epoll.isAvailable()` `== true`, returns a new `EpollEventLoopGroup`, otherwise returns a new `NioEventLoopGroup`.
`static io.netty.channel.EventLoopGroup`
`newNioOrEpollEventLoopGroup(int nThreads)`
If `Epoll.isAvailable()` `== true`, returns a new `EpollEventLoopGroup`, otherwise returns a new `NioEventLoopGroup`.
`static Class<? extends io.netty.channel.socket.ServerSocketChannel>`
`nioOrEpollServerSocketChannel()`
If `Epoll.isAvailable()` `== true`, returns `EpollServerSocketChannel`, otherwise returns `NioServerSocketChannel`.
`static Class<? extends io.netty.channel.socket.SocketChannel>`
`nioOrEpollSocketChannel()`
If `Epoll.isAvailable()` `== true`, returns `EpollSocketChannel`, otherwise returns `NioSocketChannel`.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### THREAD_POOL_NAME_PREFIX

public static final String THREAD_POOL_NAME_PREFIX

See Also:
  - Constant Field Values

## Constructor Details

### NettyUtils

public NettyUtils()

## Method Details

### newNioOrEpollEventLoopGroup

public static io.netty.channel.EventLoopGroup newNioOrEpollEventLoopGroup()
If `Epoll.isAvailable()` `== true`, returns a new `EpollEventLoopGroup`, otherwise returns a new `NioEventLoopGroup`. Creates the event loop group using the default number of threads.

Returns:
  a new `EventLoopGroup`.

### newNioOrEpollEventLoopGroup

public static io.netty.channel.EventLoopGroup newNioOrEpollEventLoopGroup(int nThreads)
If `Epoll.isAvailable()` `== true`, returns a new `EpollEventLoopGroup`, otherwise returns a new `NioEventLoopGroup`. Creates the event loop group using the specified number of threads instead of the default.

Parameters:
  `nThreads` - see `NioEventLoopGroup(int)`.
Returns:
  a new `EventLoopGroup`.

### nioOrEpollSocketChannel

public static Class<? extends io.netty.channel.socket.SocketChannel> nioOrEpollSocketChannel()
If `Epoll.isAvailable()` `== true`, returns `EpollSocketChannel`, otherwise returns `NioSocketChannel`.

Returns:
  a socket channel class.

### nioOrEpollServerSocketChannel

public static Class<? extends io.netty.channel.socket.ServerSocketChannel> nioOrEpollServerSocketChannel()
If `Epoll.isAvailable()` `== true`, returns `EpollServerSocketChannel`, otherwise returns `NioServerSocketChannel`.

Returns:
  a server socket channel class.

### getClientReachableLocalInetAddressCount

public static int getClientReachableLocalInetAddressCount()
Attempts to detect and return the number of local network addresses that could be used by a client to reach this server. This means we exclude the following address types:

- Multicast addresses. Zookeeper server sockets use TCP, thus cannot bind to a multicast address.
- Link-local addresses. Routers don't forward traffic sent to a link-local address, so any realistic server deployment would not have clients using these.
- Loopback addresses. These are typically only used for testing.

Any remaining addresses are counted, and the total count is returned. This number is used to configure the number of threads for the "boss" event loop group, to make sure we have enough threads for each address in case the server is configured to listen on all available addresses. If listing the network interfaces fails, this method will return 1.

Returns:
  the number of client-reachable local network addresses found, or 1 if listing the network interfaces fails.