Skip to content

Commit

Permalink
Merge pull request #126 from vimeo/api-2666
Browse files Browse the repository at this point in the history
Tus retry logic with cooldown.
  • Loading branch information
erunion authored Jul 16, 2018
2 parents f65e6be + 9032a98 commit 4c70128
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions vimeo/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

from __future__ import absolute_import

import os
import io
import os
import requests.exceptions
from tusclient import client
from . import exceptions
from time import sleep
from tusclient import client

try:
basestring
Expand Down Expand Up @@ -119,7 +120,7 @@ def replace(self, video_uri, filename, **kwargs):

def __perform_tus_upload(self, filename, attempt):
"""Take an upload attempt and perform the actual upload via tus.
Upon encountering an error/exception will attempt retries (3) with an exponential cooldown.
https://tus.io/
Args:
Expand All @@ -135,17 +136,23 @@ def __perform_tus_upload(self, filename, attempt):
video.
"""
upload_link = attempt.get('upload').get('upload_link')

try:
with io.open(filename, 'rb') as fs:
tus_client = client.TusClient('https://files.tus.vimeo.com')
uploader = tus_client.uploader(file_stream=fs, url=upload_link)
uploader.upload()
except Exception as e:
raise exceptions.VideoUploadFailure(
e,
'Unexpected error when uploading through tus.'
)
failures = 0
max_failures = 3

while failures < max_failures:
try:
with io.open(filename, 'rb') as fs:
tus_client = client.TusClient('https://files.tus.vimeo.com')
uploader = tus_client.uploader(file_stream=fs, url=upload_link)
uploader.upload()
except Exception as e:
failures += 1
if failures >= max_failures:
raise exceptions.VideoUploadFailure(
e,
'Unexpected error when uploading through tus.'
)
sleep(pow(4, failures))

return attempt.get('uri')

Expand Down

0 comments on commit 4c70128

Please sign in to comment.