小智 6
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
如果你只是想控制请求超时,你可以尝试 NettyReadTimeoutHandler或IdleTimeoutHandler。
小智 0
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);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)