Merge pull request #137694 from Jefftree/client-go-throttle-err

client-go: return context error directly instead of wrapping as rate limiter error
This commit is contained in:
Kubernetes Prow Robot 2026-06-08 23:33:47 +05:30 committed by GitHub
commit 1fd96eb4bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View file

@ -20,6 +20,7 @@ import (
"bytes"
"context"
"encoding/hex"
stderrors "errors"
"fmt"
"io"
"mime"
@ -671,7 +672,8 @@ func (r *Request) tryThrottleWithInfo(ctx context.Context, retryInfo string) err
now := time.Now()
err := r.rateLimiter.Wait(ctx)
if err != nil {
// Don't wrap context errors as caller initiated ctx cancellations is not a rate limiter error.
if err != nil && !stderrors.Is(err, context.Canceled) && !stderrors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("client rate limiter Wait returned an error: %w", err)
}
latency := time.Since(now)

View file

@ -4275,3 +4275,16 @@ func TestRequestWarningHandler(t *testing.T) {
assert.Nil(t, request.warningHandler)
})
}
func TestTryThrottleWithInfoContextDeadline(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
r := &Request{
rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1),
c: &RESTClient{base: &url.URL{Path: "/"}},
}
err := r.tryThrottleWithInfo(ctx, "")
require.ErrorIs(t, err, context.Canceled)
assert.NotContains(t, err.Error(), "client rate limiter")
}