Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant retries for token refreshes as they're already handled in httpclient #729

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ func (c *Config) EnsureResolved() error {
HTTPTimeout: time.Duration(c.HTTPTimeoutSeconds) * time.Second,
Transport: c.HTTPTransport,
ErrorMapper: c.refreshTokenErrorMapper,
TransientErrors: []string{
"throttled",
"too many requests",
"429",
"request limit exceeded",
"rate limit",
},
})
c.resolved = true
return nil
Expand Down
25 changes: 3 additions & 22 deletions config/oauth_visitors.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
package config

import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/databricks/databricks-sdk-go/retries"
"golang.org/x/oauth2"
)

func retriableTokenSource(ctx context.Context, ts oauth2.TokenSource) (*oauth2.Token, error) {
return retries.Poll(ctx, 1*time.Minute, func() (*oauth2.Token, *retries.Err) {
token, err := ts.Token()
if err == nil {
return token, nil
}
var retryKeywords = []string{"throttled", "too many requests", "429", "request limit exceeded", "rate limit"}
for _, retryKeyword := range retryKeywords {
if strings.Contains(err.Error(), retryKeyword) {
return nil, retries.Continue(err)
}
}
return nil, retries.Halt(err)
})
}

// serviceToServiceVisitor returns a visitor that sets the Authorization header to the token from the auth token source
// and the provided secondary header to the token from the secondary token source.
func serviceToServiceVisitor(auth, secondary oauth2.TokenSource, secondaryHeader string) func(r *http.Request) error {
refreshableAuth := oauth2.ReuseTokenSource(nil, auth)
refreshableSecondary := oauth2.ReuseTokenSource(nil, secondary)
return func(r *http.Request) error {
inner, err := retriableTokenSource(r.Context(), refreshableAuth)
inner, err := refreshableAuth.Token()
if err != nil {
return fmt.Errorf("inner token: %w", err)
}
inner.SetAuthHeader(r)

cloud, err := retriableTokenSource(r.Context(), refreshableSecondary)
cloud, err := refreshableSecondary.Token()
if err != nil {
return fmt.Errorf("cloud token: %w", err)
}
Expand All @@ -52,7 +33,7 @@ func serviceToServiceVisitor(auth, secondary oauth2.TokenSource, secondaryHeader
func refreshableVisitor(inner oauth2.TokenSource) func(r *http.Request) error {
refreshableAuth := oauth2.ReuseTokenSource(nil, inner)
return func(r *http.Request) error {
inner, err := retriableTokenSource(r.Context(), refreshableAuth)
inner, err := refreshableAuth.Token()
if err != nil {
return fmt.Errorf("inner token: %w", err)
}
Expand Down
42 changes: 0 additions & 42 deletions config/oauth_visitors_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package config

import (
"context"
"errors"
"testing"
"time"

Expand All @@ -18,46 +16,6 @@ func (m mockTokenSource) Token() (*oauth2.Token, error) {
return m.mockedTokenFunc()
}

func TestOAuthWithRetry(t *testing.T) {
triedOnce := false
mockSource := mockTokenSource{
mockedTokenFunc: func() (*oauth2.Token, error) {
if triedOnce == true {
return nil, errors.New("retried")
}
triedOnce = true
return nil, errors.New("throttled")
},
}
token, err := retriableTokenSource(context.Background(), mockSource)
assert.Nil(t, token)
assert.Contains(t, err.Error(), "retried")
}

func TestOAuthWithoutRetry(t *testing.T) {
mockSource := mockTokenSource{
mockedTokenFunc: func() (*oauth2.Token, error) {
return nil, errors.New("halt")
},
}
token, err := retriableTokenSource(context.Background(), mockSource)
assert.Nil(t, token)
assert.Contains(t, err.Error(), "halt")
}

func TestOAuthWithValidToken(t *testing.T) {
mockSource := mockTokenSource{
mockedTokenFunc: func() (*oauth2.Token, error) {
testToken := oauth2.Token{}
testToken.TokenType = "test"
return &testToken, nil
},
}
token, err := retriableTokenSource(context.Background(), mockSource)
assert.Equal(t, "test", token.TokenType)
assert.NoError(t, err)
}

func TestAzureReuseTokenSource(t *testing.T) {
mockSource := mockTokenSource{
mockedTokenFunc: func() (*oauth2.Token, error) {
Expand Down
Loading