Browse code

* S3/S3.py, S3/Exception.py: Re-issue failed requests in S3.send_request()

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/branches/0.9.8.x@249 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2008/11/05 14:50:28
Showing 3 changed files
... ...
@@ -1,5 +1,9 @@
1 1
 2008-11-05  Michal Ludvig  <michal@logix.cz>
2 2
 
3
+	* S3/S3.py, S3/Exception.py: Re-issue failed requests in S3.send_request()
4
+
5
+2008-11-05  Michal Ludvig  <michal@logix.cz>
6
+
3 7
 	* s3cmd: Don't leak open filehandles in sync. Thx Patrick Linskey for report.
4 8
 	* s3cmd: Re-raise the right exception.
5 9
 	* s3cmd, S3/S3.py, S3/Exceptions.py: Don't abort 'sync' or 'put' on files
... ...
@@ -48,6 +48,9 @@ class S3UploadError(S3Exception):
48 48
 class S3DownloadError(S3Exception):
49 49
 	pass
50 50
 
51
+class S3RequestError(S3Exception):
52
+	pass
53
+
51 54
 class InvalidFileError(S3Exception):
52 55
 	pass
53 56
 
... ...
@@ -302,19 +302,26 @@ class S3(object):
302 302
 		debug("CreateRequest: resource[uri]=" + resource['uri'])
303 303
 		return (method_string, resource, headers)
304 304
 	
305
-	def send_request(self, request, body = None):
305
+	def send_request(self, request, body = None, retries = 5):
306 306
 		method_string, resource, headers = request
307 307
 		info("Processing request, please wait...")
308
- 		conn = self.get_connection(resource['bucket'])
309
- 		conn.request(method_string, self.format_uri(resource), body, headers)
310
-		response = {}
311
-		http_response = conn.getresponse()
312
-		response["status"] = http_response.status
313
-		response["reason"] = http_response.reason
314
-		response["headers"] = convertTupleListToDict(http_response.getheaders())
315
-		response["data"] =  http_response.read()
316
-		debug("Response: " + str(response))
317
-		conn.close()
308
+		try:
309
+			conn = self.get_connection(resource['bucket'])
310
+			conn.request(method_string, self.format_uri(resource), body, headers)
311
+			response = {}
312
+			http_response = conn.getresponse()
313
+			response["status"] = http_response.status
314
+			response["reason"] = http_response.reason
315
+			response["headers"] = convertTupleListToDict(http_response.getheaders())
316
+			response["data"] =  http_response.read()
317
+			debug("Response: " + str(response))
318
+			conn.close()
319
+		except Exception:
320
+			if retries:
321
+				warning("Retrying failed request: %s" % resource['uri'])
322
+				return self.send_request(request, body, retries - 1)
323
+			else:
324
+				raise S3RequestError("Request failed for: %s" % resource['uri'])
318 325
 
319 326
 		if response["status"] == 307:
320 327
 			## RedirectPermanent
... ...
@@ -325,7 +332,11 @@ class S3(object):
325 325
 			return self.send_request(request, body)
326 326
 
327 327
 		if response["status"] < 200 or response["status"] > 299:
328
-			raise S3Error(response)
328
+			if retries:
329
+				warning("Retrying failed request: %s" % resource['uri'])
330
+				return self.send_request(request, body, retries - 1)
331
+			else:
332
+				raise S3Error(response)
329 333
 		return response
330 334
 
331 335
 	def send_file(self, request, file, throttle = 0, retries = 3):