Skip to content

Commit

Permalink
feat: update source temporary error logic (#1739)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <majinjing3@gmail.com>
  • Loading branch information
jim3ma committed Oct 10, 2022
1 parent 21fd52c commit e9aae40
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion client/daemon/peer/piece_manager.go
Expand Up @@ -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),
Expand Down
34 changes: 17 additions & 17 deletions pkg/source/clients/httpprotocol/http_source_client.go
Expand Up @@ -52,7 +52,12 @@ var (
contentRangeRegexp = regexp.MustCompile(`bytes (?P<Start>\d+)-(?P<End>\d+)/(?P<Length>(\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() {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions pkg/source/clients/ossprotocol/oss_source_client.go
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/metadata.go
Expand Up @@ -31,5 +31,5 @@ type Metadata struct {
TotalContentLength int64

Validate func() error
Temporary func() bool
Temporary bool
}
10 changes: 4 additions & 6 deletions pkg/source/response.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e9aae40

Please sign in to comment.