retry requests after BadStatusLine
authorTom Clegg <tom@clinicalfuture.com>
Tue, 4 Jun 2013 17:34:16 +0000 (13:34 -0400)
committerTom Clegg <tom@clinicalfuture.com>
Tue, 4 Jun 2013 17:54:03 +0000 (13:54 -0400)
sdk/python/arvados.py

index 61511f5d793c1a814f0dff58ad34dc4ea0728517..9b145f3d9e9be7abe2a763689067533cbf426769 100644 (file)
@@ -16,10 +16,20 @@ from apiclient.discovery import build
 class CredentialsFromEnv:
     @staticmethod
     def http_request(self, uri, **kwargs):
+        from httplib import BadStatusLine
         if 'headers' not in kwargs:
             kwargs['headers'] = {}
         kwargs['headers']['Authorization'] = 'OAuth2 %s' % os.environ['ARVADOS_API_TOKEN']
-        return self.orig_http_request(uri, **kwargs)
+        try:
+            return self.orig_http_request(uri, **kwargs)
+        except BadStatusLine:
+            # This is how httplib tells us that it tried to reuse an
+            # existing connection but it was already closed by the
+            # server. In that case, yes, we would like to retry.
+            # Unfortunately, we are not absolutely certain that the
+            # previous call did not succeed, so this is slightly
+            # risky.
+            return self.orig_http_request(uri, **kwargs)
     def authorize(self, http):
         http.orig_http_request = http.request
         http.request = types.MethodType(self.http_request, http)