Application.shareAsyncGroup
This commit is contained in:
@@ -180,6 +180,9 @@ public final class Application {
|
|||||||
// 给客户端使用,包含SNCP客户端、自定义数据库客户端连接池
|
// 给客户端使用,包含SNCP客户端、自定义数据库客户端连接池
|
||||||
private AsyncIOGroup clientAsyncGroup;
|
private AsyncIOGroup clientAsyncGroup;
|
||||||
|
|
||||||
|
// 给单一服务使用,有且仅有一个Server配置且buffer相关配置都是默认值的情况下才有值
|
||||||
|
private AsyncIOGroup shareAsyncGroup;
|
||||||
|
|
||||||
// 服务配置项
|
// 服务配置项
|
||||||
final AnyValue config;
|
final AnyValue config;
|
||||||
|
|
||||||
@@ -838,11 +841,33 @@ public final class Application {
|
|||||||
clientWorkExecutor = WorkThread.createWorkExecutor(clientThreads, "Redkale-DefaultClient-WorkThread-%s");
|
clientWorkExecutor = WorkThread.createWorkExecutor(clientThreads, "Redkale-DefaultClient-WorkThread-%s");
|
||||||
executorLog.append(", threads=").append(clientThreads).append("}");
|
executorLog.append(", threads=").append(clientThreads).append("}");
|
||||||
}
|
}
|
||||||
AsyncIOGroup ioGroup = new AsyncIOGroup(
|
if (config.getAnyValues("server").length == 1) {
|
||||||
"Redkale-DefaultClient-IOThread-%s", clientWorkExecutor, bufferCapacity, bufferPoolSize)
|
AnyValue servConf = config.getAnyValues("server")[0];
|
||||||
.skipClose(true);
|
if ("true".equals(servConf.getValue("shareio"))) {
|
||||||
this.clientAsyncGroup = ioGroup.start();
|
String servNetprotocol = Server.getConfNetprotocol(servConf);
|
||||||
|
int servBufferCapacity = Server.getConfBufferCapacity(servConf, servNetprotocol);
|
||||||
|
int serverBufferPoolSize = Server.getConfBufferPoolSize(servConf);
|
||||||
|
int defBufferCapacity = "UDP".equals(servNetprotocol)
|
||||||
|
? ByteBufferPool.DEFAULT_BUFFER_UDP_CAPACITY
|
||||||
|
: ByteBufferPool.DEFAULT_BUFFER_TCP_CAPACITY;
|
||||||
|
if (serverBufferPoolSize == ByteBufferPool.DEFAULT_BUFFER_POOL_SIZE
|
||||||
|
&& servBufferCapacity == defBufferCapacity) {
|
||||||
|
AsyncIOGroup ioGroup = new AsyncIOGroup(
|
||||||
|
"Redkale-DefaultServlet-IOThread-%s",
|
||||||
|
workExecutor, servBufferCapacity, serverBufferPoolSize)
|
||||||
|
.skipClose(true);
|
||||||
|
this.shareAsyncGroup = ioGroup.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.shareAsyncGroup != null) {
|
||||||
|
this.clientAsyncGroup = this.shareAsyncGroup;
|
||||||
|
} else {
|
||||||
|
AsyncIOGroup ioGroup = new AsyncIOGroup(
|
||||||
|
"Redkale-DefaultClient-IOThread-%s", clientWorkExecutor, bufferCapacity, bufferPoolSize)
|
||||||
|
.skipClose(true);
|
||||||
|
this.clientAsyncGroup = ioGroup.start();
|
||||||
|
}
|
||||||
if (executorLog.length() > 0) {
|
if (executorLog.length() > 0) {
|
||||||
logger.log(Level.INFO, executorLog.toString());
|
logger.log(Level.INFO, executorLog.toString());
|
||||||
}
|
}
|
||||||
@@ -1506,10 +1531,15 @@ public final class Application {
|
|||||||
stopServers();
|
stopServers();
|
||||||
this.propertiesModule.destroy();
|
this.propertiesModule.destroy();
|
||||||
this.workExecutor.shutdownNow();
|
this.workExecutor.shutdownNow();
|
||||||
if (this.clientAsyncGroup != null) {
|
if (this.shareAsyncGroup != null) {
|
||||||
|
long s = System.currentTimeMillis();
|
||||||
|
this.shareAsyncGroup.dispose();
|
||||||
|
logger.info("default.share.AsyncGroup destroy in " + (System.currentTimeMillis() - s) + " ms");
|
||||||
|
}
|
||||||
|
if (this.clientAsyncGroup != null && this.clientAsyncGroup != this.shareAsyncGroup) {
|
||||||
long s = System.currentTimeMillis();
|
long s = System.currentTimeMillis();
|
||||||
this.clientAsyncGroup.dispose();
|
this.clientAsyncGroup.dispose();
|
||||||
logger.info("AsyncGroup destroy in " + (System.currentTimeMillis() - s) + " ms");
|
logger.info("default.client.AsyncGroup destroy in " + (System.currentTimeMillis() - s) + " ms");
|
||||||
}
|
}
|
||||||
this.onAppPostShutdown();
|
this.onAppPostShutdown();
|
||||||
|
|
||||||
@@ -1726,6 +1756,10 @@ public final class Application {
|
|||||||
return clientAsyncGroup;
|
return clientAsyncGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsyncIOGroup getShareAsyncGroup() {
|
||||||
|
return shareAsyncGroup;
|
||||||
|
}
|
||||||
|
|
||||||
public ResourceFactory getResourceFactory() {
|
public ResourceFactory getResourceFactory() {
|
||||||
return resourceFactory;
|
return resourceFactory;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ import org.redkale.util.ByteBufferPool;
|
|||||||
*/
|
*/
|
||||||
public abstract class AsyncGroup {
|
public abstract class AsyncGroup {
|
||||||
|
|
||||||
public static final int UDP_BUFFER_CAPACITY = Integer.getInteger("redkale.udp.buffer.apacity", 1350);
|
|
||||||
|
|
||||||
public static AsyncGroup create(
|
public static AsyncGroup create(
|
||||||
String threadNameFormat,
|
String threadNameFormat,
|
||||||
final ExecutorService workExecutor,
|
final ExecutorService workExecutor,
|
||||||
|
|||||||
@@ -87,8 +87,6 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
LongAdder createResponseCounter = new LongAdder();
|
LongAdder createResponseCounter = new LongAdder();
|
||||||
LongAdder cycleResponseCounter = new LongAdder();
|
LongAdder cycleResponseCounter = new LongAdder();
|
||||||
|
|
||||||
ByteBufferPool safeBufferPool =
|
|
||||||
server.createSafeBufferPool(createBufferCounter, cycleBufferCounter, server.bufferPoolSize);
|
|
||||||
ObjectPool<Response> safeResponsePool =
|
ObjectPool<Response> safeResponsePool =
|
||||||
server.createSafeResponsePool(createResponseCounter, cycleResponseCounter, server.responsePoolSize);
|
server.createSafeResponsePool(createResponseCounter, cycleResponseCounter, server.responsePoolSize);
|
||||||
final int respPoolMax = server.getResponsePoolSize();
|
final int respPoolMax = server.getResponsePoolSize();
|
||||||
@@ -125,8 +123,14 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
? "Redkale-IOServletThread-%s"
|
? "Redkale-IOServletThread-%s"
|
||||||
: ("Redkale-" + server.name.replace("Server-", "") + "-IOServletThread-%s");
|
: ("Redkale-" + server.name.replace("Server-", "") + "-IOServletThread-%s");
|
||||||
if (this.ioGroup == null) {
|
if (this.ioGroup == null) {
|
||||||
this.ioGroup = new AsyncIOGroup(threadNameFormat, null, safeBufferPool);
|
if (application != null && application.getShareAsyncGroup() != null) {
|
||||||
this.ioGroup.start();
|
this.ioGroup = application.getShareAsyncGroup();
|
||||||
|
} else {
|
||||||
|
ByteBufferPool safeBufferPool =
|
||||||
|
server.createSafeBufferPool(createBufferCounter, cycleBufferCounter, server.bufferPoolSize);
|
||||||
|
this.ioGroup = new AsyncIOGroup(threadNameFormat, null, safeBufferPool);
|
||||||
|
this.ioGroup.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread acceptThread = new Thread() {
|
Thread acceptThread = new Thread() {
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import java.util.logging.*;
|
|||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import org.redkale.boot.Application;
|
import org.redkale.boot.Application;
|
||||||
import org.redkale.inject.ResourceFactory;
|
import org.redkale.inject.ResourceFactory;
|
||||||
import static org.redkale.net.AsyncGroup.UDP_BUFFER_CAPACITY;
|
|
||||||
import org.redkale.net.Filter;
|
import org.redkale.net.Filter;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -143,12 +142,10 @@ public abstract class Server<
|
|||||||
this.maxHeader = parseLenth(config.getValue("maxHeader"), 16 * 1024);
|
this.maxHeader = parseLenth(config.getValue("maxHeader"), 16 * 1024);
|
||||||
this.maxBody =
|
this.maxBody =
|
||||||
parseLenth(config.getValue("maxBody"), "UDP".equalsIgnoreCase(netprotocol) ? 16 * 1024 : 256 * 1024);
|
parseLenth(config.getValue("maxBody"), "UDP".equalsIgnoreCase(netprotocol) ? 16 * 1024 : 256 * 1024);
|
||||||
int bufCapacity = parseLenth(
|
int bufCapacity = getConfBufferCapacity(config, netprotocol);
|
||||||
config.getValue("bufferCapacity"),
|
|
||||||
"UDP".equalsIgnoreCase(netprotocol) ? UDP_BUFFER_CAPACITY : 32 * 1024);
|
|
||||||
this.bufferCapacity =
|
this.bufferCapacity =
|
||||||
"UDP".equalsIgnoreCase(netprotocol) ? bufCapacity : (bufCapacity < 1024 ? 1024 : bufCapacity);
|
"UDP".equalsIgnoreCase(netprotocol) ? bufCapacity : (bufCapacity < 1024 ? 1024 : bufCapacity);
|
||||||
this.bufferPoolSize = config.getIntValue("bufferPoolSize", ByteBufferPool.DEFAULT_BUFFER_POOL_SIZE);
|
this.bufferPoolSize = getConfBufferPoolSize(config);
|
||||||
this.responsePoolSize = config.getIntValue("responsePoolSize", 1024);
|
this.responsePoolSize = config.getIntValue("responsePoolSize", 1024);
|
||||||
this.name = config.getValue(
|
this.name = config.getValue(
|
||||||
"name",
|
"name",
|
||||||
@@ -194,6 +191,28 @@ public abstract class Server<
|
|||||||
this.context = this.createContext();
|
this.context = this.createContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int getConfBufferCapacity(AnyValue config, String netprotocol) {
|
||||||
|
return parseLenth(
|
||||||
|
config.getValue("bufferCapacity"),
|
||||||
|
"UDP".equalsIgnoreCase(netprotocol)
|
||||||
|
? ByteBufferPool.DEFAULT_BUFFER_UDP_CAPACITY
|
||||||
|
: ByteBufferPool.DEFAULT_BUFFER_TCP_CAPACITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getConfBufferPoolSize(AnyValue config) {
|
||||||
|
return config.getIntValue("bufferPoolSize", ByteBufferPool.DEFAULT_BUFFER_POOL_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConfNetprotocol(AnyValue config) {
|
||||||
|
if (config != null) {
|
||||||
|
String protocol = config.getValue("protocol", "").toUpperCase();
|
||||||
|
if ("UDP".equals(protocol) || protocol.endsWith(".UDP")) {
|
||||||
|
return "UDP";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "TCP";
|
||||||
|
}
|
||||||
|
|
||||||
protected static int parseLenth(String value, int defValue) {
|
protected static int parseLenth(String value, int defValue) {
|
||||||
return (int) parseLenth(value, defValue + 0L);
|
return (int) parseLenth(value, defValue + 0L);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,18 +34,7 @@ public class SncpServer extends Server<Uint128, SncpContext, SncpRequest, SncpRe
|
|||||||
|
|
||||||
public SncpServer(
|
public SncpServer(
|
||||||
Application application, long serverStartTime, AnyValue serconf, ResourceFactory resourceFactory) {
|
Application application, long serverStartTime, AnyValue serconf, ResourceFactory resourceFactory) {
|
||||||
super(application, serverStartTime, netprotocol(serconf), resourceFactory, new SncpDispatcherServlet());
|
super(application, serverStartTime, getConfNetprotocol(serconf), resourceFactory, new SncpDispatcherServlet());
|
||||||
}
|
|
||||||
|
|
||||||
private static String netprotocol(AnyValue serconf) {
|
|
||||||
if (serconf == null) {
|
|
||||||
return "TCP";
|
|
||||||
}
|
|
||||||
String protocol = serconf.getValue("protocol", "").toUpperCase();
|
|
||||||
if (protocol.endsWith(".UDP")) {
|
|
||||||
return "UDP";
|
|
||||||
}
|
|
||||||
return "TCP";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -18,9 +18,13 @@ import java.util.concurrent.atomic.LongAdder;
|
|||||||
*/
|
*/
|
||||||
public class ByteBufferPool extends ObjectPool<ByteBuffer> {
|
public class ByteBufferPool extends ObjectPool<ByteBuffer> {
|
||||||
|
|
||||||
public static final int DEFAULT_BUFFER_POOL_SIZE = Utility.cpus() * 4;
|
public static final int DEFAULT_BUFFER_POOL_SIZE =
|
||||||
|
Integer.getInteger("redkale.bytebuffer.pool.size", Utility.cpus() * 4);
|
||||||
|
|
||||||
public static final int DEFAULT_BUFFER_CAPACITY = 16 * 1024;
|
public static final int DEFAULT_BUFFER_TCP_CAPACITY =
|
||||||
|
Integer.getInteger("redkale.bytebuffer.tcp.apacity", 32 * 1024);
|
||||||
|
|
||||||
|
public static final int DEFAULT_BUFFER_UDP_CAPACITY = Integer.getInteger("redkale.bytebuffer.udp.apacity", 1350);
|
||||||
|
|
||||||
private final int bufferCapacity;
|
private final int bufferCapacity;
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ public class SncpRequestParseTest {
|
|||||||
SncpContext.SncpContextConfig config = new SncpContext.SncpContextConfig();
|
SncpContext.SncpContextConfig config = new SncpContext.SncpContextConfig();
|
||||||
config.logger = Logger.getLogger(SncpRequestParseTest.class.getSimpleName());
|
config.logger = Logger.getLogger(SncpRequestParseTest.class.getSimpleName());
|
||||||
config.serverAddress = sncpAddress;
|
config.serverAddress = sncpAddress;
|
||||||
|
config.maxHeader = 16 * 1024;
|
||||||
config.maxBody = 1024 * 1024 * 1024;
|
config.maxBody = 1024 * 1024 * 1024;
|
||||||
SncpContext context = new SncpContext(config);
|
SncpContext context = new SncpContext(config);
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class SncpTest {
|
|||||||
|
|
||||||
private static final String protocol = "SNCP.TCP"; // TCP UDP
|
private static final String protocol = "SNCP.TCP"; // TCP UDP
|
||||||
|
|
||||||
private static final int clientCapacity = protocol.endsWith(".UDP") ? AsyncGroup.UDP_BUFFER_CAPACITY : 8192;
|
private static final int clientCapacity =
|
||||||
|
protocol.endsWith(".UDP") ? ByteBufferPool.DEFAULT_BUFFER_UDP_CAPACITY : 8192;
|
||||||
|
|
||||||
private static ResourceFactory factory;
|
private static ResourceFactory factory;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user