Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc6d398

Browse files
committed
Retry rate limits on chunk uploading
1 parent 35aadaa commit cc6d398

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

googleapiclient/http.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def next_chunk(self, num_retries=0):
639639
"""Get the next chunk of the download.
640640
641641
Args:
642-
num_retries: Integer, number of times to retry 500's with randomized
642+
num_retries: Integer, number of times to retry with randomized
643643
exponential backoff. If all retries fail, the raised HttpError
644644
represents the last request. If zero (default), we attempt the
645645
request only once.
@@ -782,7 +782,7 @@ def execute(self, http=None, num_retries=0):
782782
Args:
783783
http: httplib2.Http, an http object to be used in place of the
784784
one the HttpRequest request object was constructed with.
785-
num_retries: Integer, number of times to retry 500's with randomized
785+
num_retries: Integer, number of times to retry with randomized
786786
exponential backoff. If all retries fail, the raised HttpError
787787
represents the last request. If zero (default), we attempt the
788788
request only once.
@@ -870,7 +870,7 @@ def next_chunk(self, http=None, num_retries=0):
870870
Args:
871871
http: httplib2.Http, an http object to be used in place of the
872872
one the HttpRequest request object was constructed with.
873-
num_retries: Integer, number of times to retry 500's with randomized
873+
num_retries: Integer, number of times to retry with randomized
874874
exponential backoff. If all retries fail, the raised HttpError
875875
represents the last request. If zero (default), we attempt the
876876
request only once.
@@ -965,7 +965,7 @@ def next_chunk(self, http=None, num_retries=0):
965965
except:
966966
self._in_error_state = True
967967
raise
968-
if resp.status < 500:
968+
if not _should_retry_response(resp.status, content):
969969
break
970970

971971
return self._process_response(resp, content)

tests/test_http.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,16 @@ def test_media_io_base_next_chunk_retries(self):
341341
upload = MediaIoBaseUpload(
342342
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
343343

344-
# Simulate 5XXs for both the request that creates the resumable upload and
345-
# the upload itself.
344+
# Simulate errors for both the request that creates the resumable upload
345+
# and the upload itself.
346346
http = HttpMockSequence([
347347
({'status': '500'}, ''),
348348
({'status': '500'}, ''),
349349
({'status': '503'}, ''),
350350
({'status': '200', 'location': 'location'}, ''),
351-
({'status': '500'}, ''),
352-
({'status': '500'}, ''),
353-
({'status': '503'}, ''),
351+
({'status': '403'}, USER_RATE_LIMIT_EXCEEDED_RESPONSE),
352+
({'status': '403'}, RATE_LIMIT_EXCEEDED_RESPONSE),
353+
({'status': '429'}, ''),
354354
({'status': '200'}, '{}'),
355355
])
356356

@@ -372,6 +372,34 @@ def test_media_io_base_next_chunk_retries(self):
372372
request.execute(num_retries=3)
373373
self.assertEqual([20, 40, 80, 20, 40, 80], sleeptimes)
374374

375+
def test_media_io_base_next_chunk_no_retry_403_not_configured(self):
376+
fd = BytesIO(b"i am png")
377+
upload = MediaIoBaseUpload(
378+
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
379+
380+
http = HttpMockSequence([
381+
({'status': '403'}, NOT_CONFIGURED_RESPONSE),
382+
({'status': '200'}, '{}')
383+
])
384+
385+
model = JsonModel()
386+
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
387+
method = u'POST'
388+
request = HttpRequest(
389+
http,
390+
model.response,
391+
uri,
392+
method=method,
393+
headers={},
394+
resumable=upload)
395+
396+
request._rand = lambda: 1.0
397+
request._sleep = mock.MagicMock()
398+
399+
with self.assertRaises(HttpError):
400+
request.execute(num_retries=3)
401+
request._sleep.assert_not_called()
402+
375403

376404
class TestMediaIoBaseDownload(unittest.TestCase):
377405

0 commit comments

Comments
 (0)