From 158ea2ec4a233e74c1f3643384f067106bab5341 Mon Sep 17 00:00:00 2001 From: sunwp Date: Mon, 5 Jul 2021 17:07:51 +0800 Subject: [PATCH] fix: wrong cache header (#423) Signed-off-by: santong --- cdnsystem/daemon/task/manager_util.go | 12 ++++++++---- pkg/source/httpprotocol/http_source_client.go | 3 ++- pkg/source/source_client.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cdnsystem/daemon/task/manager_util.go b/cdnsystem/daemon/task/manager_util.go index a4a1395e0..04f100218 100644 --- a/cdnsystem/daemon/task/manager_util.go +++ b/cdnsystem/daemon/task/manager_util.go @@ -19,6 +19,7 @@ package task import ( "context" "fmt" + "reflect" "time" "d7y.io/dragonfly/v2/cdnsystem/config" @@ -46,14 +47,17 @@ func (tm *Manager) addOrUpdateTask(ctx context.Context, request *types.TaskRegis synclock.Lock(taskID, false) defer synclock.UnLock(taskID, false) if key, err := tm.taskURLUnReachableStore.Get(taskID); err == nil { - if unReachableStartTime, ok := key.(time.Time); ok && - time.Since(unReachableStartTime) < tm.cfg.FailAccessInterval { - return nil, errors.Wrapf(cdnerrors.ErrURLNotReachable{URL: request.URL}, "task hit unReachable cache and interval less than %d, "+ - "url: %s", tm.cfg.FailAccessInterval, request.URL) + if unReachableStartTime, ok := key.(time.Time); ok && time.Since(unReachableStartTime) < tm.cfg.FailAccessInterval { + existTask, err := tm.taskStore.Get(taskID) + if err != nil || reflect.DeepEqual(request.Header, existTask.(*types.SeedTask).Header) { + return nil, errors.Wrapf(cdnerrors.ErrURLNotReachable{URL: request.URL}, "task hit unReachable cache and interval less than %d, "+ + "url: %s", tm.cfg.FailAccessInterval, request.URL) + } } tm.taskURLUnReachableStore.Delete(taskID) logger.Debugf("delete taskID:%s from url unReachable store", taskID) } + var task *types.SeedTask newTask := &types.SeedTask{ TaskID: taskID, diff --git a/pkg/source/httpprotocol/http_source_client.go b/pkg/source/httpprotocol/http_source_client.go index 485e0e08f..4a4f5fe8c 100644 --- a/pkg/source/httpprotocol/http_source_client.go +++ b/pkg/source/httpprotocol/http_source_client.go @@ -24,6 +24,7 @@ import ( "net/http" "time" + "d7y.io/dragonfly/v2/cdnsystem/daemon/task" "d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/structure/maputils" "d7y.io/dragonfly/v2/pkg/util/stringutils" @@ -86,7 +87,7 @@ func (client *httpSourceClient) GetContentLength(ctx context.Context, url string // todo Here if other status codes should be added to ErrURLNotReachable, if not, it will be downloaded frequently for 404 or 403 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { // todo Whether this situation should be distinguished from the err situation, similar to proposing another error type to indicate that this error can interact with the URL, but the status code does not meet expectations - return -1, fmt.Errorf("get http resource length failed, unexpected code: %d", resp.StatusCode) + return task.IllegalSourceFileLen, fmt.Errorf("get http resource length failed, unexpected code: %d", resp.StatusCode) } return resp.ContentLength, nil } diff --git a/pkg/source/source_client.go b/pkg/source/source_client.go index f7202929d..cb5e6379d 100644 --- a/pkg/source/source_client.go +++ b/pkg/source/source_client.go @@ -37,7 +37,7 @@ type ResourceClient interface { // GetContentLength get length of resource content // return -l if request fail - // return -1 if response status is not StatusOK and StatusPartialContent + // return task.IllegalSourceFileLen if response status is not StatusOK and StatusPartialContent GetContentLength(ctx context.Context, url string, header RequestHeader) (int64, error) // IsSupportRange checks if resource supports breakpoint continuation