HttpClient sockettimeout ConnectTimeout connectionrequesttimeout__http

Source: Internet
Author: User
Tags connection pooling
httpclient Timeout Time Description Connectionrequesttimeout

HttpClient uses connection pooling to manage connections, which is the timeout to get connections from the connection pool, and you can imagine the database connection pool connecttimeout

Connection establishment time, three handshake completion time sockettimeout

Maximum time interval between packets during data transfer

The following highlights the sockettimeout, such as the HTTP request as shown in the following figure

Although the message ("ABC") returns a total of 6 seconds, if the sockettimeout is set to 4 seconds, the actual program will not throw Java.net.SocketTimeoutException:Read timed out exception.

Because the value of the sockettimeout represents the three messages "a", "B", "C", each of the two adjacent packets must not exceed sockettimeout. Program Validation: Server End (PYTHON3): Returns a, B, C every 3 seconds

__author__ = ' Yanglikun ' from
flask import flask to
flask import Response Import time
app = Flask (__nam e__)

@app. Route ("/")
def hello ():
    def generate ():
        for row in ["A", "B", "C"]:
            time.sleep (3)
            Yield row;

    Return Response (Generate ());

if __name__ = = "__main__":
    app.run (host= ' 0.0.0.0 ', port=8897)
Java Client (httpclient 4.3.6)
        Closeablehttpclient client = Httpclientbuilder.create (). build ();
        Requestconfig config = Requestconfig.custom (). Setconnectionrequesttimeout (5)
                . Setconnecttimeout (5)
                . SetSocketTimeout (6000). Build ();
        HttpGet httpget = new HttpGet ("http://192.168.147.90:8897/");
        Httpget.setconfig (config);
        Long begin = System.currenttimemillis ();
        String respstr = null;
        try {
            Closeablehttpresponse resp = Client.execute (httpget);
            Respstr = entityutils.tostring (Resp.getentity ());
        } catch (IOException e) {
            e.printstacktrace ();
        }
        System.err.println ("Respstr:" + respstr);
        System.err.println ("End:" + (System.currenttimemillis ()-begin);
Client Execution Results

Although the setting Sockettimeout is 6000 (6 seconds), the program executes for 9 seconds and does not throw Java.net.SocketTimeoutException:Read timed out exception
Wireshark Grab Bag result

Might think that the actual message is not every 3 seconds after the return of a message, the following is through the Wireshark grab bag results
which
1: Returns the TCP message of letter A
2: Returns the TCP message of the letter B
3: Returns the TCP message of letter C
According to the time column, you can see that the interval between TCP data transmission is 3 seconds written in the Python program. BTW, you can review another TCP knowledge, each message has an ACK message, is the TCP client receipt message

Click to view larger image

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.