-
Notifications
You must be signed in to change notification settings - Fork 607
fix: add retries to getKey operation #3825
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd97dc8
fix: add retries to getKey operation
Flo4604 b2195fd
add retries to important operations
Flo4604 0f9c231
fmt
Flo4604 8f8a5be
fmt
Flo4604 3ee9ea8
fmt
Flo4604 e86f734
use require
Flo4604 6f300a8
adjust for comments
Flo4604 ca88e57
Merge branch 'main' into feat/db_retries
Flo4604 c8aca2a
Merge branch 'main' into feat/db_retries
Flo4604 def1fa6
Merge branch 'main' into feat/db_retries
Flo4604 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
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,67 @@ | ||
| package db | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/unkeyed/unkey/go/pkg/retry" | ||
| ) | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const ( | ||
| // DefaultBackoff is the base duration for exponential backoff in database retries | ||
| DefaultBackoff = 50 * time.Millisecond | ||
| ) | ||
|
|
||
| // WithRetry executes a database operation with optimized retry configuration. | ||
| // It retries transient errors with exponential backoff but skips non-retryable errors | ||
| // like "not found" or "duplicate key" to avoid unnecessary delays. | ||
| // | ||
| // Configuration: | ||
| // - 3 attempts maximum | ||
| // - Exponential backoff: 50ms, 100ms, 200ms | ||
| // - Skips retries for "not found" and "duplicate key" errors | ||
| // | ||
| // Usage: | ||
| // | ||
| // result, err := db.WithRetry(func() (SomeType, error) { | ||
| // return db.Query.SomeOperation(ctx, db.RO(), params) | ||
| // }) | ||
| func WithRetry[T any](fn func() (T, error)) (T, error) { | ||
| retrier := retry.New( | ||
| retry.Attempts(3), | ||
| retry.Backoff(func(n int) time.Duration { | ||
| // Predefined backoff delays: 50ms, 100ms, 200ms | ||
| delays := []time.Duration{ | ||
| DefaultBackoff, // 50ms for attempt 1 | ||
| DefaultBackoff * 2, // 100ms for attempt 2 | ||
| DefaultBackoff * 4, // 200ms for attempt 3 | ||
| } | ||
| if n <= 0 || n > len(delays) { | ||
| return DefaultBackoff // fallback to base delay | ||
| } | ||
| return delays[n-1] | ||
| }), | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| retry.ShouldRetry(func(err error) bool { | ||
| // Don't retry if resource is not found - this is a valid response | ||
| if IsNotFound(err) { | ||
| return false | ||
| } | ||
|
|
||
| // Don't retry duplicate key errors - these won't succeed on retry | ||
| if IsDuplicateKeyError(err) { | ||
| return false | ||
| } | ||
|
|
||
| // Retry all other errors (network issues, timeouts, deadlocks, etc.) | ||
| return true | ||
| }), | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| var result T | ||
| err := retrier.Do(func() error { | ||
Flo4604 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var retryErr error | ||
| result, retryErr = fn() | ||
| return retryErr | ||
| }) | ||
|
|
||
| return result, err | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.