Socket Connection timeout

Source: Internet
Author: User

The socket. Connect connection times out in two cases:

1. Due to network problems, the three-way handshake time of TCP/IP> timeout is set. This may happen when Weibo is accessed abroad and the network environment is very poor.
Solution: increase the value of the timeout parameter in the socket. Connect Method. For example, if the value is 50 s, the default value of Linux is 70 s. If the value exceeds 70 s, Linux uses 70 s.
However, when the value is increased, a timeout exception is reported if the value is less than 10 seconds.
Ping api.weibo.com from a foreign machine to find unreachable.
It indicates that the client cannot send Syn packets at the network layer under the transport layer, not to mention three handshakes. The client directly fails and throws a timeout exception.

Experience: the first step in connection timeout diagnosis should be to ping to confirm that there is no problem at the network layer.
Note: The client sets timeout, but does not return an exception until it times out. The client will throw a timeout exception as long as the connection fails.

2. if the time specified by timeout is sufficient, but the processing capability of the server is poor, for example, the buffer connection queue is small, and the processing capability of the application layer is not fast, the buffer connection is full, and reject new connections.
When the server rejects services because the connection queue is full, the client retries three times over TCP. The time of each operation doubles.
If the value of the three-time accumulation parameter <timeout can be connected, it is normal that the queue makes room for the current connection.
If the value of the <timeout parameter is accumulated three times and cannot be connected, the client immediately throws a timeout exception, and does not throw until the timeout expires.

 

Appendix: read/write timeout

1. read/write timeout

The read timeout setting is meaningful. When the server processing capability is poor, but the server will eventually respond, the client can wait for a longer response time. If it is too long, the client uses the bio method, and the thread will be suspended due to Io blocking. When the processing capability of the client is significantly faster than that of the server, there will be a lot of suspended threads.

Regardless of the client or server, the performance of the machine will be affected when many threads are blocked. I saw someone reading timed out on Weibo forum and set the sotimeout time to 100 s. This is very dangerous. Once Sina's server crashes, its own server will also crash due to a large thread backlog.

Because after a thread is suspended, the resources it has mastered will not be released, such as memory, until the blocking is completed. At the same time, the suspension of a large number of threads means that the system needs to perform a large amount of context recovery and scheduling.

Solution:

If the client uses NiO, if the server's response can mark the client's request, the thread can put the request into a blockqueue after the client's request, then, the future or wait/notify mechanism is used to call the request in the queue to process the response with the request flag, so as to implement asynchronous processing. A small number of threads can be used to serve a large number of requests.

Similarly, if the server can use NiO to process requests per Thread, rather than connecting to each thread, it can greatly reduce the waste of resources caused by thread suspension. Nio is suitable for scenarios with many connections and few requests. In addition, commet, also known as server pushing technology, features persistent connections. Avoid inefficient request polling on the client. It is mainly used in chat rooms and weibo. Because of the many connections, there may not be many requests, and NiO is also suitable for servers.

Note: When a read timeout exception occurs, you do not need to ping the remote machine because it is used to locate the connection timeout. If the ping fails, it must be conneciton timeout instead of read timeout. Read timeout exception does not cause connection interruption. This provides an opportunity for retry.

 

2. Write timeout is generally not like connection timeout and read timeout, which can be displayed on the client. TCP has the concept of write retransmission, And will retry within 8 Mb. Otherwise, the connection is directly disconnected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.