From f16c5592abf96f75ef8aeace7e042e3249fe1fe8 Mon Sep 17 00:00:00 2001 From: Gaius Date: Wed, 2 Mar 2022 14:59:19 +0800 Subject: [PATCH] feat: change back-to-source timeout (#1112) Signed-off-by: Gaius --- pkg/source/source_client.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/source/source_client.go b/pkg/source/source_client.go index a2277eb1e..1c7343014 100644 --- a/pkg/source/source_client.go +++ b/pkg/source/source_client.go @@ -31,6 +31,11 @@ import ( logger "d7y.io/dragonfly/v2/internal/dflog" ) +var ( + // Clinet back-to-source download timeout + contextTimeout = 30 * time.Second +) + var ( // ErrResourceNotReachable represents the url resource is a not reachable. ErrResourceNotReachable = errors.New("resource is not reachable") @@ -260,7 +265,7 @@ func GetContentLength(request *Request) (int64, error) { return UnknownSourceFileLen, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme) } if _, ok := request.Context().Deadline(); !ok { - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) request = request.WithContext(ctx) defer cancel() } @@ -273,7 +278,7 @@ func IsSupportRange(request *Request) (bool, error) { return false, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme) } if _, ok := request.Context().Deadline(); !ok { - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) request = request.WithContext(ctx) defer cancel() } @@ -289,7 +294,7 @@ func IsExpired(request *Request, info *ExpireInfo) (bool, error) { return false, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme) } if _, ok := request.Context().Deadline(); !ok { - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) request = request.WithContext(ctx) defer cancel() } @@ -302,7 +307,7 @@ func GetLastModified(request *Request) (int64, error) { return -1, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme) } if _, ok := request.Context().Deadline(); !ok { - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) request = request.WithContext(ctx) defer cancel() }