From b2b810ddea042dd0deeb5f71d64ab65ada790abd Mon Sep 17 00:00:00 2001 From: Zaxar163 <35835496+Zaxar163@users.noreply.github.com> Date: Sun, 11 Nov 2018 09:01:19 +0300 Subject: [PATCH] ThreadCount #49 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #48 выполнено --- .../java/ru/gravit/launchserver/LaunchServer.java | 11 +++++++++-- .../socket/NettyServerSocketHandler.java | 5 +++-- .../launchserver/socket/ServerSocketHandler.java | 13 +++++++++---- .../socket/websocket/WebSocketService.java | 6 +++--- .../websocket/json/auth/CheckServerResponse.java | 5 +---- .../request/websockets/ClientWebSocketService.java | 10 +++++----- .../java/ru/gravit/launcher/server/ServerAgent.java | 8 +------- .../ru/gravit/launcher/server/ServerWrapper.java | 1 + .../java/ru/gravit/launcher/hasher/HashedDir.java | 2 +- .../gravit/launcher/hasher/HashedEntryAdapter.java | 2 +- .../ru/gravit/launcher/ssl/LauncherSSLContext.java | 3 ++- .../main/java/ru/gravit/utils/helper/JVMHelper.java | 2 +- 12 files changed, 37 insertions(+), 31 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index ddc82673..ac995a57 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -66,7 +66,6 @@ public final class LaunchServer implements Runnable, AutoCloseable { public static final class Config extends ConfigObject { - public final int port; // Handlers & Providers @@ -80,7 +79,10 @@ public static final class Config extends ConfigObject { public final HWIDHandler hwidHandler; // Misc options - + public final int threadCount; + + public final int threadCoreCount; + public final ExeConf launch4j; public final SignConf sign; @@ -111,6 +113,11 @@ private Config(BlockConfigEntry block, Path coredir, LaunchServer server) { address = block.getEntry("address", StringConfigEntry.class); port = VerifyHelper.verifyInt(block.getEntryValue("port", IntegerConfigEntry.class), VerifyHelper.range(0, 65535), "Illegal LaunchServer port"); + threadCoreCount = block.hasEntry("threadCoreCacheSize") ? VerifyHelper.verifyInt(block.getEntryValue("threadCoreCacheSize", IntegerConfigEntry.class), + VerifyHelper.range(0, 100), "Illegal LaunchServer inital thread pool cache size") : 0; + int internalThreadCount = block.hasEntry("threadCacheSize") ? VerifyHelper.verifyInt(block.getEntryValue("threadCacheSize", IntegerConfigEntry.class), + VerifyHelper.range(2, 100), "Illegal LaunchServer thread pool cache size") : (JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() >= 4 ? JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors() / 2 : JVMHelper.OPERATING_SYSTEM_MXBEAN.getAvailableProcessors()); + threadCount = threadCoreCount > internalThreadCount ? threadCoreCount : internalThreadCount; authRateLimit = VerifyHelper.verifyInt(block.getEntryValue("authRateLimit", IntegerConfigEntry.class), VerifyHelper.range(0, 1000000), "Illegal authRateLimit"); authRateLimitMilis = VerifyHelper.verifyInt(block.getEntryValue("authRateLimitMilis", IntegerConfigEntry.class), diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java index 32e0d214..9d265cdf 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java @@ -44,9 +44,10 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +@SuppressWarnings({"unused", "rawtypes"}) 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"); @@ -55,7 +56,7 @@ public final class NettyServerSocketHandler implements Runnable, AutoCloseable { private final ExecutorService threadPool = Executors.newCachedThreadPool(THREAD_FACTORY); // API - private final Map customResponses = new ConcurrentHashMap<>(2); + private final Map customResponses = new ConcurrentHashMap<>(2); private final AtomicLong idCounter = new AtomicLong(0L); private Set sockets; private volatile Listener listener; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java index 7ff3825f..46b25989 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/ServerSocketHandler.java @@ -5,8 +5,10 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -35,7 +37,7 @@ public interface Listener { // Instance private final LaunchServer server; private final AtomicReference serverSocket = new AtomicReference<>(); - private final ExecutorService threadPool = Executors.newCachedThreadPool(THREAD_FACTORY); + private final ExecutorService threadPool; public final SessionManager sessionManager; private final AtomicLong idCounter = new AtomicLong(0L); @@ -43,13 +45,16 @@ public interface Listener { private volatile Listener listener; public ServerSocketHandler(LaunchServer server) { - this.server = server; - sessionManager = new SessionManager(); + this(server, new SessionManager()); GarbageManager.registerNeedGC(sessionManager); } public ServerSocketHandler(LaunchServer server, SessionManager sessionManager) { this.server = server; + threadPool = new ThreadPoolExecutor(server.config.threadCoreCount, Integer.MAX_VALUE, + server.config.threadCount, TimeUnit.SECONDS, + new SynchronousQueue(), + THREAD_FACTORY); this.sessionManager = sessionManager; } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java index b4cb6005..2eb12279 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketService.java @@ -23,6 +23,7 @@ import java.util.HashMap; +@SuppressWarnings({"unused", "rawtypes"}) public class WebSocketService { public final ChannelGroup channels; @@ -30,9 +31,8 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder this.channels = channels; this.server = server; this.gsonBuiler = gson; - this. - gsonBuiler.registerTypeAdapter(JsonResponseInterface.class, new JsonResponseAdapter(this)); - gsonBuiler.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); + this.gsonBuiler.registerTypeAdapter(JsonResponseInterface.class, new JsonResponseAdapter(this)); + this.gsonBuiler.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); this.gson = gsonBuiler.create(); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java index 32724321..1e740a7e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/auth/CheckServerResponse.java @@ -8,8 +8,6 @@ import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface; import ru.gravit.utils.helper.LogHelper; -import java.util.UUID; - public class CheckServerResponse implements JsonResponseInterface { public String serverID; public String username; @@ -21,9 +19,8 @@ public String getType() { @Override public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { - UUID uuid; try { - uuid = LaunchServer.server.config.authHandler[0].checkServer(username, serverID); + LaunchServer.server.config.authHandler[0].checkServer(username, serverID); } catch (AuthException e) { service.sendObject(ctx, new WebSocketService.ErrorResult(e.getMessage())); return; diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index 5844841a..5ce34a4e 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -13,8 +13,8 @@ public class ClientWebSocketService { public final GsonBuilder gsonBuilder; public final Gson gson; public final ClientJSONPoint point; - private HashMap requests; - private HashMap results; + private HashMap> requests; + private HashMap> results; public ClientWebSocketService(GsonBuilder gsonBuilder, ClientJSONPoint point) { requests = new HashMap<>(); @@ -32,11 +32,11 @@ public void processMessage(Reader reader) { result.process(); } - public Class getRequestClass(String key) { + public Class getRequestClass(String key) { return requests.get(key); } - public void registerRequest(String key, Class clazz) { + public void registerRequest(String key, Class clazz) { requests.put(key, clazz); } @@ -44,7 +44,7 @@ public void registerRequests() { } - public void registerResult(String key, Class clazz) { + public void registerResult(String key, Class clazz) { results.put(key, clazz); } diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerAgent.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerAgent.java index e669a637..b776f501 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerAgent.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerAgent.java @@ -14,12 +14,6 @@ public class ServerAgent { public static Instrumentation inst; public static final class StarterVisitor extends SimpleFileVisitor { - private Instrumentation inst; - - public StarterVisitor(Instrumentation inst) { - this.inst = inst; - } - @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toFile().getName().endsWith(".jar")) addJVMClassPath(new JarFile(file.toFile())); @@ -51,7 +45,7 @@ public static void premain(String agentArgument, Instrumentation instrumentation isAgentStarted = true; try { - Files.walkFileTree(Paths.get("libraries"), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor(inst)); + Files.walkFileTree(Paths.get("libraries"), Collections.singleton(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new StarterVisitor()); } catch (IOException e) { e.printStackTrace(System.err); } diff --git a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java index 2e414b80..9d13308c 100644 --- a/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java @@ -38,6 +38,7 @@ public static boolean auth(ServerWrapper wrapper) { try { LauncherConfig cfg = Launcher.getConfig(); Boolean auth = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), 0, config.title).request(); + if (auth == null) throw new Exception("Non auth!"); // security check ProfilesRequest.Result result = new ProfilesRequest(cfg).request(); for (SignedObjectHolder p : result.profiles) { LogHelper.debug("Get profile: %s", p.object.getTitle()); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java index 5fd96de5..5e4793b4 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedDir.java @@ -265,7 +265,7 @@ private HashedDir sideDiff(HashedDir other, FileNameMatcher matcher, Deque path, boolean mismatchList) { + public HashedDir sideCompare(HashedDir other, FileNameMatcher matcher, Deque path, boolean mismatchList) { HashedDir diff = new HashedDir(); for (Entry mapEntry : map.entrySet()) { String name = mapEntry.getKey(); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java index dc0f94ac..d31fce3a 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/hasher/HashedEntryAdapter.java @@ -15,7 +15,7 @@ public HashedEntryAdapter() { @Override public HashedEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); - Class cls = null; + Class cls = null; if (typename.equals("dir")) cls = HashedDir.class; if (typename.equals("file")) cls = HashedFile.class; diff --git a/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java b/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java index cdd03ba4..bdc41b46 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/ssl/LauncherSSLContext.java @@ -6,7 +6,8 @@ public class LauncherSSLContext { public SSLServerSocketFactory ssf; public SSLSocketFactory sf; - private SSLContext sc; + @SuppressWarnings("unused") + private SSLContext sc; public LauncherSSLContext(KeyStore ks, String keypassword) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException { TrustManager[] trustAllCerts = new TrustManager[]{ diff --git a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java index 2f0aaac3..678b0feb 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java +++ b/libLauncher/src/main/java/ru/gravit/utils/helper/JVMHelper.java @@ -115,7 +115,7 @@ public static URL[] getClassPathURL() { return list; } - public static void checkStackTrace(Class mainClass) { + public static void checkStackTrace(Class mainClass) { LogHelper.debug("Testing stacktrace"); Exception e = new Exception("Testing stacktrace"); StackTraceElement[] list = e.getStackTrace();