2 Copyright 2015 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 "k8s.io/apimachinery/pkg/util/clock"
24 "k8s.io/client-go/util/integer"
27 type backoffEntry struct {
35 defaultDuration time.Duration
36 maxDuration time.Duration
37 perItemBackoff map[string]*backoffEntry
40 func NewFakeBackOff(initial, max time.Duration, tc *clock.FakeClock) *Backoff {
42 perItemBackoff: map[string]*backoffEntry{},
44 defaultDuration: initial,
49 func NewBackOff(initial, max time.Duration) *Backoff {
51 perItemBackoff: map[string]*backoffEntry{},
52 Clock: clock.RealClock{},
53 defaultDuration: initial,
58 // Get the current backoff Duration
59 func (p *Backoff) Get(id string) time.Duration {
62 var delay time.Duration
63 entry, ok := p.perItemBackoff[id]
70 // move backoff to the next mark, capping at maxDuration
71 func (p *Backoff) Next(id string, eventTime time.Time) {
74 entry, ok := p.perItemBackoff[id]
75 if !ok || hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
76 entry = p.initEntryUnsafe(id)
78 delay := entry.backoff * 2 // exponential
79 entry.backoff = time.Duration(integer.Int64Min(int64(delay), int64(p.maxDuration)))
81 entry.lastUpdate = p.Clock.Now()
84 // Reset forces clearing of all backoff data for a given key.
85 func (p *Backoff) Reset(id string) {
88 delete(p.perItemBackoff, id)
91 // Returns True if the elapsed time since eventTime is smaller than the current backoff window
92 func (p *Backoff) IsInBackOffSince(id string, eventTime time.Time) bool {
95 entry, ok := p.perItemBackoff[id]
99 if hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
102 return p.Clock.Now().Sub(eventTime) < entry.backoff
105 // Returns True if time since lastupdate is less than the current backoff window.
106 func (p *Backoff) IsInBackOffSinceUpdate(id string, eventTime time.Time) bool {
109 entry, ok := p.perItemBackoff[id]
113 if hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
116 return eventTime.Sub(entry.lastUpdate) < entry.backoff
119 // Garbage collect records that have aged past maxDuration. Backoff users are expected
120 // to invoke this periodically.
121 func (p *Backoff) GC() {
125 for id, entry := range p.perItemBackoff {
126 if now.Sub(entry.lastUpdate) > p.maxDuration*2 {
127 // GC when entry has not been updated for 2*maxDuration
128 delete(p.perItemBackoff, id)
133 func (p *Backoff) DeleteEntry(id string) {
136 delete(p.perItemBackoff, id)
139 // Take a lock on *Backoff, before calling initEntryUnsafe
140 func (p *Backoff) initEntryUnsafe(id string) *backoffEntry {
141 entry := &backoffEntry{backoff: p.defaultDuration}
142 p.perItemBackoff[id] = entry
146 // After 2*maxDuration we restart the backoff factor to the beginning
147 func hasExpired(eventTime time.Time, lastUpdate time.Time, maxDuration time.Duration) bool {
148 return eventTime.Sub(lastUpdate) > maxDuration*2 // consider stable if it's ok for twice the maxDuration