Skip to content

Commit dd57752

Browse files
Retry token acquisition on 429, 502, 503, and 504
Expand retriable codes to include 502 (Bad Gateway) and 504 (Gateway Timeout) alongside 429 and 503. These are infrastructure-layer transients from load balancers and API gateways, not application errors. 500 and 501 remain non-retriable as they indicate server bugs or missing endpoints. This matches the retry behavior of Azure SDK, AWS SDK, and Google Cloud client libraries for token endpoints. Co-authored-by: Isaac Signed-off-by: Ubuntu <renaud.hartert@databricks.com>
1 parent ab8549f commit dd57752

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

config/experimental/auth/retrying_token_source.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package auth
33
import (
44
"context"
55
"errors"
6+
"net/http"
67
"net/url"
8+
"slices"
79
"strings"
810
"time"
911

@@ -50,12 +52,19 @@ type httpStatusCoder interface {
5052
HTTPStatusCode() int
5153
}
5254

55+
var retriableCodes = []int{
56+
http.StatusTooManyRequests, // 429
57+
http.StatusBadGateway, // 502
58+
http.StatusServiceUnavailable, // 503
59+
http.StatusGatewayTimeout, // 504
60+
}
61+
5362
// isRetriableTokenError returns true if the error is a transient failure that
5463
// should be retried. This covers HTTP errors from the SDK's transport layer,
5564
// OAuth2 token endpoint errors, and transient network errors.
5665
func isRetriableTokenError(err error) bool {
5766
if code := httpStatusCode(err); code != 0 {
58-
return code == 429 || code == 503
67+
return slices.Contains(retriableCodes, code)
5968
}
6069
var urlErr *url.Error
6170
if errors.As(err, &urlErr) {

config/experimental/auth/retrying_token_source_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,21 @@ func TestIsRetriableTokenError(t *testing.T) {
145145
err: &testHTTPError{code: 500},
146146
want: false,
147147
},
148+
{
149+
name: "http 502",
150+
err: &testHTTPError{code: 502},
151+
want: true,
152+
},
148153
{
149154
name: "http 503",
150155
err: &testHTTPError{code: 503},
151156
want: true,
152157
},
158+
{
159+
name: "http 504",
160+
err: &testHTTPError{code: 504},
161+
want: true,
162+
},
153163
{
154164
name: "http 401",
155165
err: &testHTTPError{code: 401},

0 commit comments

Comments
 (0)