Skip to content

Commit

Permalink
added experimental comment
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Apr 8, 2024
1 parent e3a69fe commit efe3039
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 50 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nol
}
}

// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func WithRetryLimiter(l retry.Limiter) Option {
return func(c *Config) {
config.SetRetryLimiter(&c.Common, l)
Expand Down
46 changes: 13 additions & 33 deletions internal/query/options/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
)

var (
_ DoOption = idempotentOption{}
_ DoOption = labelOption("")
_ DoOption = retryOptionsOption(nil)
_ DoOption = traceOption{}

_ DoTxOption = idempotentOption{}
_ DoTxOption = labelOption("")
_ DoTxOption = retryOptionsOption(nil)
_ DoTxOption = traceOption{}
_ DoTxOption = doTxSettingsOption{}
)
Expand All @@ -36,15 +34,13 @@ type (
txSettings tx.Settings
}

idempotentOption struct{}
labelOption string
traceOption struct {
retryOptionsOption []retry.Option
traceOption struct {
t *trace.Query
}
doTxSettingsOption struct {
txSettings tx.Settings
}
retryOptionsOption []retry.Option
)

func (s *doSettings) Trace() *trace.Query {
Expand All @@ -63,14 +59,6 @@ func (s *doTxSettings) TxSettings() tx.Settings {
return s.txSettings
}

func (opt idempotentOption) applyDoTxOption(s *doTxSettings) {
s.doOpts = append(s.doOpts, opt)
}

func (idempotentOption) applyDoOption(s *doSettings) {
s.retryOpts = append(s.retryOpts, retry.WithIdempotent(true))
}

func (opt traceOption) applyDoOption(s *doSettings) {
s.trace = s.trace.Compose(opt.t)
}
Expand All @@ -79,11 +67,11 @@ func (opt traceOption) applyDoTxOption(s *doTxSettings) {
s.doOpts = append(s.doOpts, opt)
}

func (opt labelOption) applyDoOption(s *doSettings) {
s.retryOpts = append(s.retryOpts, retry.WithLabel(string(opt)))
func (opts retryOptionsOption) applyDoOption(s *doSettings) {

Check failure on line 70 in internal/query/options/retry.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1016: methods on the same type should have the same receiver name (seen 1x "opt", 1x "opts") (stylecheck)
s.retryOpts = append(s.retryOpts, opts...)
}

func (opt labelOption) applyDoTxOption(s *doTxSettings) {
func (opt retryOptionsOption) applyDoTxOption(s *doTxSettings) {

Check warning on line 74 in internal/query/options/retry.go

View workflow job for this annotation

GitHub Actions / golangci-lint

receiver-naming: receiver name opt should be consistent with previous receiver name opts for retryOptionsOption (revive)
s.doOpts = append(s.doOpts, opt)
}

Expand All @@ -95,28 +83,20 @@ func WithTxSettings(txSettings tx.Settings) doTxSettingsOption {
return doTxSettingsOption{txSettings: txSettings}
}

func WithIdempotent() idempotentOption {
return idempotentOption{}
func WithIdempotent() retryOptionsOption {
return []retry.Option{retry.WithIdempotent(true)}
}

func WithLabel(lbl string) labelOption {
return labelOption(lbl)
func WithLabel(lbl string) retryOptionsOption {
return []retry.Option{retry.WithLabel(lbl)}
}

func WithTrace(t *trace.Query) traceOption {
return traceOption{t: t}
}

func (opts retryOptionsOption) applyDoOption(s *doSettings) {
s.retryOpts = append(s.retryOpts, opts...)
}

func (opts retryOptionsOption) applyDoTxOption(s *doTxSettings) {
s.doOpts = append(s.doOpts, opts)
}

func WithRetryOptions(retryOptions ...retry.Option) retryOptionsOption {
return retryOptions
func WithLimiter(l retry.Limiter) retryOptionsOption {
return []retry.Option{retry.WithLimiter(l)}
}

func ParseDoOpts(t *trace.Query, opts ...DoOption) (s *doSettings) {
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ func WithDiscoveryInterval(discoveryInterval time.Duration) Option {
}

// WithRetryLimiter sets retry limiter for all calls of all retryers.
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func WithRetryLimiter(l retry.Limiter) Option {
return func(ctx context.Context, c *Driver) error {
c.options = append(c.options, config.WithRetryLimiter(l))
Expand Down
5 changes: 3 additions & 2 deletions query/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func WithLabel(lbl string) bothDoAndDoTxOption {
return options.WithLabel(lbl)
}

func WithRetryOptions(retryOptions ...retry.Option) bothDoAndDoTxOption {
return options.WithRetryOptions(retryOptions...)
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func WithLimiter(l retry.Limiter) bothDoAndDoTxOption {
return options.WithLimiter(l)
}
5 changes: 5 additions & 0 deletions retry/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
)

var (
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
ErrNoQuota = xerrors.Wrap(errors.New("no retry quota"))

_ Limiter = (*rateLimiter)(nil)
)

type (
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
Limiter interface {
Acquire(ctx context.Context) error
}
Expand All @@ -29,6 +31,7 @@ type (
rateLimiterOption func(q *rateLimiter)
)

// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func Quoter(attemptsPerSecond int, opts ...rateLimiterOption) *rateLimiter {
q := &rateLimiter{
clock: clockwork.NewRealClock(),
Expand Down Expand Up @@ -66,13 +69,15 @@ func Quoter(attemptsPerSecond int, opts ...rateLimiterOption) *rateLimiter {
return q
}

// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func (q *rateLimiter) Stop() {
if q.ticker != nil {
q.ticker.Stop()
}
close(q.done)
}

// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func (q *rateLimiter) Acquire(ctx context.Context) error {
select {
case <-ctx.Done():
Expand Down
2 changes: 2 additions & 0 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func (l limiterOption) ApplyDoTxOption(opts *doTxOptions) {
}

// WithLimiter returns limiter option
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func WithLimiter(l Limiter) limiterOption {
return limiterOption{l: l}
}
Expand Down
21 changes: 10 additions & 11 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,20 @@ func (retryOptions retryOptionsOption) ApplyTableOption(opts *Options) {
opts.RetryOptions = append(opts.RetryOptions, retryOptions...)
}

func WithRetryOptions(retryOptions ...retry.Option) retryOptionsOption {
return retryOptions
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
func WithLimiter(l retry.Limiter) retryOptionsOption {
return []retry.Option{retry.WithLimiter(l)}
}

var _ Option = idempotentOption{}

type idempotentOption struct{}

func (idempotentOption) ApplyTableOption(opts *Options) {
opts.Idempotent = true
opts.RetryOptions = append(opts.RetryOptions, retry.WithIdempotent(true))
// Deprecated: redundant option
// Will be removed after Oct 2024.
// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
func WithRetryOptions(retryOptions []retry.Option) retryOptionsOption {
return retryOptions
}

func WithIdempotent() idempotentOption {
return idempotentOption{}
func WithIdempotent() retryOptionsOption {
return []retry.Option{retry.WithIdempotent(true)}
}

var _ Option = txSettingsOption{}
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/retry_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,26 @@ func TestRetryLimiter(t *testing.T) {
t.Run("db.Table().Do", func(t *testing.T) {
err := nativeDriver.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
return retry.RetryableError(errors.New("custom error"))
}, table.WithRetryOptions(retry.WithLimiter(retryLimiter)))
}, table.WithLimiter(retryLimiter))
require.ErrorIs(t, err, retry.ErrNoQuota)
})
t.Run("db.Table().DoTx", func(t *testing.T) {
err := nativeDriver.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
return retry.RetryableError(errors.New("custom error"))
}, table.WithRetryOptions(retry.WithLimiter(retryLimiter)))
}, table.WithLimiter(retryLimiter))
require.ErrorIs(t, err, retry.ErrNoQuota)
})
if version.Gte(os.Getenv("YDB_VERSION"), "24.1") {
t.Run("db.Query().Do", func(t *testing.T) {
err := nativeDriver.Query().Do(ctx, func(ctx context.Context, s query.Session) error {
return retry.RetryableError(errors.New("custom error"))
}, query.WithRetryOptions(retry.WithLimiter(retryLimiter)))
}, query.WithLimiter(retryLimiter))
require.ErrorIs(t, err, retry.ErrNoQuota)
})
t.Run("db.Query().DoTx", func(t *testing.T) {
err := nativeDriver.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error {
return retry.RetryableError(errors.New("custom error"))
}, query.WithRetryOptions(retry.WithLimiter(retryLimiter)))
}, query.WithLimiter(retryLimiter))
require.ErrorIs(t, err, retry.ErrNoQuota)
})
}
Expand Down

0 comments on commit efe3039

Please sign in to comment.