fix: wrong cache header (#423)

Signed-off-by: santong <weipeng.swp@alibaba-inc.com>
This commit is contained in:
sunwp 2021-07-05 17:07:51 +08:00 committed by Gaius
parent a8f2b818d1
commit 158ea2ec4a
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
3 changed files with 11 additions and 6 deletions

View File

@ -19,6 +19,7 @@ package task
import ( import (
"context" "context"
"fmt" "fmt"
"reflect"
"time" "time"
"d7y.io/dragonfly/v2/cdnsystem/config" "d7y.io/dragonfly/v2/cdnsystem/config"
@ -46,14 +47,17 @@ func (tm *Manager) addOrUpdateTask(ctx context.Context, request *types.TaskRegis
synclock.Lock(taskID, false) synclock.Lock(taskID, false)
defer synclock.UnLock(taskID, false) defer synclock.UnLock(taskID, false)
if key, err := tm.taskURLUnReachableStore.Get(taskID); err == nil { if key, err := tm.taskURLUnReachableStore.Get(taskID); err == nil {
if unReachableStartTime, ok := key.(time.Time); ok && if unReachableStartTime, ok := key.(time.Time); ok && time.Since(unReachableStartTime) < tm.cfg.FailAccessInterval {
time.Since(unReachableStartTime) < tm.cfg.FailAccessInterval { existTask, err := tm.taskStore.Get(taskID)
return nil, errors.Wrapf(cdnerrors.ErrURLNotReachable{URL: request.URL}, "task hit unReachable cache and interval less than %d, "+ if err != nil || reflect.DeepEqual(request.Header, existTask.(*types.SeedTask).Header) {
"url: %s", tm.cfg.FailAccessInterval, request.URL) 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) tm.taskURLUnReachableStore.Delete(taskID)
logger.Debugf("delete taskID:%s from url unReachable store", taskID) logger.Debugf("delete taskID:%s from url unReachable store", taskID)
} }
var task *types.SeedTask var task *types.SeedTask
newTask := &types.SeedTask{ newTask := &types.SeedTask{
TaskID: taskID, TaskID: taskID,

View File

@ -24,6 +24,7 @@ import (
"net/http" "net/http"
"time" "time"
"d7y.io/dragonfly/v2/cdnsystem/daemon/task"
"d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/source"
"d7y.io/dragonfly/v2/pkg/structure/maputils" "d7y.io/dragonfly/v2/pkg/structure/maputils"
"d7y.io/dragonfly/v2/pkg/util/stringutils" "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 // 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 { 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 // 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 return resp.ContentLength, nil
} }

View File

@ -37,7 +37,7 @@ type ResourceClient interface {
// GetContentLength get length of resource content // GetContentLength get length of resource content
// return -l if request fail // 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) GetContentLength(ctx context.Context, url string, header RequestHeader) (int64, error)
// IsSupportRange checks if resource supports breakpoint continuation // IsSupportRange checks if resource supports breakpoint continuation