retry.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "net"
  17. "net/url"
  18. "time"
  19. "github.com/vmware/govmomi/vim25/soap"
  20. )
  21. type RetryFunc func(err error) (retry bool, delay time.Duration)
  22. // TemporaryNetworkError returns a RetryFunc that retries up to a maximum of n
  23. // times, only if the error returned by the RoundTrip function is a temporary
  24. // network error (for example: a connect timeout).
  25. func TemporaryNetworkError(n int) RetryFunc {
  26. return func(err error) (retry bool, delay time.Duration) {
  27. var nerr net.Error
  28. var ok bool
  29. // Never retry if this is not a network error.
  30. switch rerr := err.(type) {
  31. case *url.Error:
  32. if nerr, ok = rerr.Err.(net.Error); !ok {
  33. return false, 0
  34. }
  35. case net.Error:
  36. nerr = rerr
  37. default:
  38. return false, 0
  39. }
  40. if !nerr.Temporary() {
  41. return false, 0
  42. }
  43. // Don't retry if we're out of tries.
  44. if n--; n <= 0 {
  45. return false, 0
  46. }
  47. return true, 0
  48. }
  49. }
  50. type retry struct {
  51. roundTripper soap.RoundTripper
  52. // fn is a custom function that is called when an error occurs.
  53. // It returns whether or not to retry, and if so, how long to
  54. // delay before retrying.
  55. fn RetryFunc
  56. }
  57. // Retry wraps the specified soap.RoundTripper and invokes the
  58. // specified RetryFunc. The RetryFunc returns whether or not to
  59. // retry the call, and if so, how long to wait before retrying. If
  60. // the result of this function is to not retry, the original error
  61. // is returned from the RoundTrip function.
  62. func Retry(roundTripper soap.RoundTripper, fn RetryFunc) soap.RoundTripper {
  63. r := &retry{
  64. roundTripper: roundTripper,
  65. fn: fn,
  66. }
  67. return r
  68. }
  69. func (r *retry) RoundTrip(ctx context.Context, req, res soap.HasFault) error {
  70. var err error
  71. for {
  72. err = r.roundTripper.RoundTrip(ctx, req, res)
  73. if err == nil {
  74. break
  75. }
  76. // Invoke retry function to see if another attempt should be made.
  77. if retry, delay := r.fn(err); retry {
  78. time.Sleep(delay)
  79. continue
  80. }
  81. break
  82. }
  83. return err
  84. }