|
@@ -28,6 +28,7 @@ import io.netty.channel.socket.ServerSocketChannel;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
+import io.netty.util.concurrent.DefaultThreadFactory;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.NetworkInterface;
|
|
|
import java.net.SocketException;
|
|
@@ -35,6 +36,7 @@ import java.util.Collections;
|
|
|
import java.util.Enumeration;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
+import java.util.concurrent.ThreadFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
@@ -43,10 +45,22 @@ import org.slf4j.LoggerFactory;
|
|
|
*/
|
|
|
public class NettyUtils {
|
|
|
|
|
|
+ public static final String THREAD_POOL_NAME_PREFIX = "zkNetty-";
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(NettyUtils.class);
|
|
|
-
|
|
|
private static final int DEFAULT_INET_ADDRESS_COUNT = 1;
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a ThreadFactory which generates daemon threads, and uses
|
|
|
+ * the passed class's name to generate the thread names.
|
|
|
+ *
|
|
|
+ * @param clazz Class name to use for generating thread names
|
|
|
+ * @return Netty DefaultThreadFactory configured to create daemon threads
|
|
|
+ */
|
|
|
+ private static ThreadFactory createThreadFactory(String clazz) {
|
|
|
+ final String poolName = THREAD_POOL_NAME_PREFIX + clazz;
|
|
|
+ return new DefaultThreadFactory(poolName, true);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* If {@link Epoll#isAvailable()} <code>== true</code>, returns a new
|
|
|
* {@link EpollEventLoopGroup}, otherwise returns a new
|
|
@@ -55,11 +69,7 @@ public class NettyUtils {
|
|
|
* @return a new {@link EventLoopGroup}.
|
|
|
*/
|
|
|
public static EventLoopGroup newNioOrEpollEventLoopGroup() {
|
|
|
- if (Epoll.isAvailable()) {
|
|
|
- return new EpollEventLoopGroup();
|
|
|
- } else {
|
|
|
- return new NioEventLoopGroup();
|
|
|
- }
|
|
|
+ return newNioOrEpollEventLoopGroup(0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -72,9 +82,13 @@ public class NettyUtils {
|
|
|
*/
|
|
|
public static EventLoopGroup newNioOrEpollEventLoopGroup(int nThreads) {
|
|
|
if (Epoll.isAvailable()) {
|
|
|
- return new EpollEventLoopGroup(nThreads);
|
|
|
+ final String clazz = EpollEventLoopGroup.class.getSimpleName();
|
|
|
+ final ThreadFactory factory = createThreadFactory(clazz);
|
|
|
+ return new EpollEventLoopGroup(nThreads, factory);
|
|
|
} else {
|
|
|
- return new NioEventLoopGroup(nThreads);
|
|
|
+ final String clazz = NioEventLoopGroup.class.getSimpleName();
|
|
|
+ final ThreadFactory factory = createThreadFactory(clazz);
|
|
|
+ return new NioEventLoopGroup(nThreads, factory);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -145,7 +159,7 @@ public class NettyUtils {
|
|
|
}
|
|
|
}
|
|
|
LOG.debug("Detected {} local network addresses: {}", validInetAddresses.size(), validInetAddresses);
|
|
|
- return validInetAddresses.size() > 0 ? validInetAddresses.size() : DEFAULT_INET_ADDRESS_COUNT;
|
|
|
+ return !validInetAddresses.isEmpty() ? validInetAddresses.size() : DEFAULT_INET_ADDRESS_COUNT;
|
|
|
} catch (SocketException ex) {
|
|
|
LOG.warn("Failed to list all network interfaces, assuming 1", ex);
|
|
|
return DEFAULT_INET_ADDRESS_COUNT;
|