[FEATURE] Опции производительности Netty

This commit is contained in:
Gravit 2019-04-06 18:41:38 +07:00
parent 1d4d9ae170
commit bb611d2022
4 changed files with 107 additions and 50 deletions

View file

@ -76,11 +76,11 @@ public void reload() throws Exception {
} }
public static final class Config { public static final class Config {
public int port; public int legacyPort;
private String address; private String legacyAddress;
private String bindAddress; private String legacyBindAddress;
public String projectName; public String projectName;
@ -148,13 +148,13 @@ public AuthProviderPair getAuthProviderPair() {
public String startScript; public String startScript;
public String getAddress() { public String getLegacyAddress() {
return address; return legacyAddress;
} }
public String getBindAddress() { public String getLegacyBindAddress() {
return bindAddress; return legacyBindAddress;
} }
public void setProjectName(String projectName) { public void setProjectName(String projectName) {
@ -171,17 +171,17 @@ public void setEnv(LauncherConfig.LauncherEnvironment env) {
public SocketAddress getSocketAddress() { public SocketAddress getSocketAddress() {
return new InetSocketAddress(bindAddress, port); return new InetSocketAddress(legacyBindAddress, legacyPort);
} }
public void setAddress(String address) { public void setLegacyAddress(String legacyAddress) {
this.address = address; this.legacyAddress = legacyAddress;
} }
public void verify() { public void verify() {
VerifyHelper.verify(getAddress(), VerifyHelper.NOT_EMPTY, "LaunchServer address can't be empty"); VerifyHelper.verify(getLegacyAddress(), VerifyHelper.NOT_EMPTY, "LaunchServer address can't be empty");
if (auth == null || auth[0] == null) { if (auth == null || auth[0] == null) {
throw new NullPointerException("AuthHandler must not be null"); throw new NullPointerException("AuthHandler must not be null");
} }
@ -243,13 +243,28 @@ public static class ExeConf {
} }
public class NettyConfig { public class NettyConfig {
public String bindAddress;
public int port;
public boolean clientEnabled; public boolean clientEnabled;
public String launcherURL; public String launcherURL;
public String downloadURL; public String downloadURL;
public String launcherEXEURL; public String launcherEXEURL;
public String address; public String address;
public NettyPerformanceConfig performance;
public NettyBindAddress[] binds;
}
public class NettyPerformanceConfig
{
public int bossThread;
public int workerThread;
}
public class NettyBindAddress
{
public String address;
public int port;
public NettyBindAddress(String address, int port) {
this.address = address;
this.port = port;
}
} }
public class GuardLicenseConf { public class GuardLicenseConf {
@ -626,15 +641,21 @@ private void generateConfigIfNotExists() throws IOException {
, "std")}; , "std")};
newConfig.protectHandler = new NoProtectHandler(); newConfig.protectHandler = new NoProtectHandler();
newConfig.permissionsHandler = new JsonFilePermissionsHandler(); newConfig.permissionsHandler = new JsonFilePermissionsHandler();
newConfig.port = 7240; newConfig.legacyPort = 7240;
newConfig.bindAddress = "0.0.0.0"; newConfig.legacyBindAddress = "0.0.0.0";
newConfig.binaryName = "Launcher"; newConfig.binaryName = "Launcher";
newConfig.whitelistRejectString = "Вас нет в белом списке"; newConfig.whitelistRejectString = "Вас нет в белом списке";
newConfig.netty = new NettyConfig(); newConfig.netty = new NettyConfig();
newConfig.netty.address = "ws://localhost:9274/api"; newConfig.netty.address = "ws://localhost:9274/api";
newConfig.netty.downloadURL = "http://localhost:9274/%dirname%/";
newConfig.netty.launcherURL = "http://localhost:9274/Launcher.jar";
newConfig.netty.launcherEXEURL = "http://localhost:9274/Launcher.exe";
newConfig.netty.clientEnabled = false; newConfig.netty.clientEnabled = false;
newConfig.netty.port = 9274; newConfig.netty.binds = new NettyBindAddress[]{ new NettyBindAddress("0.0.0.0", 9274) };
newConfig.netty.performance = new NettyPerformanceConfig();
newConfig.netty.performance.bossThread = 2;
newConfig.netty.performance.workerThread = 8;
newConfig.threadCoreCount = 0; // on your own newConfig.threadCoreCount = 0; // on your own
newConfig.threadCount = JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 4 ? JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2 : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors(); newConfig.threadCount = JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 4 ? JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2 : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors();
@ -654,7 +675,7 @@ private void generateConfigIfNotExists() throws IOException {
// Set server address // Set server address
System.out.println("LaunchServer address: "); System.out.println("LaunchServer address: ");
newConfig.setAddress(commandHandler.readLine()); newConfig.setLegacyAddress(commandHandler.readLine());
System.out.println("LaunchServer projectName: "); System.out.println("LaunchServer projectName: ");
newConfig.setProjectName(commandHandler.readLine()); newConfig.setProjectName(commandHandler.readLine());

View file

@ -0,0 +1,60 @@
package ru.gravit.launchserver.websocket;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.websocket.fileserver.FileServerHandler;
import java.net.InetSocketAddress;
public class LauncherNettyServer implements AutoCloseable {
public final ServerBootstrap serverBootstrap;
public final EventLoopGroup bossGroup;
public final EventLoopGroup workerGroup;
private static final String WEBSOCKET_PATH = "/api";
public LauncherNettyServer() {
LaunchServer.NettyConfig config = LaunchServer.server.config.netty;
bossGroup = new NioEventLoopGroup(config.performance.bossThread);
workerGroup = new NioEventLoopGroup(config.performance.workerThread);
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
public void initChannel(NioSocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
//p.addLast(new LoggingHandler(LogLevel.INFO));
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true));
pipeline.addLast(new WebSocketFrameHandler());
}
});
}
public ChannelFuture bind(InetSocketAddress address)
{
return serverBootstrap.bind(address);
}
@Override
public void close() throws Exception {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}

View file

@ -48,14 +48,13 @@
@SuppressWarnings({"unused", "rawtypes"}) @SuppressWarnings({"unused", "rawtypes"})
public final class NettyServerSocketHandler implements Runnable, AutoCloseable { public final class NettyServerSocketHandler implements Runnable, AutoCloseable {
private static final String WEBSOCKET_PATH = "/api";
private static SSLServerSocketFactory ssf; private static SSLServerSocketFactory ssf;
private static final ThreadFactory THREAD_FACTORY = r -> CommonHelper.newThread("Network Thread", true, r);
public volatile boolean logConnections = Boolean.getBoolean("launcher.logConnections"); public volatile boolean logConnections = Boolean.getBoolean("launcher.logConnections");
public static LauncherNettyServer nettyServer;
private final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>(); private final AtomicReference<ServerSocket> serverSocket = new AtomicReference<>();
private final ExecutorService threadPool = Executors.newCachedThreadPool(THREAD_FACTORY);
// API // API
private final Map<String, Response.Factory> customResponses = new ConcurrentHashMap<>(2); private final Map<String, Response.Factory> customResponses = new ConcurrentHashMap<>(2);
@ -115,44 +114,21 @@ public void run() {
}*/ }*/
//System.setProperty( "javax.net.ssl.keyStore","keystore"); //System.setProperty( "javax.net.ssl.keyStore","keystore");
//System.setProperty( "javax.net.ssl.keyStorePassword","PSP1000"); //System.setProperty( "javax.net.ssl.keyStorePassword","PSP1000");
try { /*try {
Selector selector = Selector.open(); Selector selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open(); ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false); serverChannel.configureBlocking(false);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }*/
LogHelper.info("Starting server socket thread"); LogHelper.info("Starting server socket thread");
//SSLEngine engine = sc.createSSLEngine(); //SSLEngine engine = sc.createSSLEngine();
//engine.setUseClientMode(false); //engine.setUseClientMode(false);
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
WebSocketFrameHandler.server = LaunchServer.server; WebSocketFrameHandler.server = LaunchServer.server;
try { nettyServer = new LauncherNettyServer();
ServerBootstrap b = new ServerBootstrap(); for(LaunchServer.NettyBindAddress address : LaunchServer.server.config.netty.binds)
b.group(bossGroup, workerGroup) {
.channel(NioServerSocketChannel.class) nettyServer.bind(new InetSocketAddress(address.address, address.port));
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
public void initChannel(NioSocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
//p.addLast(new LoggingHandler(LogLevel.INFO));
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast(new WebSocketServerCompressionHandler());
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true));
pipeline.addLast(new WebSocketFrameHandler());
}
});
ChannelFuture f = b.bind(new InetSocketAddress(LaunchServer.server.config.netty.port)).sync(); //TEST ONLY!
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
} }
/* /*
try (SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket()) { try (SSLServerSocket serverSocket = (SSLServerSocket) ssf.createServerSocket()) {

@ -1 +1 @@
Subproject commit c8fd592617ed36823d971df38786df2da82e7325 Subproject commit 571a9bfe1b79548d1fb1617264ef33be976504b3