-->

Tornado curl_httpclient 'reset' connection

2019-09-03 00:55发布

问题:

I have a tornado application that makes requests to an http server using curl_httpclient. Every once in a while, all calls to the server returns incorrect results. In other words, the http responses are still fine, but the contents returned by the server becomes incorrect.

I find I can get around this problem by terminating and restarting my process, which seems to mean that if I make a new TCP connection to the server and send new requests, things will be fine.

Now my problem is how to make HTTP requests over a new TCP connection. I tried to replace the http client with a new one like below, but it seems that a new TCP connection is not made and the old connection reused.

http_client = tornado.httpclient.AsyncHTTPClient()

How can I close my existing TCP connection and make a new connection to the server and continue the business?

回答1:

You can pass the force_instance=True keyword argument to the AsyncHTTPClient constructor to ensure that you get a new object instead of reusing an old one, but it's probably worth figuring out why you're getting "incorrect" results in the first place.

How are the results "incorrect"? Is it not a well-formed HTTP response? Is it a response for an earlier (or subsequent pipelined) request on the same connection, or something unrelated?

Make sure you're using an up-to-date version of libcurl - very old versions had a history of problems with the asynchronous interfaces, although I haven't seen any problems in a couple of years. Note that if you're on a slow-moving distribution like centos this may mean installing libcurl from source instead of getting it from your distribution. If you don't have a specific need for curl_httpclient you can also use tornado's simple_httpclient instead.