Skip to content

Commit

Permalink
🎨 optimize the struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 24, 2017
1 parent 0f0925f commit bdca868
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/blade/kit/BladeKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static boolean notIsWebHook(HttpMethod httpMethod) {
public static boolean epollIsAvailable() {
try {
Object obj = Class.forName("io.netty.channel.epoll.Epoll").getMethod("isAvailable").invoke(null);
return null != obj && Boolean.valueOf(obj.toString());
return null != obj && Boolean.valueOf(obj.toString()) && System.getProperty("os.name").toLowerCase().contains("linux");
} catch (Exception e) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/blade/mvc/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public interface Const {
String ENE_KEY_SSL_PRIVATE_KEY_PASS = "server.ssl.private-key-pass";
String ENV_KEY_NETTY_ACCECPT_THREAD_COUNT = "server.netty.accept-thread-count";
String ENV_KEY_NETTY_IO_THREAD_COUNT = "server.netty.io-thread-count";
String ENV_KEY_NETTY_SO_BACKLOG = "server.netty.so-backlog";

String ENV_KEY_BOOT_CONF = "boot_conf";

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/blade/server/netty/EpollKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* @author biezhi
* @date 2017/9/22
*/
public class EpollKit {
class EpollKit {

public static NettyServerGroup group(int threadCount, int workers) {
static NettyServerGroup group(int threadCount, int workers) {
EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(threadCount, new NamedThreadFactory("epoll-boss@"));
EpollEventLoopGroup workerGroup = new EpollEventLoopGroup(workers, new NamedThreadFactory("epoll-worker@"));
return NettyServerGroup.builder().boosGroup(bossGroup).workerGroup(workerGroup).socketChannel(EpollServerSocketChannel.class).build();
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/blade/server/netty/NettyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.util.ResourceLeakDetector;
import io.netty.util.concurrent.ThreadPerTaskExecutor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
Expand All @@ -58,9 +57,6 @@ public class NettyServer implements Server {
private Environment environment;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private int threadCount;
private int workers;
private int backlog;
private Channel channel;
private RouteBuilder routeBuilder;
private List<BeanProcessor> processors;
Expand Down Expand Up @@ -140,27 +136,30 @@ private void startServer(long startTime) throws Exception {
}

// Configure the server.
int backlog = environment.getInt(ENV_KEY_NETTY_SO_BACKLOG, 8192);

ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, backlog);
b.option(ChannelOption.SO_REUSEADDR, true);
b.childOption(ChannelOption.SO_REUSEADDR, true);

int acceptThreadCount = environment.getInt(ENV_KEY_NETTY_ACCECPT_THREAD_COUNT, 0);
int ioThreadCount = environment.getInt(ENV_KEY_NETTY_IO_THREAD_COUNT, 0);

// enable epoll
if (BladeKit.epollIsAvailable()) {
log.info("⬢ Use EpollEventLoopGroup");
b.option(EpollChannelOption.SO_REUSEPORT, true);

NettyServerGroup nettyServerGroup = EpollKit.group(threadCount, workers);
NettyServerGroup nettyServerGroup = EpollKit.group(acceptThreadCount, ioThreadCount);
this.bossGroup = nettyServerGroup.getBoosGroup();
this.workerGroup = nettyServerGroup.getWorkerGroup();
b.group(bossGroup, workerGroup).channel(nettyServerGroup.getSocketChannel());
} else {
log.info("⬢ Use NioEventLoopGroup");

new ThreadPerTaskExecutor(new NamedThreadFactory(""));

this.bossGroup = new NioEventLoopGroup(threadCount, new NamedThreadFactory("nio-boss@"));
this.workerGroup = new NioEventLoopGroup(workers, new NamedThreadFactory("nio-worker@"));
this.bossGroup = new NioEventLoopGroup(acceptThreadCount, new NamedThreadFactory("nio-boss@"));
this.workerGroup = new NioEventLoopGroup(ioThreadCount, new NamedThreadFactory("nio-worker@"));
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
}

Expand Down

0 comments on commit bdca868

Please sign in to comment.