1+ import os
12import math
23import requests
34import threading
45import concurrent .futures
5- import os
6+
7+ from requests .adapters import HTTPAdapter
8+ from requests .packages .urllib3 .util .retry import Retry
9+
610
711thread_local = threading .local ()
812
@@ -11,6 +15,12 @@ def __init__(self, asset, file):
1115 self .asset = asset
1216 self .file = file
1317 self .chunk_size = None
18+ self .retry_strategy = Retry (
19+ total = 3 ,
20+ backoff_factor = 1 ,
21+ status_forcelist = [400 , 500 , 503 ],
22+ method_whitelist = ["PUT" ]
23+ )
1424
1525 def _calculate_chunks (self , total_size , chunk_count ):
1626 self .chunk_size = int (math .ceil (total_size / chunk_count ))
@@ -24,8 +34,12 @@ def _calculate_chunks(self, total_size, chunk_count):
2434 return chunk_offsets
2535
2636 def _get_session (self ):
27- if not hasattr (thread_local , "session" ):
28- thread_local .session = requests .Session ()
37+ if not hasattr (thread_local , "session" ):
38+ adapter = HTTPAdapter (max_retries = self .retry_strategy )
39+ http = requests .Session ()
40+ http .mount ("https" , adapter )
41+
42+ thread_local .session = http
2943 return thread_local .session
3044
3145 def _smart_read_chunk (self , chunk_offset , is_final_chunk ):
@@ -47,7 +61,7 @@ def _upload_chunk(self, task):
4761
4862 if chunk_id + 1 == chunks_total :
4963 is_final_chunk = True
50-
64+
5165 session = self ._get_session ()
5266
5367 chunk_data = self ._smart_read_chunk (chunk_offset , is_final_chunk )
0 commit comments