ByteBufferPool

This commit is contained in:
redkale
2024-09-07 23:01:26 +08:00
parent 9cb4889a9c
commit 50c10f31fa
5 changed files with 15 additions and 6 deletions

View File

@@ -52,6 +52,7 @@ import org.redkale.source.*;
import org.redkale.source.spi.SourceModuleEngine;
import org.redkale.util.AnyValue;
import org.redkale.util.AnyValueWriter;
import org.redkale.util.ByteBufferPool;
import org.redkale.util.Creator;
import org.redkale.util.Environment;
import org.redkale.util.Redkale;
@@ -810,11 +811,12 @@ public final class Application {
/** 设置WorkExecutor */
private void initWorkExecutor() {
int bufferCapacity = 32 * 1024;
int bufferPoolSize = Utility.cpus() * 8;
int bufferPoolSize = ByteBufferPool.DEFAULT_BUFFER_POOL_SIZE;
final AnyValue executorConf = config.getAnyValue("executor", true);
StringBuilder executorLog = new StringBuilder();
final int workThreads = Math.max(Utility.cpus(), executorConf.getIntValue("threads", Utility.cpus() * 10));
int confThreads = executorConf.getIntValue("threads", WorkThread.DEFAULT_WORK_POOL_SIZE);
final int workThreads = Math.max(Utility.cpus(), confThreads);
// 指定threads则不使用虚拟线程池
this.workExecutor = executorConf.getValue("threads") != null
? WorkThread.createExecutor(workThreads, "Redkale-WorkThread-%s")
@@ -832,7 +834,7 @@ public final class Application {
executorLog.append(", clientWorkExecutor: [workExecutor]");
} else {
// 给所有client给一个新的默认ExecutorService
int clientThreads = executorConf.getIntValue("clients", Utility.cpus() * 4);
int clientThreads = executorConf.getIntValue("clients", WorkThread.DEFAULT_WORK_POOL_SIZE);
clientWorkExecutor = WorkThread.createWorkExecutor(clientThreads, "Redkale-DefaultClient-WorkThread-%s");
executorLog.append(", threads=").append(clientThreads).append("}");
}

View File

@@ -144,7 +144,7 @@ public abstract class Server<
"UDP".equalsIgnoreCase(netprotocol) ? UDP_BUFFER_CAPACITY : 32 * 1024);
this.bufferCapacity =
"UDP".equalsIgnoreCase(netprotocol) ? bufCapacity : (bufCapacity < 1024 ? 1024 : bufCapacity);
this.bufferPoolSize = config.getIntValue("bufferPoolSize", Utility.cpus() * 8);
this.bufferPoolSize = config.getIntValue("bufferPoolSize", ByteBufferPool.DEFAULT_BUFFER_POOL_SIZE);
this.responsePoolSize = config.getIntValue("responsePoolSize", 1024);
this.name = config.getValue(
"name",

View File

@@ -19,7 +19,9 @@ import org.redkale.util.Utility;
* @author zhangjx
*/
public class WorkThread extends Thread implements Executor {
public static final int DEFAULT_WORK_POOL_SIZE = Utility.cpus() * 8;
protected final ExecutorService workExecutor;
// WorkThread下标从0开始

View File

@@ -18,6 +18,7 @@ import static org.redkale.boot.Application.RESNAME_APP_NODEID;
import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.mq.spi.MessageAgent;
import org.redkale.net.WorkThread;
import static org.redkale.net.http.WebSocket.RETCODE_GROUP_EMPTY;
import org.redkale.net.sncp.Sncp;
import org.redkale.service.*;
@@ -81,7 +82,7 @@ public abstract class WebSocketNode implements Service {
if (localEngine != null) {
int wsthreads = localEngine.wsThreads;
if (wsthreads == 0) {
wsthreads = Utility.cpus() * 8;
wsthreads = WorkThread.DEFAULT_WORK_POOL_SIZE;
}
if (wsthreads > 0) {
this.semaphore = new Semaphore(wsthreads);

View File

@@ -18,6 +18,10 @@ import java.util.concurrent.atomic.LongAdder;
*/
public class ByteBufferPool extends ObjectPool<ByteBuffer> {
public static final int DEFAULT_BUFFER_POOL_SIZE = Utility.cpus() * 4;
public static final int DEFAULT_BUFFER_CAPACITY = 16 * 1024;
private final int bufferCapacity;
protected ByteBufferPool(