Author martin.panter
Recipients Yuri.Bochkarev, agriffis, alanjds, amak, cananian, demian.brecht, icordasc, jcea, jhylton, martin.panter, mhammond, orsenthil, r.david.murray, rbcollins
Date 2015-01-24.08:42:45
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1422088970.23.0.620705659157.issue3566@psf.upfronthosting.co.za>
In-reply-to
Content
Here is a patch, including tests and documentation. It ended up a bit more complicated than I anticipated, so I’m interested in hearing other ideas or options.

* Added http.client.ConnectionClosed exception
* HTTPConnection.close() is implicitly called for a persistent connection closure
* BadStatusLine or ConnectionError (rather than new exception) is still raised on first getresponse()
* request() raising a ConnectionError does not necessarily mean the server did not send a response, so ConnectionClosed is only raised by getresponse()
* ConnectionClosed wraps ECONNRESET from the first recv() of the status line, but not after part of the status line has already been received

With this I hope code for making idempotent requests on a persistent connection would look a bit like this:

def idempotent_request(connection)
    try:
        attempt_request(connection, ...)
        response = connection.get_response()
        if response.status == HTTPStatus.REQUEST_TIMEOUT:
            raise ConnectionClosed(response.reason)
    except ConnectionClosed:
        attempt_request(connection, ...)
        response = connection.get_response()
    return response

def attempt_request(connection):
    try:
        connection.request(...)
    except ConnectionError:
        pass  # Ignore and read server response
History
Date User Action Args
2015-01-24 08:42:51martin.pantersetrecipients: + martin.panter, jhylton, mhammond, jcea, orsenthil, amak, rbcollins, cananian, r.david.murray, alanjds, agriffis, icordasc, demian.brecht, Yuri.Bochkarev
2015-01-24 08:42:50martin.pantersetmessageid: <1422088970.23.0.620705659157.issue3566@psf.upfronthosting.co.za>
2015-01-24 08:42:50martin.panterlinkissue3566 messages
2015-01-24 08:42:49martin.pantercreate