Skip to content

Commit 63560a5

Browse files
authored
Merge pull request #880 from ydb-platform/retry
finalErr in retry
2 parents 1ce8fb7 + 9b6db46 commit 63560a5

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

retry/retry.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

77
"github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff"
8+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/wait"
910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
1011
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -217,11 +218,12 @@ func isRetryCalledAbove(ctx context.Context) bool {
217218
// Warning: if deadline without deadline or cancellation func Retry will be worked infinite
218219
//
219220
// If you need to retry your op func on some logic errors - you must return RetryableError() from retryOperation
220-
func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) {
221+
func Retry(ctx context.Context, op retryOperation, opts ...Option) (finalErr error) {
221222
options := &retryOptions{
223+
trace: &trace.Retry{},
222224
fastBackoff: backoff.Fast,
223225
slowBackoff: backoff.Slow,
224-
trace: &trace.Retry{},
226+
label: stack.Record(1, stack.Lambda(false), stack.FileName(false)),
225227
}
226228
for _, opt := range opts {
227229
if opt != nil {
@@ -230,8 +232,8 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) {
230232
}
231233
ctx = xcontext.WithIdempotent(ctx, options.idempotent)
232234
defer func() {
233-
if err != nil && options.stackTrace {
234-
err = xerrors.WithStackTrace(err,
235+
if finalErr != nil && options.stackTrace {
236+
finalErr = xerrors.WithStackTrace(finalErr,
235237
xerrors.WithSkipDepth(2), // 1 - exit from defer, 1 - exit from Retry call
236238
)
237239
}
@@ -246,7 +248,7 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) {
246248
)
247249
)
248250
defer func() {
249-
onIntermediate(err)(attempts, err)
251+
onIntermediate(finalErr)(attempts, finalErr)
250252
}()
251253
for {
252254
i++
@@ -260,7 +262,7 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) {
260262
)
261263

262264
default:
263-
err = func() (err error) {
265+
err := func() (err error) {
264266
if options.panicCallback != nil {
265267
defer func() {
266268
if e := recover(); e != nil {
@@ -295,10 +297,8 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (err error) {
295297

296298
if !m.MustRetry(options.idempotent) {
297299
return xerrors.WithStackTrace(
298-
xerrors.Join(
299-
fmt.Errorf("non-retryable error occurred on attempt No.%d (idempotent=%v)",
300-
attempts, options.idempotent,
301-
), err,
300+
fmt.Errorf("non-retryable error occurred on attempt No.%d (idempotent=%v): %w",
301+
attempts, options.idempotent, err,
302302
),
303303
)
304304
}

retry/sql.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Do(ctx context.Context, db *sql.DB, f func(ctx context.Context, cc *sql.Con
5353
opt.ApplyDoOption(&options)
5454
}
5555
}
56-
err := Retry(ctx, func(ctx context.Context) (err error) {
56+
err := Retry(ctx, func(ctx context.Context) error {
5757
attempts++
5858
cc, err := db.Conn(ctx)
5959
if err != nil {
@@ -140,24 +140,24 @@ func DoTx(ctx context.Context, db *sql.DB, f func(context.Context, *sql.Tx) erro
140140
opt.ApplyDoTxOption(&options)
141141
}
142142
}
143-
err := Retry(ctx, func(ctx context.Context) (err error) {
143+
err := Retry(ctx, func(ctx context.Context) (finalErr error) {
144144
attempts++
145145
tx, err := db.BeginTx(ctx, options.txOptions)
146146
if err != nil {
147147
return unwrapErrBadConn(xerrors.WithStackTrace(err))
148148
}
149149
defer func() {
150-
if err != nil {
151-
errRollback := tx.Rollback()
152-
if errRollback != nil {
153-
err = xerrors.NewWithIssues("",
154-
xerrors.WithStackTrace(err),
155-
xerrors.WithStackTrace(errRollback),
156-
)
157-
} else {
158-
err = xerrors.WithStackTrace(err)
159-
}
150+
if finalErr == nil {
151+
return
160152
}
153+
errRollback := tx.Rollback()
154+
if errRollback == nil {
155+
return
156+
}
157+
finalErr = xerrors.NewWithIssues("",
158+
xerrors.WithStackTrace(finalErr),
159+
xerrors.WithStackTrace(fmt.Errorf("rollback failed: %w", errRollback)),
160+
)
161161
}()
162162
if err = f(ctx, tx); err != nil {
163163
return unwrapErrBadConn(xerrors.WithStackTrace(err))

0 commit comments

Comments
 (0)