Skip to content

Commit

Permalink
feat: TransactionOptions.NonRetriableOnTransientErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayoub Chouak committed Jan 17, 2025
1 parent 5526aba commit c81e4aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions mongo/options/transactionoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type TransactionOptions struct {
// be used in its place to control the amount of time that a single operation can run before returning an error.
// MaxCommitTime is ignored if Timeout is set on the client.
MaxCommitTime *time.Duration

// NonRetryableOnTransientErrors indicates whether the transaction should not be retried on transient errors.
NonRetryableOnTransientErrors bool
}

// Transaction creates a new TransactionOptions instance.
Expand Down
16 changes: 12 additions & 4 deletions mongo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,16 @@ func (s *sessionImpl) EndSession(ctx context.Context) {
}

// WithTransaction implements the Session interface.
func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionContext) (interface{}, error),
opts ...*options.TransactionOptions) (interface{}, error) {
func (s *sessionImpl) WithTransaction(
ctx context.Context,
fn func(ctx SessionContext) (interface{}, error),
opts ...*options.TransactionOptions,
) (interface{}, error) {
var options options.TransactionOptions
if len(opts) > 0 && opts[0] != nil {
options = *opts[0]
}

timeout := time.NewTimer(withTransactionTimeout)
defer timeout.Stop()
var err error
Expand All @@ -202,7 +210,7 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionCo
default:
}

if errorHasLabel(err, driver.TransientTransactionError) {
if !options.NonRetryableOnTransientErrors && errorHasLabel(err, driver.TransientTransactionError) {
continue
}
return res, err
Expand Down Expand Up @@ -247,7 +255,7 @@ func (s *sessionImpl) WithTransaction(ctx context.Context, fn func(ctx SessionCo
if cerr.HasErrorLabel(driver.UnknownTransactionCommitResult) && !cerr.IsMaxTimeMSExpiredError() {
continue
}
if cerr.HasErrorLabel(driver.TransientTransactionError) {
if !options.NonRetryableOnTransientErrors && cerr.HasErrorLabel(driver.TransientTransactionError) {
break CommitLoop
}
}
Expand Down

0 comments on commit c81e4aa

Please sign in to comment.