base-golang/pkg/httpclient/retry.go
2024-07-23 10:23:43 +08:00

45 lines
767 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package httpclient
import (
"context"
"net/http"
"time"
)
const (
// DefaultRetryTimes 如果请求失败最多重试3次
DefaultRetryTimes = 3
// DefaultRetryDelay 在重试前延迟等待100毫秒
DefaultRetryDelay = time.Millisecond * 100
)
// Verify parse the body and verify that it is correct
type RetryVerify func(body []byte) (shouldRetry bool)
func shouldRetry(ctx context.Context, httpCode int) bool {
select {
case <-ctx.Done():
return false
default:
}
switch httpCode {
case
_StatusReadRespErr,
_StatusDoReqErr,
http.StatusRequestTimeout,
http.StatusLocked,
http.StatusTooEarly,
http.StatusTooManyRequests,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
return true
default:
return false
}
}