retry.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package vim25
  14. import (
  15. "context"
  16. "time"
  17. "github.com/vmware/govmomi/vim25/soap"
  18. )
  19. type RetryFunc func(err error) (retry bool, delay time.Duration)
  20. // TemporaryNetworkError is deprecated. Use Retry() with RetryTemporaryNetworkError and retryAttempts instead.
  21. func TemporaryNetworkError(n int) RetryFunc {
  22. return func(err error) (bool, time.Duration) {
  23. if IsTemporaryNetworkError(err) {
  24. // Don't retry if we're out of tries.
  25. if n--; n <= 0 {
  26. return false, 0
  27. }
  28. return true, 0
  29. }
  30. return false, 0
  31. }
  32. }
  33. // RetryTemporaryNetworkError returns a RetryFunc that returns IsTemporaryNetworkError(err)
  34. func RetryTemporaryNetworkError(err error) (bool, time.Duration) {
  35. return IsTemporaryNetworkError(err), 0
  36. }
  37. // IsTemporaryNetworkError returns false unless the error implements
  38. // a Temporary() bool method such as url.Error and net.Error.
  39. // Otherwise, returns the value of the Temporary() method.
  40. func IsTemporaryNetworkError(err error) bool {
  41. t, ok := err.(interface {
  42. // Temporary is implemented by url.Error and net.Error
  43. Temporary() bool
  44. })
  45. if !ok {
  46. // Not a Temporary error.
  47. return false
  48. }
  49. return t.Temporary()
  50. }
  51. type retry struct {
  52. roundTripper soap.RoundTripper
  53. // fn is a custom function that is called when an error occurs.
  54. // It returns whether or not to retry, and if so, how long to
  55. // delay before retrying.
  56. fn RetryFunc
  57. maxRetryAttempts int
  58. }
  59. // Retry wraps the specified soap.RoundTripper and invokes the
  60. // specified RetryFunc. The RetryFunc returns whether or not to
  61. // retry the call, and if so, how long to wait before retrying. If
  62. // the result of this function is to not retry, the original error
  63. // is returned from the RoundTrip function.
  64. // The soap.RoundTripper will return the original error if retryAttempts is specified and reached.
  65. func Retry(roundTripper soap.RoundTripper, fn RetryFunc, retryAttempts ...int) soap.RoundTripper {
  66. r := &retry{
  67. roundTripper: roundTripper,
  68. fn: fn,
  69. maxRetryAttempts: 1,
  70. }
  71. if len(retryAttempts) == 1 {
  72. r.maxRetryAttempts = retryAttempts[0]
  73. }
  74. return r
  75. }
  76. func (r *retry) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
  77. var err error
  78. for attempt := 0; attempt < r.maxRetryAttempts; attempt++ {
  79. err = r.roundTripper.RoundTrip(ctx, req, res)
  80. if err == nil {
  81. break
  82. }
  83. // Invoke retry function to see if another attempt should be made.
  84. if retry, delay := r.fn(err); retry {
  85. time.Sleep(delay)
  86. continue
  87. }
  88. break
  89. }
  90. return err
  91. }