Skip to content

Commit ea833ee

Browse files
Improved upload retry
- Improved upload retry functionality
1 parent faf5c9b commit ea833ee

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

sendsafely/utilities.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
import json
1616
import os
1717
import secrets
18+
import time
1819

1920
import requests
2021
from .pgpy import PGPMessage, PGPKey
2122
from .pgpy.constants import HashAlgorithm, SymmetricKeyAlgorithm, CompressionAlgorithm, KeyFlags
2223

24+
_S3_UPLOAD_MAX_RETRIES = 6
25+
_S3_UPLOAD_TIMEOUT_SECONDS = 120
26+
_S3_UPLOAD_RETRY_STATUS_CODES = [408, 429, 500, 502, 503, 504]
27+
2328

2429
def _encrypt_file_part(file, server_secret, client_secret, path=True):
2530
"""
@@ -193,9 +198,28 @@ def _upload_file_part_to_s3(encrypted_file_part, url):
193198
Content-Type must NOT be specified
194199
:param encrypted_file_part: Part of a file to upload to S3. Must not exceed 2621440 Bytes.
195200
:param url: The S3 URL we're uploading to
196-
:return: The JSON response from S3.
201+
:return: The response from S3 once the part has been accepted.
202+
:raises requests.exceptions.RequestException: If the part still cannot be uploaded after retries.
197203
"""
198-
return requests.put(url=url, data=encrypted_file_part)
204+
last_exception = None
205+
for retries in range(_S3_UPLOAD_MAX_RETRIES):
206+
try:
207+
response = requests.put(url=url, data=encrypted_file_part, timeout=_S3_UPLOAD_TIMEOUT_SECONDS)
208+
except requests.exceptions.RequestException as e:
209+
last_exception = e
210+
else:
211+
if response.status_code not in _S3_UPLOAD_RETRY_STATUS_CODES:
212+
response.raise_for_status()
213+
return response
214+
last_exception = requests.exceptions.HTTPError(
215+
"S3 returned retryable failure status %d" % response.status_code, response=response
216+
)
217+
218+
if retries == _S3_UPLOAD_MAX_RETRIES - 1:
219+
break
220+
time.sleep(2 ** retries)
221+
222+
raise last_exception
199223

200224

201225
def _calculate_package_checksum(package_code, keycode):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='sendsafely',
5-
version='1.0.9.6',
5+
version='1.0.10',
66
packages=['sendsafely', 'sendsafely/pgpy', 'sendsafely/pgpy/packet/subpackets'],
77
description='The SendSafely Client API allows programmatic access to SendSafely and provides a layer of abstraction from our REST API, which requires developers to perform several complex tasks in a correct manner.',
88
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)