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