Skip to content

Commit da79473

Browse files
feat: optimize the thread pool size (#83)
* feat: optimize the thread pool size
1 parent 1674370 commit da79473

6 files changed

Lines changed: 34 additions & 9 deletions

File tree

trpc-core/src/main/java/com/tencent/trpc/core/common/Constants.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ public class Constants {
7474
*/
7575
public static final String DEFAULT_BOSS_THREADS = "1";
7676
/**
77-
* Default business thread count cpus * 2
77+
* Default business core thread count 50
7878
*/
79-
public static final int DEFAULT_BIZ_THREADS = CPUS * 2;
79+
public static final int DEFAULT_CORE_THREADS = 50;
80+
/**
81+
* Default business max thread count 200
82+
*/
83+
public static final int DEFAULT_MAX_THREADS = 200;
8084
/**
8185
* Default coroutine pool core count
8286
*/

trpc-core/src/main/java/com/tencent/trpc/core/worker/WorkerPoolManager.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ public class WorkerPoolManager {
6363

6464
static {
6565
DEF_PROVIDER_WORKER_POOL_CONFIG = newThreadWorkerPoolConfig(DEF_PROVIDER_WORKER_POOL_NAME,
66-
Constants.DEFAULT_BIZ_THREADS, Boolean.FALSE);
66+
Constants.DEFAULT_CORE_THREADS, Constants.DEFAULT_MAX_THREADS, Boolean.FALSE);
6767
DEF_CONSUMER_WORKER_POOL_CONFIG = newThreadWorkerPoolConfig(DEF_CONSUMER_WORKER_POOL_NAME,
68-
Constants.DEFAULT_BIZ_THREADS, Boolean.FALSE);
68+
Constants.DEFAULT_CORE_THREADS, Constants.DEFAULT_MAX_THREADS, Boolean.FALSE);
6969
DEF_NAMING_WORKER_POOL_CONFIG = newThreadWorkerPoolConfig(DEF_NAMING_WORKER_POOL_NAME,
70-
Constants.DEFAULT_BIZ_THREADS, Boolean.FALSE);
70+
Constants.DEFAULT_CORE_THREADS, Constants.DEFAULT_MAX_THREADS, Boolean.FALSE);
7171
shareScheduler = new ScheduledThreadPoolExecutor(Math.min(Constants.CPUS, 4),
7272
// Parameters can be considered for startup parameter configuration
7373
new NamedThreadFactory("trpc_share_scheduler"));
@@ -122,6 +122,11 @@ public static PluginConfig newThreadWorkerPoolConfig(String name, int thread, bo
122122
return ThreadWorkerPool.newThreadWorkerPoolConfig(name, thread, useFiber);
123123
}
124124

125+
public static PluginConfig newThreadWorkerPoolConfig(String name, int corePoolSize,
126+
int maxPoolSize, boolean useFiber) {
127+
return ThreadWorkerPool.newThreadWorkerPoolConfig(name, corePoolSize, maxPoolSize, useFiber);
128+
}
129+
125130
/**
126131
* Close shared thread pool, graceful shutdown.
127132
*/

trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,15 @@ public class ThreadWorkerPool extends AbstractWorkerPool
7474
private UncaughtExceptionHandler uncaughtExceptionHandler;
7575

7676
public static PluginConfig newThreadWorkerPoolConfig(String name, int corePoolSize, boolean useFiber) {
77+
return newThreadWorkerPoolConfig(name, corePoolSize, corePoolSize, useFiber);
78+
}
79+
80+
public static PluginConfig newThreadWorkerPoolConfig(String name,
81+
int corePoolSize, int maxPoolSize, boolean useFiber) {
7782
ThreadPoolConfig poolConfig = new ThreadPoolConfig();
7883
poolConfig.setUseFiber(useFiber);
7984
poolConfig.setCorePoolSize(corePoolSize);
80-
poolConfig.setMaximumPoolSize(corePoolSize);
85+
poolConfig.setMaximumPoolSize(maxPoolSize);
8186
poolConfig.setShareSchedule(Boolean.TRUE);
8287
return new PluginConfig(name, WorkerPool.class, ThreadWorkerPool.class, poolConfig.toMap());
8388
}

trpc-core/src/test/java/com/tencent/trpc/core/common/config/BackendConfigTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ public void testIp() {
239239
public void test() {
240240
ExtensionLoader
241241
.registerPlugin(new PluginConfig("attalog", Filter.class, RemoteLoggerTest.class));
242-
ExtensionLoader.registerPlugin(ThreadWorkerPool.newThreadWorkerPoolConfig("thread", 10, Boolean.FALSE));
242+
ExtensionLoader.registerPlugin(ThreadWorkerPool.newThreadWorkerPoolConfig("thread", 10,
243+
10, Boolean.FALSE));
243244
BackendConfig config = new BackendConfig();
244245
config.setCallee("trpc.calleeapp.calleeserver.calleeservice.calleemethod");
245246
config.setNamingUrl("ip://127.0.0.1:8888");

trpc-core/src/test/java/com/tencent/trpc/core/worker/WorkerPoolManagerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ public void testRefresh() {
100100
WorkerPoolManager.refresh(WorkerPoolManager.DEF_NAMING_WORKER_POOL_NAME, pluginConfig);
101101
WorkerPool workerPool = WorkerPoolManager.get(WorkerPoolManager.DEF_CONSUMER_WORKER_POOL_NAME);
102102
Assert.assertNotNull(workerPool);
103+
}
103104

105+
@Test
106+
public void testWorkPool() {
107+
this.testRegistDefaultPluginConfig();
108+
PluginConfig pluginConfig = WorkerPoolManager
109+
.newThreadWorkerPoolConfig(WorkerPoolManager.DEF_CONSUMER_WORKER_POOL_NAME,
110+
20,20, Boolean.FALSE);
111+
WorkerPoolManager.refresh(WorkerPoolManager.DEF_NAMING_WORKER_POOL_NAME, pluginConfig);
112+
WorkerPool workerPool = WorkerPoolManager.get(WorkerPoolManager.DEF_CONSUMER_WORKER_POOL_NAME);
113+
Assert.assertNotNull(workerPool);
104114
}
105115

106116
/**

trpc-transport/trpc-transport-http/src/main/java/com/tencent/trpc/transport/http/support/jetty/JettyHttpServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ private QueuedThreadPool getServerThreadPool(ProtocolConfig config) {
136136
int threads = config.getIoThreads();
137137
QueuedThreadPool threadPool = new QueuedThreadPool();
138138
threadPool.setDaemon(true);
139-
threadPool.setMaxThreads(Math.max(threads, 4));
140-
threadPool.setMinThreads(Math.max(threads, 4));
139+
threadPool.setMaxThreads(Math.max(threads, Constants.DEFAULT_MAX_THREADS));
140+
threadPool.setMinThreads(Math.max(threads, Constants.DEFAULT_CORE_THREADS));
141141
threadPool.setIdleTimeout(Math.max(config.getIdleTimeout(), Integer.parseInt(Constants.DEFAULT_IDLE_TIMEOUT)));
142142
return threadPool;
143143
}

0 commit comments

Comments
 (0)