-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
54 lines (45 loc) · 1.36 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package httpify
import (
"context"
"crypto/x509"
"net/http"
"net/url"
"regexp"
)
var (
redirectsErrorRegex = regexp.MustCompile(`stopped after \d+ redirects\z`)
schemeErrorRegex = regexp.MustCompile(`unsupported protocol scheme`)
)
// CheckRetry defines a policy for retrying requests based on the response and error.
type CheckRetry func(ctx context.Context, resp *http.Response, err error) (bool, error)
// DefaultRetryPolicy retries on connection errors and server errors.
func DefaultRetryPolicy() CheckRetry {
return func(ctx context.Context, resp *http.Response, err error) (bool, error) {
if ctx.Err() != nil {
return false, ctx.Err()
}
if err != nil {
if urlErr, ok := err.(*url.Error); ok {
// Handle specific error conditions
if isNonRetryableError(urlErr) {
return false, nil
}
}
return true, nil // Retry on likely recoverable error
}
return false, nil
}
}
// HostSprayRetryPolicy retries on connection and server errors for host-spraying use cases.
func HostSprayRetryPolicy() CheckRetry {
return DefaultRetryPolicy()
}
func isNonRetryableError(urlErr *url.Error) bool {
return redirectsErrorRegex.MatchString(urlErr.Error()) ||
schemeErrorRegex.MatchString(urlErr.Error()) ||
isTLSCertError(urlErr)
}
func isTLSCertError(urlErr *url.Error) bool {
_, ok := urlErr.Err.(x509.UnknownAuthorityError)
return ok
}