From 41345ec9f65da8650817b760c4d3cf939c6e56c4 Mon Sep 17 00:00:00 2001 From: kamhung <22250530@qq.com> Date: Thu, 26 Nov 2015 14:23:27 +0800 Subject: [PATCH] --- .../wentch/redkale/net/ProtocolServer.java | 13 +++++++++++++ src/com/wentch/redkale/net/Server.java | 19 ++++++++++--------- src/com/wentch/redkale/net/Transport.java | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/com/wentch/redkale/net/ProtocolServer.java b/src/com/wentch/redkale/net/ProtocolServer.java index 3ccd6575e..d39be0f61 100644 --- a/src/com/wentch/redkale/net/ProtocolServer.java +++ b/src/com/wentch/redkale/net/ProtocolServer.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.*; +import java.util.*; import java.util.concurrent.*; /** @@ -23,6 +24,8 @@ public abstract class ProtocolServer { public abstract void bind(SocketAddress local, int backlog) throws IOException; + public abstract Set> supportedOptions(); + public abstract void setOption(SocketOption name, T value) throws IOException; public abstract void accept(); @@ -67,6 +70,11 @@ public abstract class ProtocolServer { this.serverChannel.setOption(name, value); } + @Override + public Set> supportedOptions() { + return this.serverChannel.supportedOptions(); + } + @Override public void accept() { final DatagramChannel serchannel = this.serverChannel; @@ -139,6 +147,11 @@ public abstract class ProtocolServer { this.serverChannel.setOption(name, value); } + @Override + public Set> supportedOptions() { + return this.serverChannel.supportedOptions(); + } + @Override public void accept() { final AsynchronousServerSocketChannel serchannel = this.serverChannel; diff --git a/src/com/wentch/redkale/net/Server.java b/src/com/wentch/redkale/net/Server.java index 962844eb1..bd61cbb30 100644 --- a/src/com/wentch/redkale/net/Server.java +++ b/src/com/wentch/redkale/net/Server.java @@ -46,7 +46,7 @@ public abstract class Server { protected int backlog; - protected ProtocolServer transport; + protected ProtocolServer serverChannel; protected int capacity; @@ -78,7 +78,7 @@ public abstract class Server { this.config = config; this.address = new InetSocketAddress(config.getValue("host", "0.0.0.0"), config.getIntValue("port", 80)); this.charset = Charset.forName(config.getValue("charset", "UTF-8")); - this.backlog = config.getIntValue("backlog", 10240); + this.backlog = config.getIntValue("backlog", 8 * 1024); this.readTimeoutSecond = config.getIntValue("readTimeoutSecond", 0); this.writeTimeoutSecond = config.getIntValue("writeTimeoutSecond", 0); this.capacity = config.getIntValue("capacity", 8 * 1024); @@ -117,12 +117,13 @@ public abstract class Server { this.context = this.createContext(); this.prepare.init(this.context, config); if (this.watch != null) this.watch.inject(this.prepare); - this.transport = ProtocolServer.create(this.protocol, context); - this.transport.open(); - transport.setOption(StandardSocketOptions.SO_REUSEADDR, true); - transport.setOption(StandardSocketOptions.SO_RCVBUF, 8 * 1024); - transport.bind(address, backlog); - transport.accept(); + this.serverChannel = ProtocolServer.create(this.protocol, context); + this.serverChannel.open(); + if (this.serverChannel.supportedOptions().contains(StandardSocketOptions.TCP_NODELAY)) { + this.serverChannel.setOption(StandardSocketOptions.TCP_NODELAY, true); + } + serverChannel.bind(address, backlog); + serverChannel.accept(); final String threadName = "[" + Thread.currentThread().getName() + "] "; logger.info(threadName + this.getClass().getSimpleName() + "." + protocol + " listen: " + address + ", threads: " + threads + ", bufferCapacity: " + capacity + ", bufferPoolSize: " + bufferPoolSize + ", responsePoolSize: " + responsePoolSize @@ -135,7 +136,7 @@ public abstract class Server { long s = System.currentTimeMillis(); logger.info(this.getClass().getSimpleName() + "-" + this.protocol + " shutdowning"); try { - this.transport.close(); + this.serverChannel.close(); } catch (Exception e) { } logger.info(this.getClass().getSimpleName() + "-" + this.protocol + " shutdow prepare servlet"); diff --git a/src/com/wentch/redkale/net/Transport.java b/src/com/wentch/redkale/net/Transport.java index 096404713..d3a87fe01 100644 --- a/src/com/wentch/redkale/net/Transport.java +++ b/src/com/wentch/redkale/net/Transport.java @@ -26,6 +26,19 @@ public final class Transport { protected static final int MAX_POOL_LIMIT = Runtime.getRuntime().availableProcessors() * 16; + protected static final boolean supportTcpNoDelay; + + static { + boolean tcpNoDelay = false; + try { + AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(); + tcpNoDelay = channel.supportedOptions().contains(StandardSocketOptions.TCP_NODELAY); + channel.close(); + } catch (Exception e) { + } + supportTcpNoDelay = tcpNoDelay; + } + protected final String name; protected final int bufferPoolSize; @@ -156,7 +169,10 @@ public final class Transport { if (conn.isOpen()) return conn; } } - if (channel == null) channel = AsynchronousSocketChannel.open(group); + if (channel == null) { + channel = AsynchronousSocketChannel.open(group); + if (supportTcpNoDelay) channel.setOption(StandardSocketOptions.TCP_NODELAY, true); + } try { channel.connect(addr).get(2, TimeUnit.SECONDS); break; @@ -167,6 +183,7 @@ public final class Transport { } } else { channel = AsynchronousSocketChannel.open(group); + if (supportTcpNoDelay) channel.setOption(StandardSocketOptions.TCP_NODELAY, true); channel.connect(addr).get(2, TimeUnit.SECONDS); } if (channel == null) return null;