blob: eff848abc12b65896074c498470abf37f92093b0 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package rest
18
19import (
20 "net/url"
21 "time"
22
23 "github.com/golang/glog"
24 "k8s.io/apimachinery/pkg/util/sets"
25 "k8s.io/client-go/util/flowcontrol"
26)
27
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
31// described in
32var serverIsOverloadedSet = sets.NewInt(429)
33var maxResponseCode = 499
34
35type BackoffManager interface {
36 UpdateBackoff(actualUrl *url.URL, err error, responseCode int)
37 CalculateBackoff(actualUrl *url.URL) time.Duration
38 Sleep(d time.Duration)
39}
40
41// URLBackoff struct implements the semantics on top of Backoff which
42// we need for URL specific exponential backoff.
43type URLBackoff struct {
44 // Uses backoff as underlying implementation.
45 Backoff *flowcontrol.Backoff
46}
47
48// NoBackoff is a stub implementation, can be used for mocking or else as a default.
49type NoBackoff struct {
50}
51
52func (n *NoBackoff) UpdateBackoff(actualUrl *url.URL, err error, responseCode int) {
53 // do nothing.
54}
55
56func (n *NoBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
57 return 0 * time.Second
58}
59
60func (n *NoBackoff) Sleep(d time.Duration) {
61 time.Sleep(d)
62}
63
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.
66func (b *URLBackoff) Disable() {
67 glog.V(4).Infof("Disabling backoff strategy")
68 b.Backoff = flowcontrol.NewBackOff(0*time.Second, 0*time.Second)
69}
70
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.
73func (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
76 // in the future.
77 host, err := url.Parse(rawurl.String())
78 if err != nil {
79 glog.V(4).Infof("Error extracting url: %v", rawurl)
80 panic("bad url!")
81 }
82 return host.Host
83}
84
85// UpdateBackoff updates backoff metadata
86func (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())
90 return
91 } else if responseCode >= 300 || err != nil {
92 glog.V(4).Infof("Client is returning errors: code %v, error %v", responseCode, err)
93 }
94
95 //If we got this far, there is no backoff required for this URL anymore.
96 b.Backoff.Reset(b.baseUrlKey(actualUrl))
97}
98
99// CalculateBackoff takes a url and back's off exponentially,
100// based on its knowledge of existing failures.
101func (b *URLBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
102 return b.Backoff.Get(b.baseUrlKey(actualUrl))
103}
104
105func (b *URLBackoff) Sleep(d time.Duration) {
106 b.Backoff.Clock.Sleep(d)
107}