diff --git a/client/daemon/peer/piece_manager.go b/client/daemon/peer/piece_manager.go index 6894b3900..0ad4bd0d4 100644 --- a/client/daemon/peer/piece_manager.go +++ b/client/daemon/peer/piece_manager.go @@ -385,7 +385,7 @@ singleDownload: } } srcErr := &errordetailsv1.SourceError{ - Temporary: response.Temporary == nil || response.Temporary(), + Temporary: response.Temporary, Metadata: &commonv1.ExtendAttribute{ Header: hdr, StatusCode: int32(response.StatusCode), diff --git a/pkg/source/clients/httpprotocol/http_source_client.go b/pkg/source/clients/httpprotocol/http_source_client.go index 96dcdc1ea..bed53a183 100644 --- a/pkg/source/clients/httpprotocol/http_source_client.go +++ b/pkg/source/clients/httpprotocol/http_source_client.go @@ -52,7 +52,12 @@ var ( contentRangeRegexp = regexp.MustCompile(`bytes (?P\d+)-(?P\d+)/(?P(\d*|\*))`) contentRangeRegexpLengthIndex = contentRangeRegexp.SubexpIndex("Length") - notTemporaryStatusCode = []int{http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound, http.StatusProxyAuthRequired} + notTemporaryStatusCode = []int{ + http.StatusUnauthorized, + http.StatusForbidden, + http.StatusNotFound, + http.StatusProxyAuthRequired, + } ) func init() { @@ -218,14 +223,7 @@ func (client *httpSourceClient) GetMetadata(request *source.Request) (*source.Me Validate: func() error { return source.CheckResponseCode(resp.StatusCode, []int{http.StatusOK, http.StatusPartialContent}) }, - Temporary: func() bool { - for _, code := range notTemporaryStatusCode { - if code == resp.StatusCode { - return false - } - } - return false - }, + Temporary: detectTemporary(resp.StatusCode), }, nil } @@ -261,14 +259,7 @@ func (client *httpSourceClient) Download(request *source.Request) (*source.Respo source.WithValidate(func() error { return source.CheckResponseCode(resp.StatusCode, []int{http.StatusOK, http.StatusPartialContent}) }), - source.WithTemporary(func() bool { - for _, code := range notTemporaryStatusCode { - if code == resp.StatusCode { - return false - } - } - return true - }), + source.WithTemporary(detectTemporary(resp.StatusCode)), source.WithHeader(exportPassThroughHeader(resp.Header)), source.WithExpireInfo( source.ExpireInfo{ @@ -336,3 +327,12 @@ func exportPassThroughHeader(header http.Header) map[string]string { } return ph } + +func detectTemporary(statusCode int) bool { + for _, code := range notTemporaryStatusCode { + if code == statusCode { + return false + } + } + return true +} diff --git a/pkg/source/clients/ossprotocol/oss_source_client.go b/pkg/source/clients/ossprotocol/oss_source_client.go index 1771d6445..6d32e833f 100644 --- a/pkg/source/clients/ossprotocol/oss_source_client.go +++ b/pkg/source/clients/ossprotocol/oss_source_client.go @@ -173,9 +173,7 @@ func (osc *ossSourceClient) GetMetadata(request *source.Request) (*source.Metada Validate: func() error { return nil }, - Temporary: func() bool { - return true - }, + Temporary: true, }, nil } diff --git a/pkg/source/metadata.go b/pkg/source/metadata.go index 45a7e6ca4..50f8c9a40 100644 --- a/pkg/source/metadata.go +++ b/pkg/source/metadata.go @@ -31,5 +31,5 @@ type Metadata struct { TotalContentLength int64 Validate func() error - Temporary func() bool + Temporary bool } diff --git a/pkg/source/response.go b/pkg/source/response.go index e9e376c51..ea19c8a24 100644 --- a/pkg/source/response.go +++ b/pkg/source/response.go @@ -35,8 +35,8 @@ type Response struct { // Validate this response is okay to transfer in p2p network, like status 200 or 206 in http is valid to do this, // otherwise return status code to original client Validate func() error - // Temporary check the error whether the error is temporary, if is true, we can retry it later - Temporary func() bool + // Temporary indecates the error whether the error is temporary, if is true, we can retry it later + Temporary bool } func NewResponse(rc io.ReadCloser, opts ...func(*Response)) *Response { @@ -54,9 +54,7 @@ func NewResponse(rc io.ReadCloser, opts ...func(*Response)) *Response { Validate: func() error { return nil }, - Temporary: func() bool { - return true - }, + Temporary: true, } for _, opt := range opts { @@ -103,7 +101,7 @@ func WithValidate(validate func() error) func(*Response) { } } -func WithTemporary(temporary func() bool) func(*Response) { +func WithTemporary(temporary bool) func(*Response) { return func(resp *Response) { resp.Temporary = temporary }