Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update source temporary error logic #1739

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/daemon/peer/piece_manager.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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