Reputation: 141
What does SO_TIMEOUT
and CONNECT_TIMEOUT_MILLIS
mean and what is the difference between them?
I have found that: many request cost 3.004s and my handler always cost 0.003s or 0.004s and I set the SO_TIMEOUT
to 3000 , is there a relationship among them?
I think SO_TIMEOUT
means that when a response is not send in SO_TIMEOUT time
, send this response immediately. Is this correct?
Upvotes: 12
Views: 15527
Reputation: 236
CONNECT_TIMEOUT_MILLIS
means timeout value to setup a
connection, this timeout is supported by Netty.
SO_TIMEOUT
is option for socket, it will impact:
| ServerSocket.accept();
| SocketInputStream.read()
| DatagramSocket.receive()
For more details please check: http://docs.oracle.com/javase/1.5.0/docs/api/java/net/SocketOptions.html#SO_TIMEOUT
If you just want to control request timeout, you could try Netty's ReadTimeoutHandler
or IdleTimeoutHandler
.
Upvotes: 6
Reputation: 1
SO_TIMEOUT is set by java.net.ServerSocket (OIO/BIO) CONNECT_TIMEOUT_MILLIS is set by 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);
}
// ...
}
Upvotes: 0