...

Source file src/k8s.io/client-go/rest/urlbackoff.go

Documentation: k8s.io/client-go/rest

     1  /*
     2  Copyright 2015 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 rest
    18  
    19  import (
    20  	"net/url"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  	"k8s.io/client-go/util/flowcontrol"
    25  	"k8s.io/klog/v2"
    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
    32  var serverIsOverloadedSet = sets.NewInt(429)
    33  var maxResponseCode = 499
    34  
    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)
    39  }
    40  
    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
    46  }
    47  
    48  // NoBackoff is a stub implementation, can be used for mocking or else as a default.
    49  type NoBackoff struct {
    50  }
    51  
    52  func (n *NoBackoff) UpdateBackoff(actualUrl *url.URL, err error, responseCode int) {
    53  	// do nothing.
    54  }
    55  
    56  func (n *NoBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
    57  	return 0 * time.Second
    58  }
    59  
    60  func (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.
    66  func (b *URLBackoff) Disable() {
    67  	klog.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.
    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
    76  	// in the future.
    77  	host, err := url.Parse(rawurl.String())
    78  	if err != nil {
    79  		klog.V(4).Infof("Error extracting url: %v", rawurl)
    80  		panic("bad url!")
    81  	}
    82  	return host.Host
    83  }
    84  
    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())
    90  		return
    91  	} else if responseCode >= 300 || err != nil {
    92  		klog.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.
   101  func (b *URLBackoff) CalculateBackoff(actualUrl *url.URL) time.Duration {
   102  	return b.Backoff.Get(b.baseUrlKey(actualUrl))
   103  }
   104  
   105  func (b *URLBackoff) Sleep(d time.Duration) {
   106  	b.Backoff.Clock.Sleep(d)
   107  }
   108  

View as plain text