Skip to content

Commit 9032a98

Browse files
committed
Tus retry logic with cooldown.
1 parent f65e6be commit 9032a98

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

vimeo/upload.py

+21-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
from __future__ import absolute_import
55

6-
import os
76
import io
7+
import os
88
import requests.exceptions
9-
from tusclient import client
109
from . import exceptions
10+
from time import sleep
11+
from tusclient import client
1112

1213
try:
1314
basestring
@@ -119,7 +120,7 @@ def replace(self, video_uri, filename, **kwargs):
119120

120121
def __perform_tus_upload(self, filename, attempt):
121122
"""Take an upload attempt and perform the actual upload via tus.
122-
123+
Upon encountering an error/exception will attempt retries (3) with an exponential cooldown.
123124
https://tus.io/
124125
125126
Args:
@@ -135,17 +136,23 @@ def __perform_tus_upload(self, filename, attempt):
135136
video.
136137
"""
137138
upload_link = attempt.get('upload').get('upload_link')
138-
139-
try:
140-
with io.open(filename, 'rb') as fs:
141-
tus_client = client.TusClient('https://files.tus.vimeo.com')
142-
uploader = tus_client.uploader(file_stream=fs, url=upload_link)
143-
uploader.upload()
144-
except Exception as e:
145-
raise exceptions.VideoUploadFailure(
146-
e,
147-
'Unexpected error when uploading through tus.'
148-
)
139+
failures = 0
140+
max_failures = 3
141+
142+
while failures < max_failures:
143+
try:
144+
with io.open(filename, 'rb') as fs:
145+
tus_client = client.TusClient('https://files.tus.vimeo.com')
146+
uploader = tus_client.uploader(file_stream=fs, url=upload_link)
147+
uploader.upload()
148+
except Exception as e:
149+
failures += 1
150+
if failures >= max_failures:
151+
raise exceptions.VideoUploadFailure(
152+
e,
153+
'Unexpected error when uploading through tus.'
154+
)
155+
sleep(pow(4, failures))
149156

150157
return attempt.get('uri')
151158

0 commit comments

Comments
 (0)