From de8eaad5ee9e10aa04806b71158dce14709cccb5 Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Fri, 21 Jun 2024 11:53:01 +0100 Subject: [PATCH] clients: Download segments from backup URL --- clients/input_copy.go | 24 ++++++++++++++++++++++++ clients/manifest.go | 2 +- thumbnails/thumbnails.go | 2 +- transcode/transcode.go | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/clients/input_copy.go b/clients/input_copy.go index d7cb1dd56..feb5c2ddf 100644 --- a/clients/input_copy.go +++ b/clients/input_copy.go @@ -287,6 +287,30 @@ func GetFile(ctx context.Context, requestID, url string, dStorage *DStorageDownl } } +func GetFileWithBackup(ctx context.Context, requestID, url string, dStorage *DStorageDownload) (io.ReadCloser, error) { + rc, err := GetFile(ctx, requestID, url, dStorage) + if err == nil { + return rc, nil + } + + backupURL := config.GetStorageBackupURL(url) + if backupURL == "" { + return nil, err + } + rc, backupErr := GetFile(ctx, requestID, backupURL, dStorage) + if backupErr == nil { + return rc, nil + } + + // prioritize retriable errors in the response so we don't skip retries + if !xerrors.IsUnretriable(err) { + return nil, err + } else if !xerrors.IsUnretriable(backupErr) { + return nil, backupErr + } + return nil, err +} + var retryableHttpClient = newRetryableHttpClient() func newRetryableHttpClient() *http.Client { diff --git a/clients/manifest.go b/clients/manifest.go index 25315997a..38d504365 100644 --- a/clients/manifest.go +++ b/clients/manifest.go @@ -332,7 +332,7 @@ func ClipInputManifest(requestID, sourceURL, clipTargetUrl string, startTimeUnix segmentURL := sourceSegmentURLs[v.SeqId].URL dStorage := NewDStorageDownload() err = backoff.Retry(func() error { - rc, err := GetFile(context.Background(), requestID, segmentURL.String(), dStorage) + rc, err := GetFileWithBackup(context.Background(), requestID, segmentURL.String(), dStorage) if err != nil { return fmt.Errorf("error clipping: failed to download segment %d: %w", v.SeqId, err) } diff --git a/thumbnails/thumbnails.go b/thumbnails/thumbnails.go index 6e7a8490b..8f9bd8967 100644 --- a/thumbnails/thumbnails.go +++ b/thumbnails/thumbnails.go @@ -169,7 +169,7 @@ func GenerateThumbsFromManifest(requestID, input string, output *url.URL) error ) // save the segment to memory err = backoff.Retry(func() error { - rc, err = clients.GetFile(context.Background(), requestID, segURL.String(), nil) + rc, err = clients.GetFileWithBackup(context.Background(), requestID, segURL.String(), nil) return err }, clients.DownloadRetryBackoff()) if err != nil { diff --git a/transcode/transcode.go b/transcode/transcode.go index 4fbf2f532..09ebfdde0 100644 --- a/transcode/transcode.go +++ b/transcode/transcode.go @@ -536,7 +536,7 @@ func transcodeSegment( err := backoff.Retry(func() error { ctx, cancel := context.WithTimeout(context.Background(), clients.MaxCopyFileDuration) defer cancel() - rc, err := clients.GetFile(ctx, transcodeRequest.RequestID, segment.Input.URL.String(), nil) + rc, err := clients.GetFileWithBackup(ctx, transcodeRequest.RequestID, segment.Input.URL.String(), nil) if err != nil { return fmt.Errorf("failed to download source segment %q: %w", segment.Input, err) }