diff --git a/src/org/redkale/net/Context.java b/src/org/redkale/net/Context.java index 7178c4483..e6e329332 100644 --- a/src/org/redkale/net/Context.java +++ b/src/org/redkale/net/Context.java @@ -55,6 +55,9 @@ public class Context { //字符集 protected final Charset charset; + //最大连接数, 为0表示没限制 + protected final int maxconns; + //请求内容的大小上限, 默认64K protected final int maxbody; @@ -81,13 +84,13 @@ public class Context { public Context(ContextConfig config) { this(config.serverStartTime, config.logger, config.executor, config.sslContext, - config.bufferCapacity, config.bufferPool, config.responsePool, config.maxbody, + config.bufferCapacity, config.bufferPool, config.responsePool, config.maxconns, config.maxbody, config.charset, config.address, config.resourceFactory, config.prepare, config.aliveTimeoutSeconds, config.readTimeoutSeconds, config.writeTimeoutSeconds); } public Context(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext, - int bufferCapacity, ObjectPool bufferPool, ObjectPool responsePool, + int bufferCapacity, ObjectPool bufferPool, ObjectPool responsePool, final int maxconns, final int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory, final PrepareServlet prepare, final int aliveTimeoutSeconds, final int readTimeoutSeconds, final int writeTimeoutSeconds) { this.serverStartTime = serverStartTime; @@ -97,6 +100,7 @@ public class Context { this.bufferCapacity = bufferCapacity; this.bufferPool = bufferPool; this.responsePool = responsePool; + this.maxconns = maxconns; this.maxbody = maxbody; this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset; this.address = address; @@ -117,6 +121,10 @@ public class Context { return sslContext; } + public int getMaxconns() { + return maxconns; + } + public int getMaxbody() { return maxbody; } @@ -228,6 +236,9 @@ public class Context { //请求内容的大小上限, 默认64K public int maxbody; + //最大连接数, 为0表示没限制 + public int maxconns; + //keep alive IO读取的超时时间 public int aliveTimeoutSeconds; diff --git a/src/org/redkale/net/ProtocolServer.java b/src/org/redkale/net/ProtocolServer.java index 5e9f4f383..4178378e3 100644 --- a/src/org/redkale/net/ProtocolServer.java +++ b/src/org/redkale/net/ProtocolServer.java @@ -65,10 +65,6 @@ public abstract class ProtocolServer { public abstract void accept() throws IOException; - public void setMaxconns(int maxconns) { - this.maxconns = maxconns; - } - public abstract void close() throws IOException; public long getCreateCount() { @@ -108,6 +104,7 @@ public abstract class ProtocolServer { public ProtocolBIOUDPServer(Context context) { this.context = context; + this.maxconns = context.getMaxconns(); } @Override @@ -115,6 +112,22 @@ public abstract class ProtocolServer { DatagramChannel ch = DatagramChannel.open(); ch.configureBlocking(true); this.serverChannel = ch; + final Set> options = this.serverChannel.supportedOptions(); + if (options.contains(StandardSocketOptions.TCP_NODELAY)) { + this.serverChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); + } + if (options.contains(StandardSocketOptions.SO_KEEPALIVE)) { + this.serverChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); + } + if (options.contains(StandardSocketOptions.SO_REUSEADDR)) { + this.serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + } + if (options.contains(StandardSocketOptions.SO_RCVBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024); + } + if (options.contains(StandardSocketOptions.SO_SNDBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024); + } } @Override @@ -195,12 +208,30 @@ public abstract class ProtocolServer { public ProtocolAIOTCPServer(Context context) { this.context = context; + this.maxconns = context.getMaxconns(); } @Override public void open(AnyValue config) throws IOException { group = AsynchronousChannelGroup.withCachedThreadPool(context.executor, 1); this.serverChannel = AsynchronousServerSocketChannel.open(group); + + final Set> options = this.serverChannel.supportedOptions(); + if (options.contains(StandardSocketOptions.TCP_NODELAY)) { + this.serverChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); + } + if (options.contains(StandardSocketOptions.SO_KEEPALIVE)) { + this.serverChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); + } + if (options.contains(StandardSocketOptions.SO_REUSEADDR)) { + this.serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + } + if (options.contains(StandardSocketOptions.SO_RCVBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024); + } + if (options.contains(StandardSocketOptions.SO_SNDBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024); + } } @Override @@ -306,6 +337,7 @@ public abstract class ProtocolServer { public ProtocolNIOTCPServer(Context context) { this.context = context; + this.maxconns = context.getMaxconns(); } @Override @@ -316,6 +348,23 @@ public abstract class ProtocolServer { ServerSocket socket = serverChannel.socket(); socket.setReceiveBufferSize(16 * 1024); socket.setReuseAddress(true); + + final Set> options = this.serverChannel.supportedOptions(); + if (options.contains(StandardSocketOptions.TCP_NODELAY)) { + this.serverChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); + } + if (options.contains(StandardSocketOptions.SO_KEEPALIVE)) { + this.serverChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); + } + if (options.contains(StandardSocketOptions.SO_REUSEADDR)) { + this.serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + } + if (options.contains(StandardSocketOptions.SO_RCVBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024); + } + if (options.contains(StandardSocketOptions.SO_SNDBUF)) { + this.serverChannel.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024); + } } @Override diff --git a/src/org/redkale/net/Server.java b/src/org/redkale/net/Server.java index 3b08bd80a..5b44445e3 100644 --- a/src/org/redkale/net/Server.java +++ b/src/org/redkale/net/Server.java @@ -90,6 +90,9 @@ public abstract class Server servlet) { this.serverStartTime = serverStartTime; this.protocol = protocol; @@ -276,24 +276,7 @@ public abstract class Server> options = this.serverChannel.supportedOptions(); - if (options.contains(StandardSocketOptions.TCP_NODELAY)) { - this.serverChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); - } - if (options.contains(StandardSocketOptions.SO_KEEPALIVE)) { - this.serverChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); - } - if (options.contains(StandardSocketOptions.SO_REUSEADDR)) { - this.serverChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true); - } - if (options.contains(StandardSocketOptions.SO_RCVBUF)) { - this.serverChannel.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024); - } - if (options.contains(StandardSocketOptions.SO_SNDBUF)) { - this.serverChannel.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024); - } serverChannel.bind(address, backlog); - serverChannel.setMaxconns(this.maxconns); serverChannel.accept(); final String threadName = "[" + Thread.currentThread().getName() + "] "; logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address diff --git a/src/org/redkale/net/http/HttpServer.java b/src/org/redkale/net/http/HttpServer.java index fd3d43107..a05503ea7 100644 --- a/src/org/redkale/net/http/HttpServer.java +++ b/src/org/redkale/net/http/HttpServer.java @@ -443,6 +443,7 @@ public class HttpServer extends Server