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

Make it possible to configure maximum retry count #126

Merged
merged 1 commit into from Feb 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions crdb/common.go
Expand Up @@ -62,8 +62,7 @@ func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
return err
}

// TODO(rafi): make the maxRetryCount configurable. Maybe pass it in the context?)
const maxRetries = 50
maxRetries := numRetriesFromContext(ctx)
retryCount := 0
for {
releaseFailed := false
Expand All @@ -90,7 +89,7 @@ func ExecuteInTx(ctx context.Context, tx Tx, fn func() error) (err error) {
}

retryCount++
if retryCount > maxRetries {
if maxRetries > 0 && retryCount > maxRetries {
return newMaxRetriesExceededError(err, maxRetries)
}
}
Expand Down
20 changes: 20 additions & 0 deletions crdb/tx.go
Expand Up @@ -102,6 +102,26 @@ func Execute(fn func() error) (err error) {
}
}

type txConfigKey struct {}

// WithMaxRetries configures context so that ExecuteTx retries tx specified
// number of times when encountering retryable errors.
// Setting retries to 0 will retry indefinitely.
func WithMaxRetries(ctx context.Context, retries int) context.Context {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a basic test to verify something like

ctx = WithMaxRetries(ctx, n)
require.Equal(t, n, numRetriesFromContext(ctx)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return context.WithValue(ctx, txConfigKey{}, retries)
}

const defaultRetries = 50

func numRetriesFromContext(ctx context.Context) int {
if v := ctx.Value(txConfigKey{}); v != nil {
if retries, ok := v.(int); ok && retries >= 0 {
return retries
}
}
return defaultRetries
}

// ExecuteTx runs fn inside a transaction and retries it as needed. On
// non-retryable failures, the transaction is aborted and rolled back; on
// success, the transaction is committed.
Expand Down
13 changes: 13 additions & 0 deletions crdb/tx_test.go
Expand Up @@ -35,6 +35,19 @@ func TestExecuteTx(t *testing.T) {
}
}

// TestConfigureRetries verifies that the number of retries can be specified
// via context.
func TestConfigureRetries(t *testing.T) {
ctx := context.Background()
if numRetriesFromContext(ctx) != defaultRetries {
t.Fatal("expect default number of retries")
}
ctx = WithMaxRetries(context.Background(), 123 + defaultRetries)
if numRetriesFromContext(ctx) != defaultRetries + 123 {
t.Fatal("expected default+123 retires")
}
}

type stdlibWriteSkewTest struct {
db *sql.DB
}
Expand Down