This commit is contained in:
Redkale
2018-05-26 09:58:34 +08:00
parent cc864e3e69
commit 4a2ba4e914
5 changed files with 71 additions and 26 deletions

View File

@@ -55,6 +55,9 @@ public class Context {
//字符集 //字符集
protected final Charset charset; protected final Charset charset;
//最大连接数, 为0表示没限制
protected final int maxconns;
//请求内容的大小上限, 默认64K //请求内容的大小上限, 默认64K
protected final int maxbody; protected final int maxbody;
@@ -81,13 +84,13 @@ public class Context {
public Context(ContextConfig config) { public Context(ContextConfig config) {
this(config.serverStartTime, config.logger, config.executor, config.sslContext, 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.charset, config.address, config.resourceFactory, config.prepare,
config.aliveTimeoutSeconds, config.readTimeoutSeconds, config.writeTimeoutSeconds); config.aliveTimeoutSeconds, config.readTimeoutSeconds, config.writeTimeoutSeconds);
} }
public Context(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext, public Context(long serverStartTime, Logger logger, ThreadPoolExecutor executor, SSLContext sslContext,
int bufferCapacity, ObjectPool<ByteBuffer> bufferPool, ObjectPool<Response> responsePool, int bufferCapacity, ObjectPool<ByteBuffer> bufferPool, ObjectPool<Response> responsePool, final int maxconns,
final int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory, final int maxbody, Charset charset, InetSocketAddress address, ResourceFactory resourceFactory,
final PrepareServlet prepare, final int aliveTimeoutSeconds, final int readTimeoutSeconds, final int writeTimeoutSeconds) { final PrepareServlet prepare, final int aliveTimeoutSeconds, final int readTimeoutSeconds, final int writeTimeoutSeconds) {
this.serverStartTime = serverStartTime; this.serverStartTime = serverStartTime;
@@ -97,6 +100,7 @@ public class Context {
this.bufferCapacity = bufferCapacity; this.bufferCapacity = bufferCapacity;
this.bufferPool = bufferPool; this.bufferPool = bufferPool;
this.responsePool = responsePool; this.responsePool = responsePool;
this.maxconns = maxconns;
this.maxbody = maxbody; this.maxbody = maxbody;
this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset; this.charset = StandardCharsets.UTF_8.equals(charset) ? null : charset;
this.address = address; this.address = address;
@@ -117,6 +121,10 @@ public class Context {
return sslContext; return sslContext;
} }
public int getMaxconns() {
return maxconns;
}
public int getMaxbody() { public int getMaxbody() {
return maxbody; return maxbody;
} }
@@ -228,6 +236,9 @@ public class Context {
//请求内容的大小上限, 默认64K //请求内容的大小上限, 默认64K
public int maxbody; public int maxbody;
//最大连接数, 为0表示没限制
public int maxconns;
//keep alive IO读取的超时时间 //keep alive IO读取的超时时间
public int aliveTimeoutSeconds; public int aliveTimeoutSeconds;

View File

@@ -65,10 +65,6 @@ public abstract class ProtocolServer {
public abstract void accept() throws IOException; public abstract void accept() throws IOException;
public void setMaxconns(int maxconns) {
this.maxconns = maxconns;
}
public abstract void close() throws IOException; public abstract void close() throws IOException;
public long getCreateCount() { public long getCreateCount() {
@@ -108,6 +104,7 @@ public abstract class ProtocolServer {
public ProtocolBIOUDPServer(Context context) { public ProtocolBIOUDPServer(Context context) {
this.context = context; this.context = context;
this.maxconns = context.getMaxconns();
} }
@Override @Override
@@ -115,6 +112,22 @@ public abstract class ProtocolServer {
DatagramChannel ch = DatagramChannel.open(); DatagramChannel ch = DatagramChannel.open();
ch.configureBlocking(true); ch.configureBlocking(true);
this.serverChannel = ch; this.serverChannel = ch;
final Set<SocketOption<?>> 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 @Override
@@ -195,12 +208,30 @@ public abstract class ProtocolServer {
public ProtocolAIOTCPServer(Context context) { public ProtocolAIOTCPServer(Context context) {
this.context = context; this.context = context;
this.maxconns = context.getMaxconns();
} }
@Override @Override
public void open(AnyValue config) throws IOException { public void open(AnyValue config) throws IOException {
group = AsynchronousChannelGroup.withCachedThreadPool(context.executor, 1); group = AsynchronousChannelGroup.withCachedThreadPool(context.executor, 1);
this.serverChannel = AsynchronousServerSocketChannel.open(group); this.serverChannel = AsynchronousServerSocketChannel.open(group);
final Set<SocketOption<?>> 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 @Override
@@ -306,6 +337,7 @@ public abstract class ProtocolServer {
public ProtocolNIOTCPServer(Context context) { public ProtocolNIOTCPServer(Context context) {
this.context = context; this.context = context;
this.maxconns = context.getMaxconns();
} }
@Override @Override
@@ -316,6 +348,23 @@ public abstract class ProtocolServer {
ServerSocket socket = serverChannel.socket(); ServerSocket socket = serverChannel.socket();
socket.setReceiveBufferSize(16 * 1024); socket.setReceiveBufferSize(16 * 1024);
socket.setReuseAddress(true); socket.setReuseAddress(true);
final Set<SocketOption<?>> 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 @Override

View File

@@ -90,6 +90,9 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
//Response池大小 //Response池大小
protected int responsePoolSize; protected int responsePoolSize;
//最大连接数, 为0表示没限制
protected int maxconns;
//请求包大小的上限,单位:字节 //请求包大小的上限,单位:字节
protected int maxbody; protected int maxbody;
@@ -102,9 +105,6 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
//IO写入 的超时秒数小于1视为不设置 //IO写入 的超时秒数小于1视为不设置
protected int writeTimeoutSeconds; protected int writeTimeoutSeconds;
//最大连接数
protected int maxconns;
protected Server(long serverStartTime, String protocol, ResourceFactory resourceFactory, PrepareServlet<K, C, R, P, S> servlet) { protected Server(long serverStartTime, String protocol, ResourceFactory resourceFactory, PrepareServlet<K, C, R, P, S> servlet) {
this.serverStartTime = serverStartTime; this.serverStartTime = serverStartTime;
this.protocol = protocol; this.protocol = protocol;
@@ -276,24 +276,7 @@ public abstract class Server<K extends Serializable, C extends Context, R extend
this.prepare.init(this.context, config); this.prepare.init(this.context, config);
this.serverChannel = ProtocolServer.create(this.protocol, context); this.serverChannel = ProtocolServer.create(this.protocol, context);
this.serverChannel.open(config); this.serverChannel.open(config);
final Set<SocketOption<?>> 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.bind(address, backlog);
serverChannel.setMaxconns(this.maxconns);
serverChannel.accept(); serverChannel.accept();
final String threadName = "[" + Thread.currentThread().getName() + "] "; final String threadName = "[" + Thread.currentThread().getName() + "] ";
logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address logger.info(threadName + this.getClass().getSimpleName() + ("TCP".equalsIgnoreCase(protocol) ? "" : ("." + protocol)) + " listen: " + address

View File

@@ -443,6 +443,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
contextConfig.bufferCapacity = rcapacity; contextConfig.bufferCapacity = rcapacity;
contextConfig.bufferPool = bufferPool; contextConfig.bufferPool = bufferPool;
contextConfig.responsePool = responsePool; contextConfig.responsePool = responsePool;
contextConfig.maxconns = this.maxconns;
contextConfig.maxbody = this.maxbody; contextConfig.maxbody = this.maxbody;
contextConfig.charset = this.charset; contextConfig.charset = this.charset;
contextConfig.address = this.address; contextConfig.address = this.address;

View File

@@ -116,6 +116,7 @@ public class SncpServer extends Server<DLong, SncpContext, SncpRequest, SncpResp
contextConfig.bufferCapacity = rcapacity; contextConfig.bufferCapacity = rcapacity;
contextConfig.bufferPool = bufferPool; contextConfig.bufferPool = bufferPool;
contextConfig.responsePool = responsePool; contextConfig.responsePool = responsePool;
contextConfig.maxconns = this.maxconns;
contextConfig.maxbody = this.maxbody; contextConfig.maxbody = this.maxbody;
contextConfig.charset = this.charset; contextConfig.charset = this.charset;
contextConfig.address = this.address; contextConfig.address = this.address;