@@ -260,7 +260,7 @@ func WithPanicCallback(panicCallback func(e interface{})) panicCallbackOption {
260260//
261261// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
262262func Retry (ctx context.Context , op retryOperation , opts ... Option ) (finalErr error ) {
263- _ , err := RetryWithResult [struct {}](ctx , func (ctx context.Context ) (* struct {}, error ) {
263+ _ , err := RetryWithResult [* struct {}](ctx , func (ctx context.Context ) (* struct {}, error ) {
264264 err := op (ctx )
265265 if err != nil {
266266 return nil , xerrors .WithStackTrace (err )
@@ -289,15 +289,18 @@ func Retry(ctx context.Context, op retryOperation, opts ...Option) (finalErr err
289289//
290290//nolint:funlen
291291func RetryWithResult [T any ](ctx context.Context , //nolint:revive
292- op func (context.Context ) (* T , error ), opts ... Option ,
293- ) (v * T , finalErr error ) {
294- options := & retryOptions {
295- call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/retry.RetryWithResult" ),
296- trace : & trace.Retry {},
297- budget : budget .Limited (- 1 ),
298- fastBackoff : backoff .Fast ,
299- slowBackoff : backoff .Slow ,
300- }
292+ op func (context.Context ) (T , error ), opts ... Option ,
293+ ) (_ T , finalErr error ) {
294+ var (
295+ zeroValue T
296+ options = & retryOptions {
297+ call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/retry.RetryWithResult" ),
298+ trace : & trace.Retry {},
299+ budget : budget .Limited (- 1 ),
300+ fastBackoff : backoff .Fast ,
301+ slowBackoff : backoff .Slow ,
302+ }
303+ )
301304 for _ , opt := range opts {
302305 if opt != nil {
303306 opt .ApplyRetryOption (options )
@@ -332,13 +335,12 @@ func RetryWithResult[T any](ctx context.Context, //nolint:revive
332335 attempts ++
333336 select {
334337 case <- ctx .Done ():
335- return nil , xerrors .WithStackTrace (
338+ return zeroValue , xerrors .WithStackTrace (
336339 fmt .Errorf ("retry failed on attempt No.%d: %w" , attempts , ctx .Err ()),
337340 )
338341
339342 default :
340- var err error
341- v , err = opWithRecover (ctx , options , op )
343+ v , err := opWithRecover (ctx , options , op )
342344
343345 if err == nil {
344346 return v , nil
@@ -353,7 +355,7 @@ func RetryWithResult[T any](ctx context.Context, //nolint:revive
353355 code = m .StatusCode ()
354356
355357 if ! m .MustRetry (options .idempotent ) {
356- return nil , xerrors .WithStackTrace (
358+ return zeroValue , xerrors .WithStackTrace (
357359 fmt .Errorf ("non-retryable error occurred on attempt No.%d (idempotent=%v): %w" ,
358360 attempts , options .idempotent , err ),
359361 )
@@ -368,7 +370,7 @@ func RetryWithResult[T any](ctx context.Context, //nolint:revive
368370 case <- ctx .Done ():
369371 t .Stop ()
370372
371- return nil , xerrors .WithStackTrace (
373+ return zeroValue , xerrors .WithStackTrace (
372374 xerrors .Join (
373375 fmt .Errorf ("attempt No.%d: %w" , attempts , ctx .Err ()),
374376 err ,
@@ -378,7 +380,7 @@ func RetryWithResult[T any](ctx context.Context, //nolint:revive
378380 t .Stop ()
379381
380382 if acquireErr := options .budget .Acquire (ctx ); acquireErr != nil {
381- return nil , xerrors .WithStackTrace (
383+ return zeroValue , xerrors .WithStackTrace (
382384 xerrors .Join (
383385 fmt .Errorf ("attempt No.%d: %w" , attempts , budget .ErrNoQuota ),
384386 acquireErr ,
@@ -392,20 +394,26 @@ func RetryWithResult[T any](ctx context.Context, //nolint:revive
392394}
393395
394396func opWithRecover [T any ](ctx context.Context ,
395- options * retryOptions , op func (context.Context ) (* T , error ),
396- ) (_ * T , err error ) {
397+ options * retryOptions , op func (context.Context ) (T , error ),
398+ ) (_ T , finalErr error ) {
399+ var zeroValue T
397400 if options .panicCallback != nil {
398401 defer func () {
399402 if e := recover (); e != nil {
400403 options .panicCallback (e )
401- err = xerrors .WithStackTrace (
404+ finalErr = xerrors .WithStackTrace (
402405 fmt .Errorf ("panic recovered: %v" , e ),
403406 )
404407 }
405408 }()
406409 }
407410
408- return op (ctx )
411+ v , err := op (ctx )
412+ if err != nil {
413+ return zeroValue , xerrors .WithStackTrace (err )
414+ }
415+
416+ return v , nil
409417}
410418
411419// Check returns retry mode for queryErr.
0 commit comments