123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package minio
- import (
- "context"
- "net/http"
- "time"
- )
- var MaxRetry = 10
- const MaxJitter = 1.0
- const NoJitter = 0.0
- var DefaultRetryUnit = 200 * time.Millisecond
- var DefaultRetryCap = time.Second
- func (c Client) newRetryTimer(ctx context.Context, maxRetry int, unit time.Duration, cap time.Duration, jitter float64) <-chan int {
- attemptCh := make(chan int)
-
-
- exponentialBackoffWait := func(attempt int) time.Duration {
-
- if jitter < NoJitter {
- jitter = NoJitter
- }
- if jitter > MaxJitter {
- jitter = MaxJitter
- }
-
- sleep := unit * time.Duration(1<<uint(attempt))
- if sleep > cap {
- sleep = cap
- }
- if jitter != NoJitter {
- sleep -= time.Duration(c.random.Float64() * float64(sleep) * jitter)
- }
- return sleep
- }
- go func() {
- defer close(attemptCh)
- for i := 0; i < maxRetry; i++ {
- select {
- case attemptCh <- i + 1:
- case <-ctx.Done():
- return
- }
- select {
- case <-time.After(exponentialBackoffWait(i)):
- case <-ctx.Done():
- return
- }
- }
- }()
- return attemptCh
- }
- var retryableS3Codes = map[string]struct{}{
- "RequestError": {},
- "RequestTimeout": {},
- "Throttling": {},
- "ThrottlingException": {},
- "RequestLimitExceeded": {},
- "RequestThrottled": {},
- "InternalError": {},
- "ExpiredToken": {},
- "ExpiredTokenException": {},
- "SlowDown": {},
-
- }
- func isS3CodeRetryable(s3Code string) (ok bool) {
- _, ok = retryableS3Codes[s3Code]
- return ok
- }
- var retryableHTTPStatusCodes = map[int]struct{}{
- 429: {},
- http.StatusInternalServerError: {},
- http.StatusBadGateway: {},
- http.StatusServiceUnavailable: {},
- http.StatusGatewayTimeout: {},
-
- }
- func isHTTPStatusRetryable(httpStatusCode int) (ok bool) {
- _, ok = retryableHTTPStatusCodes[httpStatusCode]
- return ok
- }
|