1 /* 2 Copyright 2017 The Kubernetes Authors. 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 ipam 18 19 import ( 20 "time" 21 ) 22 23 // Timeout manages the resync loop timing for a given node sync operation. The 24 // timeout changes depending on whether or not there was an error reported for 25 // the operation. Consecutive errors will result in exponential backoff to a 26 // maxBackoff timeout. 27 type Timeout struct { 28 // Resync is the default timeout duration when there are no errors. 29 Resync time.Duration 30 // MaxBackoff is the maximum timeout when in a error backoff state. 31 MaxBackoff time.Duration 32 // InitialRetry is the initial retry interval when an error is reported. 33 InitialRetry time.Duration 34 35 // errs is the count of consecutive errors that have occurred. 36 errs int 37 // current is the current backoff timeout. 38 current time.Duration 39 } 40 41 // Update the timeout with the current error state. 42 func (b *Timeout) Update(ok bool) { 43 if ok { 44 b.errs = 0 45 b.current = b.Resync 46 return 47 } 48 49 b.errs++ 50 if b.errs == 1 { 51 b.current = b.InitialRetry 52 return 53 } 54 55 b.current *= 2 56 if b.current >= b.MaxBackoff { 57 b.current = b.MaxBackoff 58 } 59 } 60 61 // Next returns the next operation timeout given the disposition of err. 62 func (b *Timeout) Next() time.Duration { 63 if b.errs == 0 { 64 return b.Resync 65 } 66 return b.current 67 } 68