4848 regexErrInvalidHeader = regexp .MustCompile ("invalid header" )
4949)
5050
51- func applyRetryDefaultConditions (res * Response , err error ) bool {
51+ // RetryConditionStatusTooManyRequests is a RetryConditionFunc that returns true
52+ // if the response status code is 429 Too Many Requests.
53+ //
54+ // - The 429 status code indicates that the user has sent too many requests in a given amount
55+ // of time ("rate limiting").
56+ // - Retrying after receiving a 429 status code can be effective, especially if the server includes
57+ // a Retry-After header indicating when to retry.
58+ func RetryConditionStatusTooManyRequests (res * Response , _ error ) bool {
59+ if res == nil {
60+ return false
61+ }
62+ return res .StatusCode () == http .StatusTooManyRequests
63+ }
64+
65+ // RetryConditionStatus5XX is a RetryConditionFunc that returns true if the response status code is 500 or above,
66+ // excluding Status 501 - Not Implemented.
67+ //
68+ // - 5XX status codes are generally considered temporary server errors that may be resolved on retry
69+ // - The rationale for excluding 501 Not Implemented is that it indicates the server does not support the
70+ // functionality required to fulfill the request,
71+ func RetryConditionStatus5XX (res * Response , _ error ) bool {
72+ if res == nil {
73+ return false
74+ }
75+ return res .StatusCode () >= 500 && res .StatusCode () != http .StatusNotImplemented
76+ }
77+
78+ // RetryConditionStatusZero is a RetryConditionFunc that returns true if the response status code is 0.
79+ //
80+ // - A status code of 0 typically indicates that no response was received from the server, which can occur
81+ // due to network errors, timeouts, or other issues that prevent the request from being completed.
82+ // - Retrying when a status code of 0 is encountered can be effective, as it may allow the request to succeed
83+ // on subsequent attempts if the underlying issue is transient.
84+ func RetryConditionStatusZero (res * Response , _ error ) bool {
85+ if res == nil {
86+ return false
87+ }
88+ return res .StatusCode () == 0
89+ }
90+
91+ // isDoNotRetryError checks whether the given request error should trigger a retry.
92+ //
93+ // It returns true only for retryable URL/network errors and false for errors that
94+ // should not be retried, such as TLS certificate errors, invalid scheme, invalid
95+ // headers, and too many redirects.
96+ func isDoNotRetryError (err error ) bool {
5297 // no retry on TLS error
5398 if _ , ok := err .(* tls.CertificateVerificationError ); ok {
5499 return false
@@ -68,20 +113,6 @@ func applyRetryDefaultConditions(res *Response, err error) bool {
68113 return u .Temporary () // possible retry if it's true
69114 }
70115
71- if res == nil {
72- return false
73- }
74-
75- // certain HTTP status codes are temporary so that we can retry
76- // - 429 Too Many Requests
77- // - 500 or above (it's better to ignore 501 Not Implemented)
78- // - 0 No status code received
79- if res .StatusCode () == http .StatusTooManyRequests ||
80- (res .StatusCode () >= 500 && res .StatusCode () != http .StatusNotImplemented ) ||
81- res .StatusCode () == 0 {
82- return true
83- }
84-
85116 return false
86117}
87118
0 commit comments