SO_TIMEOUT
和CONNECT_TIMEOUT_MILLIS
是什么意思,它们之间有什么区别?
我发现:许多请求的成本为 3.004 秒,而我的处理程序总是花费 0.003 秒或 0.004 秒,我将SO_TIMEOUT
设置为 3000,它们之间有关系吗?
我认为SO_TIMEOUT
意味着当响应未发送SO_TIMEOUT time
时,请立即发送此响应。这是对的吗?
-
CONNECT_TIMEOUT_MILLIS
表示设置的超时值连接,此超时由 Netty 支持。 -
SO_TIMEOUT
是套接字的选项,它将影响:
|ServerSocket.accept();
|SocketInputStream.read()
|DatagramSocket.receive()
欲了解更多详情,请查看:http://docs.oracle.com/javase/1.5.0/docs/api/java/net/SocketOptions.html#SO_TIMEOUT
如果你只想控制请求超时,你可以试试Netty的ReadTimeoutHandler
或IdleTimeoutHandler
。
SO_TIMEOUT由java.net.ServerSocket (OIO/BIO)设置CONNECT_TIMEOUT_MILLIS由Netty(NIO)设定
io.netty.channel.nio.AbstractNioChannel
:
@Override
public void connect(
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
// ...
try {
// ...
if (doConnect(remoteAddress, localAddress)) {
fulfillConnectPromise(promise, wasActive);
} else {
// ...
int connectTimeoutMillis = config().getConnectTimeoutMillis();
if (connectTimeoutMillis > 0) {
connectTimeoutFuture = eventLoop().schedule(new Runnable() {
@Override
public void run() {
ChannelPromise connectPromise = AbstractNioChannel.this.connectPromise;
ConnectTimeoutException cause =
new ConnectTimeoutException("connection timed out: " + remoteAddress);
if (connectPromise != null && connectPromise.tryFailure(cause)) {
close(voidPromise());
}
}
}, connectTimeoutMillis, TimeUnit.MILLISECONDS);
}
// ...
}