-
Notifications
You must be signed in to change notification settings - Fork 65
Make Config thread-safe using sync.Once #1465
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
Open
renaudhartert-db
wants to merge
6
commits into
main
Choose a base branch
from
renaud-hartert_data/fix-config-race-condition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b951c1f
[Fix] Make Config thread-safe using sync.Once
renaudhartert-db eceff69
Rename authOnce/authErr to resolveAuthOnce/resolveAuthErr
renaudhartert-db 82943f5
Fix comment formatting: add periods to end of sentences
renaudhartert-db 284323e
Fix comment formatting in tests: add periods to end of sentences
renaudhartert-db cb1350a
Update NEXT_CHANGELOG.md for Config race condition fix
renaudhartert-db 9da3d7e
Merge branch 'main' into renaud-hartert_data/fix-config-race-condition
renaudhartert-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/databricks-sdk-go/config/credentials" | ||
| ) | ||
|
|
||
| // TestAuthenticateConcurrency verifies that Config.Authenticate is safe for | ||
| // concurrent use and does not trigger race detector warnings. | ||
| func TestAuthenticateConcurrency(t *testing.T) { | ||
| cfg := &Config{ | ||
| Host: "https://test.cloud.databricks.com", | ||
| Token: "fake-token", | ||
| } | ||
|
|
||
| // Ensure config is resolved first. | ||
| if err := cfg.EnsureResolved(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| const numGoroutines = 10 | ||
|
|
||
| // Launch multiple goroutines that all try to authenticate concurrently. | ||
| for i := 0; i < numGoroutines; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| req, _ := http.NewRequest("GET", "https://test.com", nil) | ||
| // This should not panic or cause data races. | ||
| _ = cfg.Authenticate(req) | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| // Verify authentication happened exactly once. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This verifies that authentication happened atleast once, right? |
||
| if cfg.credentialsProvider == nil { | ||
| t.Error("Expected credentialsProvider to be initialized") | ||
| } | ||
| } | ||
|
|
||
| // TestAuthenticateIfNeededConcurrency tests authenticateIfNeeded directly. | ||
| func TestAuthenticateIfNeededConcurrency(t *testing.T) { | ||
| cfg := &Config{ | ||
| Host: "https://test.cloud.databricks.com", | ||
| Token: "fake-token", | ||
| } | ||
|
|
||
| // Ensure config is resolved first. | ||
| if err := cfg.EnsureResolved(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| const numGoroutines = 100 | ||
|
|
||
| for i := 0; i < numGoroutines; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| _ = cfg.authenticateIfNeeded() | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| // The important thing is that we didn't panic or hit data races. | ||
| } | ||
|
|
||
| // TestAuthenticateOnce verifies that authentication happens exactly once | ||
| // even with concurrent calls. | ||
| func TestAuthenticateOnce(t *testing.T) { | ||
| callCount := 0 | ||
| var mu sync.Mutex | ||
|
|
||
| cfg := &Config{ | ||
| Host: "https://test.cloud.databricks.com", | ||
| Credentials: &testStrategy{ | ||
| configure: func(ctx context.Context, c *Config) (credentials.CredentialsProvider, error) { | ||
| mu.Lock() | ||
| callCount++ | ||
| mu.Unlock() | ||
| return &testProvider{}, nil | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if err := cfg.EnsureResolved(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| const numGoroutines = 50 | ||
|
|
||
| for i := 0; i < numGoroutines; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| _ = cfg.authenticateIfNeeded() | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| mu.Lock() | ||
| count := callCount | ||
| mu.Unlock() | ||
|
|
||
| if count != 1 { | ||
| t.Errorf("Expected Configure to be called exactly once, but was called %d times", count) | ||
| } | ||
| } | ||
|
|
||
| // Test helpers. | ||
| type testStrategy struct { | ||
| configure func(ctx context.Context, c *Config) (credentials.CredentialsProvider, error) | ||
| } | ||
|
|
||
| func (ts *testStrategy) Name() string { | ||
| return "test" | ||
| } | ||
|
|
||
| func (ts *testStrategy) Configure(ctx context.Context, c *Config) (credentials.CredentialsProvider, error) { | ||
| if ts.configure != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy path should ideally not be branched |
||
| return ts.configure(ctx, c) | ||
| } | ||
| return &testProvider{}, nil | ||
| } | ||
|
|
||
| type testProvider struct{} | ||
|
|
||
| func (tp *testProvider) SetHeaders(r *http.Request) error { | ||
| r.Header.Set("Authorization", "Bearer test-token") | ||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a behaviour change from the current implementation. If the credential providers returned an error earlier, it would set
c.credentialsProviderto nil, and theauthenticateIfNeededfunction could be called again. Now, it is no longer an option as thesync.Oncefunction will run exactly once.