SO_TIMEOUT和CONNECT_TIMEOUT_MILLIS在netty的ChannelOption中意味着什么?

时间:2014-01-08 03:29:11

标签: java sockets timeout netty

SO_TIMEOUTCONNECT_TIMEOUT_MILLIS的含义是什么,它们之间有什么区别?

我发现:许多请求成本为3.004s,我的处理程序总是花费0.003s或0.004s而我将SO_TIMEOUT设置为3000,它们之间是否有关系?

我认为SO_TIMEOUT表示如果未在SO_TIMEOUT time中发送回复,请立即发送此回复。它是否正确?

2 个答案:

答案 0 :(得分:4)

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。

答案 1 :(得分:0)

SO_TIMEOUT由java.net.ServerSocket(OIO / BIO)设置 CONNECT_TIMEOUT_MILLIS由Netty(NIO)

设置

io.netty.channel.nio.AbstractNioChannel @覆盖         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);
                }

                // ...
    }