client-go/rest: backoff with context support

The BackoffManager interface sleeps without considering the caller's context,
i.e. cancellation is not supported. This alone is reason enough to deprecate it
and to replace it with an interface that supports a context parameter.

The other reason is that contextual logging needs that parameter.

Kubernetes-commit: b15a1943d51adfb8c5e0185d58d25e038c3d6ade
This commit is contained in:
Patrick Ohly
2024-09-02 20:18:47 +02:00
committed by Kubernetes Publisher
parent 2b2015d460
commit 7aa9904196
11 changed files with 421 additions and 57 deletions

View File

@@ -106,7 +106,7 @@ type Request struct {
warningHandler WarningHandlerWithContext
rateLimiter flowcontrol.RateLimiter
backoff BackoffManager
backoff BackoffManagerWithContext
timeout time.Duration
maxRetries int
@@ -136,7 +136,7 @@ type Request struct {
// NewRequest creates a new request helper object for accessing runtime.Objects on a server.
func NewRequest(c *RESTClient) *Request {
var backoff BackoffManager
var backoff BackoffManagerWithContext
if c.createBackoffMgr != nil {
backoff = c.createBackoffMgr()
}
@@ -259,13 +259,27 @@ func (r *Request) Resource(resource string) *Request {
}
// BackOff sets the request's backoff manager to the one specified,
// or defaults to the stub implementation if nil is provided
// or defaults to the stub implementation if nil is provided.
//
// Deprecated: BackoffManager.Sleep ignores the caller's context. Use BackOffWithContext and BackoffManagerWithContext instead.
func (r *Request) BackOff(manager BackoffManager) *Request {
if manager == nil {
r.backoff = &NoBackoff{}
return r
}
r.backoff = &backoffManagerNopContext{BackoffManager: manager}
return r
}
// BackOffWithContext sets the request's backoff manager to the one specified,
// or defaults to the stub implementation if nil is provided.
func (r *Request) BackOffWithContext(manager BackoffManagerWithContext) *Request {
if manager == nil {
r.backoff = &NoBackoff{}
return r
}
r.backoff = manager
return r
}