diff --git a/pom.xml b/pom.xml index 2e3f7ae..731de99 100644 --- a/pom.xml +++ b/pom.xml @@ -125,6 +125,11 @@ netty-codec 4.0.23.Final + + io.netty + netty-transport-native-epoll + 4.0.23.Final + com.googlecode.jmockit diff --git a/src/main/java/com/corundumstudio/socketio/Configuration.java b/src/main/java/com/corundumstudio/socketio/Configuration.java index 72c646b..6b8c33e 100644 --- a/src/main/java/com/corundumstudio/socketio/Configuration.java +++ b/src/main/java/com/corundumstudio/socketio/Configuration.java @@ -37,6 +37,7 @@ public class Configuration { private int bossThreads = 0; // 0 = current_processors_amount * 2 private int workerThreads = 0; // 0 = current_processors_amount * 2 + private boolean useLinuxNativeEpoll; private boolean allowCustomRequests = false; @@ -74,7 +75,7 @@ public class Configuration { private boolean addVersionHeader = true; private String origin; - + public Configuration() { } @@ -86,6 +87,7 @@ public class Configuration { Configuration(Configuration conf) { setBossThreads(conf.getBossThreads()); setWorkerThreads(conf.getWorkerThreads()); + setUseLinuxNativeEpoll(conf.isUseLinuxNativeEpoll()); setPingInterval(conf.getPingInterval()); setPingTimeout(conf.getPingTimeout()); @@ -116,7 +118,7 @@ public class Configuration { setAckMode(conf.getAckMode()); setMaxFramePayloadLength(conf.getMaxFramePayloadLength()); setUpgradeTimeout(conf.getUpgradeTimeout()); - + setAddVersionHeader(conf.isAddVersionHeader()); setOrigin(conf.getOrigin()); } @@ -433,7 +435,7 @@ public class Configuration { /** * Transport upgrade timeout in milliseconds - * + * * @param upgradeTimeout */ public void setUpgradeTimeout(int upgradeTimeout) { @@ -446,7 +448,7 @@ public class Configuration { /** * Adds Server header with lib version to http response. * Default is true - * + * * @param addVersionHeader */ public void setAddVersionHeader(boolean addVersionHeader) { @@ -458,7 +460,7 @@ public class Configuration { /** * Set Access-Control-Allow-Origin header value for http each - * response. + * response. * Default is null * * If value is null then request ORIGIN header value used. @@ -470,6 +472,13 @@ public class Configuration { } public String getOrigin() { return origin; - } - + } + + public boolean isUseLinuxNativeEpoll() { + return useLinuxNativeEpoll; + } + public void setUseLinuxNativeEpoll(boolean useLinuxNativeEpoll) { + this.useLinuxNativeEpoll = useLinuxNativeEpoll; + } + } diff --git a/src/main/java/com/corundumstudio/socketio/SocketIOServer.java b/src/main/java/com/corundumstudio/socketio/SocketIOServer.java index 5b6ad2f..c20e4ea 100644 --- a/src/main/java/com/corundumstudio/socketio/SocketIOServer.java +++ b/src/main/java/com/corundumstudio/socketio/SocketIOServer.java @@ -19,6 +19,8 @@ import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.FixedRecvByteBufAllocator; +import io.netty.channel.epoll.EpollEventLoopGroup; +import io.netty.channel.epoll.EpollServerSocketChannel; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.concurrent.Future; @@ -128,9 +130,15 @@ public class SocketIOServer implements ClientListeners { initGroups(); pipelineFactory.start(configCopy, namespacesHub); + + Class channelClass = NioServerSocketChannel.class; + if (configCopy.isUseLinuxNativeEpoll()) { + channelClass = EpollServerSocketChannel.class; + } + ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) + .channel(channelClass) .childHandler(pipelineFactory); applyConnectionOptions(b); @@ -169,8 +177,13 @@ public class SocketIOServer implements ClientListeners { } protected void initGroups() { - bossGroup = new NioEventLoopGroup(configCopy.getBossThreads()); - workerGroup = new NioEventLoopGroup(configCopy.getWorkerThreads()); + if (configCopy.isUseLinuxNativeEpoll()) { + bossGroup = new EpollEventLoopGroup(configCopy.getBossThreads()); + workerGroup = new EpollEventLoopGroup(configCopy.getWorkerThreads()); + } else { + bossGroup = new NioEventLoopGroup(configCopy.getBossThreads()); + workerGroup = new NioEventLoopGroup(configCopy.getWorkerThreads()); + } } /**