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/sets"
24 "k8s.io/client-go/util/flowcontrol"
28 // Set of resp. Codes that we backoff for.
29 // In general these should be errors that indicate a server is overloaded.
30 // These shouldn't be configured by any user, we set them based on conventions
32 var serverIsOverloadedSet = sets.NewInt(429)
33 var maxResponseCode = 499
35 type BackoffManager interface {
36 UpdateBackoff(actualUrl *url.URL, err error, responseCode int)
37 CalculateBackoff(actualUrl *url.URL) time.Duration
38 Sleep(d time.Duration)
41 // URLBackoff struct implements the semantics on top of Backoff which
42 // we need for URL specific exponential backoff.
43 type URLBackoff struct {
44 // Uses backoff as underlying implementation.
45 Backoff *flowcontrol.Backoff
48 // NoBackoff is a stub implementation, can be used for mocking or else as a default.
49 type NoBackoff struct {
52 func (n *NoBackoff) UpdateBackoff(actualUrl *url.URL, err error, responseCode int) {
56 func (n *NoBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
57 return 0 * time.Second
60 func (n *NoBackoff) Sleep(d time.Duration) {
64 // Disable makes the backoff trivial, i.e., sets it to zero. This might be used
65 // by tests which want to run 1000s of mock requests without slowing down.
66 func (b *URLBackoff) Disable() {
67 klog.V(4).Infof("Disabling backoff strategy")
68 b.Backoff = flowcontrol.NewBackOff(0*time.Second, 0*time.Second)
71 // baseUrlKey returns the key which urls will be mapped to.
72 // For example, 127.0.0.1:8080/api/v2/abcde -> 127.0.0.1:8080.
73 func (b *URLBackoff) baseUrlKey(rawurl *url.URL) string {
74 // Simple implementation for now, just the host.
75 // We may backoff specific paths (i.e. "pods") differentially
77 host, err := url.Parse(rawurl.String())
79 klog.V(4).Infof("Error extracting url: %v", rawurl)
85 // UpdateBackoff updates backoff metadata
86 func (b *URLBackoff) UpdateBackoff(actualUrl *url.URL, err error, responseCode int) {
87 // range for retry counts that we store is [0,13]
88 if responseCode > maxResponseCode || serverIsOverloadedSet.Has(responseCode) {
89 b.Backoff.Next(b.baseUrlKey(actualUrl), b.Backoff.Clock.Now())
91 } else if responseCode >= 300 || err != nil {
92 klog.V(4).Infof("Client is returning errors: code %v, error %v", responseCode, err)
95 //If we got this far, there is no backoff required for this URL anymore.
96 b.Backoff.Reset(b.baseUrlKey(actualUrl))
99 // CalculateBackoff takes a url and back's off exponentially,
100 // based on its knowledge of existing failures.
101 func (b *URLBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
102 return b.Backoff.Get(b.baseUrlKey(actualUrl))
105 func (b *URLBackoff) Sleep(d time.Duration) {
106 b.Backoff.Clock.Sleep(d)