Skip to content

Commit bc212bb

Browse files
committed
Avoid http-level retries during upload requests
1 parent c159384 commit bc212bb

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

b2sdk/_internal/b2http.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
B2ConnectionError,
3737
B2Error,
3838
B2RequestTimeout,
39-
B2RequestTimeoutDuringUpload,
4039
BadDateFormat,
4140
BrokenPipe,
4241
ClockSkew,
@@ -254,7 +253,7 @@ def request(
254253
:param url: a URL to call
255254
:param headers: headers to send.
256255
:param data: raw bytes or a file-like object to send
257-
:param try_count: a number of retries
256+
:param try_count: a number of attempts
258257
:param params: a dict that will be converted to query string for GET requests or additional metadata for POST requests
259258
:param stream: if True, the response will be streamed
260259
:param _timeout: a timeout for the request in seconds if not default
@@ -355,14 +354,9 @@ def post_content_return_json(
355354
:param data: a file-like object to send
356355
:return: a dict that is the decoded JSON
357356
"""
358-
try:
359-
return self.request_content_return_json(
360-
'POST', url, headers, data, try_count, post_params, _timeout=_timeout
361-
)
362-
except B2RequestTimeout:
363-
# this forces a token refresh, which is necessary if request is still alive
364-
# on the server but has terminated for some reason on the client. See #79
365-
raise B2RequestTimeoutDuringUpload()
357+
return self.request_content_return_json(
358+
'POST', url, headers, data, try_count, post_params, _timeout=_timeout
359+
)
366360

367361
def post_json_return_json(self, url, headers, params, try_count: int = TRY_COUNT_OTHER):
368362
"""
@@ -423,7 +417,7 @@ def get_content(self, url, headers, try_count: int = TRY_COUNT_DOWNLOAD):
423417
424418
:param str url: a URL to call
425419
:param dict headers: headers to send
426-
:param int try_count: a number or retries
420+
:param int try_count: a number of attempts
427421
:return: Context manager that returns an object that supports iter_content()
428422
"""
429423
response = self.request(
@@ -456,7 +450,7 @@ def head_content(
456450
457451
:param str url: a URL to call
458452
:param dict headers: headers to send
459-
:param int try_count: a number or retries
453+
:param int try_count: a number of attempts
460454
:return: HTTP response
461455
"""
462456
return self.request('HEAD', url, headers=headers, try_count=try_count)
@@ -586,9 +580,13 @@ def _translate_and_retry(
586580
the exception is a retryable B2Error.
587581
588582
:param fcn: request function to call
589-
:param try_count: a number of retries
583+
:param try_count: a number of attempts
590584
:param post_params: request parameters
591585
"""
586+
587+
if try_count < 1:
588+
raise ValueError('try_count must be >= 1')
589+
592590
# For all but the last try, catch the exception.
593591
wait_time = 1.0
594592
max_wait_time = 64

b2sdk/_internal/raw_api.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def _get_json(self, base_url: str, endpoint: str, auth: str, **params) -> JSON:
564564

565565
def authorize_account(self, realm_url, application_key_id, application_key):
566566
auth = (
567-
f"Basic {base64.b64encode(f'{application_key_id}:{application_key}'.encode()).decode()}"
567+
f'Basic {base64.b64encode(f"{application_key_id}:{application_key}".encode()).decode()}'
568568
)
569569
return self._post_json(realm_url, 'b2_authorize_account', auth)
570570

@@ -1071,7 +1071,12 @@ def upload_file(
10711071
legal_hold=legal_hold,
10721072
custom_upload_timestamp=custom_upload_timestamp,
10731073
)
1074-
return self.b2_http.post_content_return_json(upload_url, headers, data_stream)
1074+
return self.b2_http.post_content_return_json(
1075+
upload_url,
1076+
headers,
1077+
data_stream,
1078+
try_count=1,
1079+
)
10751080

10761081
def upload_part(
10771082
self,
@@ -1097,7 +1102,12 @@ def upload_part(
10971102
)
10981103
server_side_encryption.add_to_upload_headers(headers)
10991104

1100-
return self.b2_http.post_content_return_json(upload_url, headers, data_stream)
1105+
return self.b2_http.post_content_return_json(
1106+
upload_url,
1107+
headers,
1108+
data_stream,
1109+
try_count=1,
1110+
)
11011111

11021112
def copy_file(
11031113
self,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid http-level retries during upload requests.

0 commit comments

Comments
 (0)