Skip to content

Commit 24a46c5

Browse files
committed
refactoring of retry.RetryWithResult
1 parent fa56b8c commit 24a46c5

File tree

4 files changed

+59
-36
lines changed

4 files changed

+59
-36
lines changed

internal/query/scanner/named.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ type (
1212
NamedScanner struct {
1313
data *data
1414
}
15-
NamedDestination struct {
15+
namedDestination struct {
1616
name string
1717
ref interface{}
1818
}
19+
NamedDestination interface {
20+
Name() string
21+
Ref() interface{}
22+
}
1923
)
2024

21-
func NamedRef(columnName string, destinationValueReference interface{}) (dst NamedDestination) {
25+
func (dst namedDestination) Name() string {
26+
return dst.name
27+
}
28+
29+
func (dst namedDestination) Ref() interface{} {
30+
return dst.ref
31+
}
32+
33+
func NamedRef(columnName string, destinationValueReference interface{}) (dst namedDestination) {
2234
if columnName == "" {
2335
panic("columnName must be not empty")
2436
}
@@ -40,11 +52,11 @@ func Named(data *data) NamedScanner {
4052

4153
func (s NamedScanner) ScanNamed(dst ...NamedDestination) (err error) {
4254
for i := range dst {
43-
v, err := s.data.seekByName(dst[i].name)
55+
v, err := s.data.seekByName(dst[i].Name())
4456
if err != nil {
4557
return xerrors.WithStackTrace(err)
4658
}
47-
if err = value.CastTo(v, dst[i].ref); err != nil {
59+
if err = value.CastTo(v, dst[i].Ref()); err != nil {
4860
return xerrors.WithStackTrace(err)
4961
}
5062
}

internal/query/scanner/named_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,25 +618,25 @@ func TestNamedRef(t *testing.T) {
618618
{
619619
name: "",
620620
ref: nil,
621-
dst: NamedDestination{},
621+
dst: namedDestination{},
622622
panic: true,
623623
},
624624
{
625625
name: "nil_ref",
626626
ref: nil,
627-
dst: NamedDestination{},
627+
dst: namedDestination{},
628628
panic: true,
629629
},
630630
{
631631
name: "not_ref",
632632
ref: 123,
633-
dst: NamedDestination{},
633+
dst: namedDestination{},
634634
panic: true,
635635
},
636636
{
637637
name: "int_ptr",
638638
ref: func(v int) *int { return &v }(123),
639-
dst: NamedDestination{
639+
dst: namedDestination{
640640
name: "int_ptr",
641641
ref: func(v int) *int { return &v }(123),
642642
},
@@ -649,7 +649,7 @@ func TestNamedRef(t *testing.T) {
649649

650650
return &vv
651651
}(123),
652-
dst: NamedDestination{
652+
dst: namedDestination{
653653
name: "int_dbl_ptr",
654654
ref: func(v int) **int {
655655
vv := &v

query/result.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,31 @@ type (
1717
}
1818
ResultSet interface {
1919
Columns() []string
20-
ColumnTypes() []types.Type
20+
ColumnTypes() []Type
2121
NextRow(ctx context.Context) (Row, error)
2222
}
2323
Row interface {
2424
Scan(dst ...interface{}) error
25-
ScanNamed(dst ...scanner.NamedDestination) error
26-
ScanStruct(dst interface{}, opts ...scanner.ScanStructOption) error
25+
ScanNamed(dst ...NamedDestination) error
26+
ScanStruct(dst interface{}, opts ...ScanStructOption) error
2727
}
28+
Type = types.Type
29+
NamedDestination = scanner.NamedDestination
30+
ScanStructOption = scanner.ScanStructOption
2831
)
2932

30-
func Named(columnName string, destinationValueReference interface{}) (dst scanner.NamedDestination) {
33+
func Named(columnName string, destinationValueReference interface{}) (dst NamedDestination) {
3134
return scanner.NamedRef(columnName, destinationValueReference)
3235
}
3336

34-
func WithScanStructTagName(name string) scanner.ScanStructOption {
37+
func WithScanStructTagName(name string) ScanStructOption {
3538
return scanner.WithTagName(name)
3639
}
3740

38-
func WithScanStructAllowMissingColumnsFromSelect() scanner.ScanStructOption {
41+
func WithScanStructAllowMissingColumnsFromSelect() ScanStructOption {
3942
return scanner.WithAllowMissingColumnsFromSelect()
4043
}
4144

42-
func WithScanStructAllowMissingFieldsInStruct() scanner.ScanStructOption {
45+
func WithScanStructAllowMissingFieldsInStruct() ScanStructOption {
4346
return scanner.WithAllowMissingFieldsInStruct()
4447
}

retry/retry.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
262262
func 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
291291
func 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

394396
func 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

Comments
 (0)