You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

retry.go 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Minio Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 Minio, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package minio_ext
  18. import (
  19. "net"
  20. "net/http"
  21. "net/url"
  22. "strings"
  23. "time"
  24. )
  25. // MaxRetry is the maximum number of retries before stopping.
  26. var MaxRetry = 10
  27. // MaxJitter will randomize over the full exponential backoff time
  28. const MaxJitter = 1.0
  29. // NoJitter disables the use of jitter for randomizing the exponential backoff time
  30. const NoJitter = 0.0
  31. // DefaultRetryUnit - default unit multiplicative per retry.
  32. // defaults to 1 second.
  33. const DefaultRetryUnit = time.Second
  34. // DefaultRetryCap - Each retry attempt never waits no longer than
  35. // this maximum time duration.
  36. const DefaultRetryCap = time.Second * 30
  37. // newRetryTimer creates a timer with exponentially increasing
  38. // delays until the maximum retry attempts are reached.
  39. func (c Client) newRetryTimer(maxRetry int, unit time.Duration, cap time.Duration, jitter float64, doneCh chan struct{}) <-chan int {
  40. attemptCh := make(chan int)
  41. // computes the exponential backoff duration according to
  42. // https://www.awsarchitectureblog.com/2015/03/backoff.html
  43. exponentialBackoffWait := func(attempt int) time.Duration {
  44. // normalize jitter to the range [0, 1.0]
  45. if jitter < NoJitter {
  46. jitter = NoJitter
  47. }
  48. if jitter > MaxJitter {
  49. jitter = MaxJitter
  50. }
  51. //sleep = random_between(0, min(cap, base * 2 ** attempt))
  52. sleep := unit * time.Duration(1<<uint(attempt))
  53. if sleep > cap {
  54. sleep = cap
  55. }
  56. if jitter != NoJitter {
  57. sleep -= time.Duration(c.random.Float64() * float64(sleep) * jitter)
  58. }
  59. return sleep
  60. }
  61. go func() {
  62. defer close(attemptCh)
  63. for i := 0; i < maxRetry; i++ {
  64. select {
  65. // Attempts start from 1.
  66. case attemptCh <- i + 1:
  67. case <-doneCh:
  68. // Stop the routine.
  69. return
  70. }
  71. time.Sleep(exponentialBackoffWait(i))
  72. }
  73. }()
  74. return attemptCh
  75. }
  76. // isHTTPReqErrorRetryable - is http requests error retryable, such
  77. // as i/o timeout, connection broken etc..
  78. func isHTTPReqErrorRetryable(err error) bool {
  79. if err == nil {
  80. return false
  81. }
  82. switch e := err.(type) {
  83. case *url.Error:
  84. switch e.Err.(type) {
  85. case *net.DNSError, *net.OpError, net.UnknownNetworkError:
  86. return true
  87. }
  88. if strings.Contains(err.Error(), "Connection closed by foreign host") {
  89. return true
  90. } else if strings.Contains(err.Error(), "net/http: TLS handshake timeout") {
  91. // If error is - tlsHandshakeTimeoutError, retry.
  92. return true
  93. } else if strings.Contains(err.Error(), "i/o timeout") {
  94. // If error is - tcp timeoutError, retry.
  95. return true
  96. } else if strings.Contains(err.Error(), "connection timed out") {
  97. // If err is a net.Dial timeout, retry.
  98. return true
  99. } else if strings.Contains(err.Error(), "net/http: HTTP/1.x transport connection broken") {
  100. // If error is transport connection broken, retry.
  101. return true
  102. }
  103. }
  104. return false
  105. }
  106. // List of AWS S3 error codes which are retryable.
  107. var retryableS3Codes = map[string]struct{}{
  108. "RequestError": {},
  109. "RequestTimeout": {},
  110. "Throttling": {},
  111. "ThrottlingException": {},
  112. "RequestLimitExceeded": {},
  113. "RequestThrottled": {},
  114. "InternalError": {},
  115. "ExpiredToken": {},
  116. "ExpiredTokenException": {},
  117. "SlowDown": {},
  118. // Add more AWS S3 codes here.
  119. }
  120. // isS3CodeRetryable - is s3 error code retryable.
  121. func isS3CodeRetryable(s3Code string) (ok bool) {
  122. _, ok = retryableS3Codes[s3Code]
  123. return ok
  124. }
  125. // List of HTTP status codes which are retryable.
  126. var retryableHTTPStatusCodes = map[int]struct{}{
  127. 429: {}, // http.StatusTooManyRequests is not part of the Go 1.5 library, yet
  128. http.StatusInternalServerError: {},
  129. http.StatusBadGateway: {},
  130. http.StatusServiceUnavailable: {},
  131. // Add more HTTP status codes here.
  132. }
  133. // isHTTPStatusRetryable - is HTTP error code retryable.
  134. func isHTTPStatusRetryable(httpStatusCode int) (ok bool) {
  135. _, ok = retryableHTTPStatusCodes[httpStatusCode]
  136. return ok
  137. }