[FEATURE] Оптимизации Netty

This commit is contained in:
Gravit 2019-07-01 18:34:03 +07:00
parent b39c640cd6
commit 09afc9943d
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 57 additions and 13 deletions

View file

@ -301,7 +301,12 @@ public class NettyConfig {
public NettyProxyConfig proxy = new NettyProxyConfig();
}
public class NettySSLConfig {
public String protocol;
}
public class NettyPerformanceConfig {
public boolean usingEpoll;
public int bossThread;
public int workerThread;
}
@ -743,6 +748,7 @@ private void generateConfigIfNotExists(boolean testEnv) throws IOException {
newConfig.netty.fileServerEnabled = true;
newConfig.netty.binds = new NettyBindAddress[]{new NettyBindAddress("0.0.0.0", 9274)};
newConfig.netty.performance = new NettyPerformanceConfig();
newConfig.netty.performance.usingEpoll = JVMHelper.OS_TYPE == JVMHelper.OS.LINUX; //Only linux
newConfig.netty.performance.bossThread = 2;
newConfig.netty.performance.workerThread = 8;

View file

@ -8,9 +8,7 @@
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
@ -24,7 +22,7 @@
import pro.gravit.launchserver.socket.handlers.NettyIpForwardHandler;
import pro.gravit.launchserver.socket.handlers.WebSocketFrameHandler;
import pro.gravit.launchserver.socket.handlers.fileserver.FileServerHandler;
import pro.gravit.utils.helper.CommonHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
public class LauncherNettyServer implements AutoCloseable {
@ -36,16 +34,25 @@ public class LauncherNettyServer implements AutoCloseable {
public LauncherNettyServer(LaunchServer server) {
LaunchServer.NettyConfig config = server.config.netty;
bossGroup = new NioEventLoopGroup(config.performance.bossThread);
workerGroup = new NioEventLoopGroup(config.performance.workerThread);
NettyObjectFactory.setUsingEpoll(config.performance.usingEpoll);
if(config.performance.usingEpoll)
{
LogHelper.debug("Netty: Epoll enabled");
}
if(config.performance.usingEpoll && JVMHelper.OS_TYPE != JVMHelper.OS.LINUX)
{
LogHelper.error("netty,perfomance.usingEpoll work only Linux systems");
}
bossGroup = NettyObjectFactory.newEventLoopGroup(config.performance.bossThread);
workerGroup = NettyObjectFactory.newEventLoopGroup(config.performance.workerThread);
serverBootstrap = new ServerBootstrap();
service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), server);
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.channel(NettyObjectFactory.getServerSocketChannelClass())
.handler(new LoggingHandler(config.logLevel))
.childHandler(new ChannelInitializer<NioSocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(NioSocketChannel ch) {
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
NettyConnectContext context = new NettyConnectContext();
//p.addLast(new LoggingHandler(LogLevel.INFO));

View file

@ -0,0 +1,31 @@
package pro.gravit.launchserver.socket;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ServerChannel;
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;
public class NettyObjectFactory {
private static boolean epoll = false;
public static void setUsingEpoll(boolean value)
{
epoll = value;
}
public static EventLoopGroup newEventLoopGroup(int threads)
{
if(epoll)
return new EpollEventLoopGroup(threads);
else
return new NioEventLoopGroup(threads);
}
public static Class<? extends ServerChannel> getServerSocketChannelClass()
{
if(epoll)
return EpollServerSocketChannel.class;
else
return NioServerSocketChannel.class;
}
}

View file

@ -173,11 +173,11 @@ public static void registerResponses() {
}
public void sendObject(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)));
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)));
}
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
}
public void sendObjectAll(Object obj) {
@ -193,11 +193,11 @@ public void sendObjectAll(Object obj, Type type) {
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE);
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE);
}
public void sendEvent(EventResult obj) {