Skip to content

Commit ec1a8a5

Browse files
authored
Merge branch 'master' into master
2 parents ca82eca + 506a662 commit ec1a8a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+952
-309
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ linters-settings:
218218
linters:
219219
enable-all: true
220220
disable:
221-
- containedctx
222221
- contextcheck
223222
- cyclop
224223
- depguard

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
* Added type assertion checks to enhance type safety and prevent unexpected panics in critical sections of the codebase
2+
3+
## v3.66.0
4+
* Added experimental package `retry/budget` for limit second and subsequent retry attempts
5+
* Refactored internals for enabling `containedctx` linter
26
* Fixed the hanging semaphore issue on coordination session reconnect
37

48
## v3.65.3

config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config"
1313
"github.com/ydb-platform/ydb-go-sdk/v3/internal/config"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
1617
)
1718

@@ -157,6 +158,13 @@ func WithTrace(t trace.Driver, opts ...trace.DriverComposeOption) Option { //nol
157158
}
158159
}
159160

161+
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
162+
func WithRetryBudget(b budget.Budget) Option {
163+
return func(c *Config) {
164+
config.SetRetryBudget(&c.Common, b)
165+
}
166+
}
167+
160168
func WithTraceRetry(t *trace.Retry, opts ...trace.RetryComposeOption) Option {
161169
return func(c *Config) {
162170
config.SetTraceRetry(&c.Common, t, opts...)

driver.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ var _ Connection = (*Driver)(nil)
5151

5252
// Driver type provide access to YDB service clients
5353
type Driver struct {
54-
ctx context.Context // cancel while Driver.Close called.
5554
ctxCancel context.CancelFunc
5655

5756
userInfo *dsn.UserInfo
@@ -311,7 +310,6 @@ func newConnectionFromOptions(ctx context.Context, opts ...Option) (_ *Driver, e
311310

312311
d := &Driver{
313312
children: make(map[uint64]*Driver),
314-
ctx: ctx,
315313
ctxCancel: driverCtxCancel,
316314
}
317315

examples/topic/topicreader/topicreader_trace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func ExplicitPartitionStartStopHandler(ctx context.Context, db *ydb.Driver) {
4040
) func(
4141
trace.TopicReaderPartitionReadStartResponseDoneInfo,
4242
) {
43-
err := externalSystemLock(info.PartitionContext, info.Topic, info.PartitionID)
43+
err := externalSystemLock(*info.PartitionContext, info.Topic, info.PartitionID)
4444
if err != nil {
4545
stopReader()
4646
}
@@ -105,7 +105,7 @@ func PartitionStartStopHandlerAndOwnReadProgressStorage(ctx context.Context, db
105105
) func(
106106
trace.TopicReaderPartitionReadStartResponseDoneInfo,
107107
) {
108-
err := externalSystemLock(info.PartitionContext, info.Topic, info.PartitionID)
108+
err := externalSystemLock(*info.PartitionContext, info.Topic, info.PartitionID)
109109
if err != nil {
110110
stopReader()
111111
}

internal/background/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919

2020
// A Worker must not be copied after first use
2121
type Worker struct {
22-
ctx context.Context
22+
ctx context.Context //nolint:containedctx
2323
workers sync.WaitGroup
2424
closeReason error
2525
tasksCompleted empty.Chan

internal/backoff/delay.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package backoff
2+
3+
import (
4+
"time"
5+
)
6+
7+
type (
8+
delayOptions struct {
9+
fast Backoff
10+
slow Backoff
11+
}
12+
delayOption func(o *delayOptions)
13+
)
14+
15+
func WithFastBackoff(fast Backoff) delayOption {
16+
return func(o *delayOptions) {
17+
o.fast = fast
18+
}
19+
}
20+
21+
func WithSlowBackoff(slow Backoff) delayOption {
22+
return func(o *delayOptions) {
23+
o.slow = slow
24+
}
25+
}
26+
27+
func Delay(t Type, i int, opts ...delayOption) time.Duration {
28+
optionsHolder := delayOptions{
29+
fast: Fast,
30+
slow: Slow,
31+
}
32+
for _, opt := range opts {
33+
opt(&optionsHolder)
34+
}
35+
switch t {
36+
case TypeFast:
37+
return optionsHolder.fast.Delay(i)
38+
case TypeSlow:
39+
return optionsHolder.slow.Delay(i)
40+
default:
41+
return 0
42+
}
43+
}

internal/backoff/delay_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package backoff
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
10+
)
11+
12+
func TestDelay(t *testing.T) {
13+
for _, tt := range []struct {
14+
name string
15+
act time.Duration
16+
exp time.Duration
17+
}{
18+
{
19+
name: xtest.CurrentFileLine(),
20+
act: Delay(TypeNoBackoff, 0),
21+
exp: 0,
22+
},
23+
{
24+
name: xtest.CurrentFileLine(),
25+
act: Delay(TypeNoBackoff, 1),
26+
exp: 0,
27+
},
28+
{
29+
name: xtest.CurrentFileLine(),
30+
act: Delay(TypeNoBackoff, 2),
31+
exp: 0,
32+
},
33+
{
34+
name: xtest.CurrentFileLine(),
35+
act: Delay(TypeFast, 0, WithFastBackoff(New(
36+
WithSlotDuration(fastSlot),
37+
WithCeiling(6),
38+
WithJitterLimit(1),
39+
))),
40+
exp: 5 * time.Millisecond,
41+
},
42+
{
43+
name: xtest.CurrentFileLine(),
44+
act: Delay(TypeFast, 1, WithFastBackoff(New(
45+
WithSlotDuration(fastSlot),
46+
WithCeiling(6),
47+
WithJitterLimit(1),
48+
))),
49+
exp: 10 * time.Millisecond,
50+
},
51+
{
52+
name: xtest.CurrentFileLine(),
53+
act: Delay(TypeFast, 3, WithFastBackoff(New(
54+
WithSlotDuration(fastSlot),
55+
WithCeiling(6),
56+
WithJitterLimit(1),
57+
))),
58+
exp: 40 * time.Millisecond,
59+
},
60+
{
61+
name: xtest.CurrentFileLine(),
62+
act: Delay(TypeSlow, 0, WithSlowBackoff(New(
63+
WithSlotDuration(slowSlot),
64+
WithCeiling(6),
65+
WithJitterLimit(1),
66+
))),
67+
exp: time.Second,
68+
},
69+
{
70+
name: xtest.CurrentFileLine(),
71+
act: Delay(TypeSlow, 1, WithSlowBackoff(New(
72+
WithSlotDuration(slowSlot),
73+
WithCeiling(6),
74+
WithJitterLimit(1),
75+
))),
76+
exp: 2 * time.Second,
77+
},
78+
{
79+
name: xtest.CurrentFileLine(),
80+
act: Delay(TypeSlow, 3, WithSlowBackoff(New(
81+
WithSlotDuration(slowSlot),
82+
WithCeiling(6),
83+
WithJitterLimit(1),
84+
))),
85+
exp: 8 * time.Second,
86+
},
87+
} {
88+
t.Run(tt.name, func(t *testing.T) {
89+
require.Equal(t, tt.exp, tt.act)
90+
})
91+
}
92+
}

internal/balancer/balancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (b *Balancer) clusterDiscovery(ctx context.Context) (err error) {
8989
},
9090
retry.WithIdempotent(true),
9191
retry.WithTrace(b.driverConfig.TraceRetry()),
92+
retry.WithBudget(b.driverConfig.RetryBudget()),
9293
)
9394
}
9495

internal/config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package config
33
import (
44
"time"
55

6+
"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
67
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
78
)
89

10+
var defaultRetryBudget = budget.Limited(-1)
11+
912
type Common struct {
1013
operationTimeout time.Duration
1114
operationCancelAfter time.Duration
1215
disableAutoRetry bool
1316
traceRetry trace.Retry
17+
retryBudget budget.Budget
1418

1519
panicCallback func(e interface{})
1620
}
@@ -48,6 +52,14 @@ func (c *Common) TraceRetry() *trace.Retry {
4852
return &c.traceRetry
4953
}
5054

55+
func (c *Common) RetryBudget() budget.Budget {
56+
if c.retryBudget == nil {
57+
return defaultRetryBudget
58+
}
59+
60+
return c.retryBudget
61+
}
62+
5163
// SetOperationTimeout define the maximum amount of time a YDB server will process
5264
// an operation. After timeout exceeds YDB will try to cancel operation and
5365
// regardless of the cancellation appropriate error will be returned to
@@ -81,3 +93,7 @@ func SetAutoRetry(c *Common, autoRetry bool) {
8193
func SetTraceRetry(c *Common, t *trace.Retry, opts ...trace.RetryComposeOption) {
8294
c.traceRetry = *c.traceRetry.Compose(t, opts...)
8395
}
96+
97+
func SetRetryBudget(c *Common, b budget.Budget) {
98+
c.retryBudget = b
99+
}

0 commit comments

Comments
 (0)