Parcourir la source

Running CommunityServer packets in a thread pool (CommunityServer)

DrHouse il y a 15 ans
Parent
commit
c684987616

+ 6 - 2
L2_CommunityServer/java/com/l2jserver/communityserver/Config.java

@@ -74,7 +74,9 @@ public final class Config
     /** General settings */
     public static int     MIN_PLAYER_LVL_FOR_FORUM;
     public static int     MIN_CLAN_LVL_FOR_FORUM;
-    public static Long     MAIL_AUTO_DELETION_TIME;
+    public static long    MAIL_AUTO_DELETION_TIME;
+    
+    public static int	  GENERAL_THREAD_CORE_SIZE;
     
     public static final void load()
     {
@@ -94,7 +96,7 @@ public final class Config
             DATAPACK_ROOT						= new File(serverSettings.getProperty("DatapackRoot", ".")).getCanonicalFile();
 
             DATABASE_DRIVER						= serverSettings.getProperty("Driver", "com.mysql.jdbc.Driver");
-            DATABASE_URL						= serverSettings.getProperty("URL", "jdbc:mysql://localhost/l2jdb");
+            DATABASE_URL						= serverSettings.getProperty("URL", "jdbc:mysql://localhost/l2jcb");
             DATABASE_LOGIN						= serverSettings.getProperty("Login", "root");
             DATABASE_PASSWORD					= serverSettings.getProperty("Password", "");
             DATABASE_MAX_CONNECTIONS			= Integer.parseInt(serverSettings.getProperty("MaximumDbConnections", "10"));
@@ -107,6 +109,8 @@ public final class Config
             MIN_PLAYER_LVL_FOR_FORUM			= Integer.parseInt(generalSettings.getProperty("MinPlayerLvLForForum", "1"));
             MIN_CLAN_LVL_FOR_FORUM				= Integer.parseInt(generalSettings.getProperty("MinClanLvLForForum", "2"));
             MAIL_AUTO_DELETION_TIME				= Long.parseLong(generalSettings.getProperty("MailAutoDeletionTime", "90")) * 86400000;
+            
+            GENERAL_THREAD_CORE_SIZE			= Integer.parseInt(generalSettings.getProperty("ThreadPoolSize", "1"));
 		}
 		catch (Exception e)
 		{

+ 2 - 0
L2_CommunityServer/java/com/l2jserver/communityserver/L2CommunityServer.java

@@ -25,6 +25,7 @@ import java.util.logging.Logger;
 import com.l2jserver.communityserver.cache.HtmCache;
 import com.l2jserver.communityserver.network.GameServerListener;
 import com.l2jserver.communityserver.network.netcon.NetConnectionConfig;
+import com.l2jserver.communityserver.threading.ThreadPoolManager;
 import com.l2jserver.communityserver.Shutdown;
 
 public final class L2CommunityServer
@@ -84,6 +85,7 @@ public final class L2CommunityServer
 		
 		_shutdownHandler = Shutdown.getInstance();
 		Runtime.getRuntime().addShutdownHook(_shutdownHandler);
+		ThreadPoolManager.init();
 		
 		GameServerRegistrationTable.getInstance();
 		

+ 3 - 1
L2_CommunityServer/java/com/l2jserver/communityserver/network/GameServerThread.java

@@ -37,6 +37,7 @@ import com.l2jserver.communityserver.network.writepackets.AuthResponse;
 import com.l2jserver.communityserver.network.writepackets.CommunityServerFail;
 import com.l2jserver.communityserver.network.writepackets.InitCS;
 import com.l2jserver.communityserver.network.writepackets.RequestWorldInfo;
+import com.l2jserver.communityserver.threading.ThreadPoolManager;
 
 /**
  * @author Forsaiken
@@ -167,7 +168,8 @@ public class GameServerThread extends NetConnection
 				}
 				
 				if (packet != null)
-					new Thread(packet).start();
+					//new Thread(packet).start();
+					ThreadPoolManager.execute(packet);
 				else
 					throw new IOException("Invalid packet!");
 			}

+ 81 - 0
L2_CommunityServer/java/com/l2jserver/communityserver/threading/ThreadPoolManager.java

@@ -0,0 +1,81 @@
+/*
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ * 
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.communityserver.threading;
+
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.l2jserver.communityserver.Config;
+
+/**
+ * Simple wrapper class to run packets on a ThreadPoolExecutor.<br>
+ * PriorityThreadFactory has been imported from L2J Server, coded by Wooden
+ * 
+ * @author DrHouse - L2JServer Team
+ *
+ */
+public class ThreadPoolManager
+{
+	private static ThreadPoolExecutor _mainPool;
+	
+	
+	public static synchronized final boolean init()
+	{
+		if (_mainPool != null)
+			return false;
+		
+		_mainPool = new ThreadPoolExecutor(Config.GENERAL_THREAD_CORE_SIZE, Config.GENERAL_THREAD_CORE_SIZE + 2, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("CommunityServer Executor pool", Thread.NORM_PRIORITY));
+		return true;
+	}
+	
+	public final static void execute(Runnable task)
+	{
+		_mainPool.execute(task);
+	}
+	
+	private static class PriorityThreadFactory implements ThreadFactory
+	{
+		private int _prio;
+		private String _name;
+		private AtomicInteger _threadNumber = new AtomicInteger(1);
+		private ThreadGroup _group;
+		
+		public PriorityThreadFactory(String name, int prio)
+		{
+			_prio = prio;
+			_name = name;
+			_group = new ThreadGroup(_name);
+		}
+		
+		/* (non-Javadoc)
+		 * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
+		 */
+		public Thread newThread(Runnable r)
+		{
+			Thread t = new Thread(_group, r);
+			t.setName(_name + "-" + _threadNumber.getAndIncrement());
+			t.setPriority(_prio);
+			return t;
+		}
+		
+		public ThreadGroup getGroup()
+		{
+			return _group;
+		}
+	}
+}

+ 7 - 1
L2_CommunityServer/java/config/communityserver.properties

@@ -61,4 +61,10 @@ IPBannList =
 SendBufferSize = 8192
 # SendBufferSize: DO NOT TOUCH!
 # Default: 8192
-ReceiveBufferSize = 8192
+ReceiveBufferSize = 8192
+
+# ================================================================
+# Threading
+# ================================================================
+# Approximated number of working threads running simultaneously in the pool
+ThreadPoolSize = 1