retry.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package metadata
  15. import (
  16. "context"
  17. "io"
  18. "math/rand"
  19. "net/http"
  20. "time"
  21. )
  22. const (
  23. maxRetryAttempts = 5
  24. )
  25. var (
  26. syscallRetryable = func(error) bool { return false }
  27. )
  28. // defaultBackoff is basically equivalent to gax.Backoff without the need for
  29. // the dependency.
  30. type defaultBackoff struct {
  31. max time.Duration
  32. mul float64
  33. cur time.Duration
  34. }
  35. func (b *defaultBackoff) Pause() time.Duration {
  36. d := time.Duration(1 + rand.Int63n(int64(b.cur)))
  37. b.cur = time.Duration(float64(b.cur) * b.mul)
  38. if b.cur > b.max {
  39. b.cur = b.max
  40. }
  41. return d
  42. }
  43. // sleep is the equivalent of gax.Sleep without the need for the dependency.
  44. func sleep(ctx context.Context, d time.Duration) error {
  45. t := time.NewTimer(d)
  46. select {
  47. case <-ctx.Done():
  48. t.Stop()
  49. return ctx.Err()
  50. case <-t.C:
  51. return nil
  52. }
  53. }
  54. func newRetryer() *metadataRetryer {
  55. return &metadataRetryer{bo: &defaultBackoff{
  56. cur: 100 * time.Millisecond,
  57. max: 30 * time.Second,
  58. mul: 2,
  59. }}
  60. }
  61. type backoff interface {
  62. Pause() time.Duration
  63. }
  64. type metadataRetryer struct {
  65. bo backoff
  66. attempts int
  67. }
  68. func (r *metadataRetryer) Retry(status int, err error) (time.Duration, bool) {
  69. if status == http.StatusOK {
  70. return 0, false
  71. }
  72. retryOk := shouldRetry(status, err)
  73. if !retryOk {
  74. return 0, false
  75. }
  76. if r.attempts == maxRetryAttempts {
  77. return 0, false
  78. }
  79. r.attempts++
  80. return r.bo.Pause(), true
  81. }
  82. func shouldRetry(status int, err error) bool {
  83. if 500 <= status && status <= 599 {
  84. return true
  85. }
  86. if err == io.ErrUnexpectedEOF {
  87. return true
  88. }
  89. // Transient network errors should be retried.
  90. if syscallRetryable(err) {
  91. return true
  92. }
  93. if err, ok := err.(interface{ Temporary() bool }); ok {
  94. if err.Temporary() {
  95. return true
  96. }
  97. }
  98. if err, ok := err.(interface{ Unwrap() error }); ok {
  99. return shouldRetry(status, err.Unwrap())
  100. }
  101. return false
  102. }