Skip to content

Commit cb9ddcd

Browse files
authored
Merge pull request #189 from ydb-platform/with-operation-timeout-cancel-after
Added `ydb.WithOperationTimeout` and `ydb.WithOperationCancelAfter`…
2 parents 0f2b757 + 6dbf00e commit cb9ddcd

File tree

16 files changed

+472
-8
lines changed

16 files changed

+472
-8
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v2
1919
with:
20-
version: v1.39
20+
version: v1.45.2

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
* Add go 1.18 to test matrix
1+
* Added `go1.18` to test matrix
2+
* Added `ydb.WithOperationTimeout` and `ydb.WithOperationCancelAfter` context modifiers
23

34
## v3.17.0
45
* Removed redundant `trace.With{Table,Driver,Retry}` and `trace.Context{Table,Driver,Retry}` funcs
5-
* Moved `gtrace` tool from `./cmt/gtrace` to `./internal/cmd/gtrace`
6+
* Moved `gtrace` tool from `./cmd/gtrace` to `./internal/cmd/gtrace`
67
* Refactored `gtrace` tool for generate `Compose` options
78
* Added panic recover on trace calls in `Compose` call step
89
* Added `trace.With{Discovery,Driver,Coordination,Ratelimiter,Table,Scheme,Scripting}PanicCallback` options

context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package ydb
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/operation"
8+
)
9+
10+
// WithOperationTimeout returns a copy of parent context in which YDB operation timeout
11+
// parameter is set to d. If parent context timeout is smaller than d, parent context is returned.
12+
func WithOperationTimeout(ctx context.Context, operationTimeout time.Duration) context.Context {
13+
return operation.WithTimeout(ctx, operationTimeout)
14+
}
15+
16+
// WithOperationCancelAfter returns a copy of parent context in which YDB operation
17+
// cancel after parameter is set to d. If parent context cancellation timeout is smaller
18+
// than d, parent context is returned.
19+
func WithOperationCancelAfter(ctx context.Context, operationCancelAfter time.Duration) context.Context {
20+
return operation.WithCancelAfter(ctx, operationCancelAfter)
21+
}

internal/cmd/gtrace/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func main() {
6969
openFile := func(name string) (*os.File, func()) {
7070
p := filepath.Join(workDir, name)
7171
var f *os.File
72+
// nolint: gofumpt
73+
// nolint: nolintlint
7274
f, err = os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
7375
if err != nil {
7476
log.Fatal(err)

internal/coordination/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (c *client) CreateNode(ctx context.Context, path string, config coordinatio
4242
RateLimiterCountersMode: config.RatelimiterCountersMode.To(),
4343
},
4444
OperationParams: operation.Params(
45+
ctx,
4546
c.config.OperationTimeout(),
4647
c.config.OperationCancelAfter(),
4748
operation.ModeSync,
@@ -65,6 +66,7 @@ func (c *client) AlterNode(ctx context.Context, path string, config coordination
6566
RateLimiterCountersMode: config.RatelimiterCountersMode.To(),
6667
},
6768
OperationParams: operation.Params(
69+
ctx,
6870
c.config.OperationTimeout(),
6971
c.config.OperationCancelAfter(),
7072
operation.ModeSync,
@@ -80,6 +82,7 @@ func (c *client) DropNode(ctx context.Context, path string) (err error) {
8082
&Ydb_Coordination.DropNodeRequest{
8183
Path: path,
8284
OperationParams: operation.Params(
85+
ctx,
8386
c.config.OperationTimeout(),
8487
c.config.OperationCancelAfter(),
8588
operation.ModeSync,
@@ -107,6 +110,7 @@ func (c *client) DescribeNode(
107110
&Ydb_Coordination.DescribeNodeRequest{
108111
Path: path,
109112
OperationParams: operation.Params(
113+
ctx,
110114
c.config.OperationTimeout(),
111115
c.config.OperationCancelAfter(),
112116
operation.ModeSync,

internal/operation/context.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package operation
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
type (
9+
ctxOperationTimeoutKey struct{}
10+
ctxOperationCancelAfterKey struct{}
11+
)
12+
13+
// WithTimeout returns a copy of parent context in which YDB operation timeout
14+
// parameter is set to d. If parent context timeout is smaller than d, parent context is returned.
15+
func WithTimeout(ctx context.Context, operationTimeout time.Duration) context.Context {
16+
if d, ok := Timeout(ctx); ok && operationTimeout >= d {
17+
// The current cancelation timeout is already smaller than the new one.
18+
return ctx
19+
}
20+
return context.WithValue(ctx, ctxOperationTimeoutKey{}, operationTimeout)
21+
}
22+
23+
// WithCancelAfter returns a copy of parent context in which YDB operation
24+
// cancel after parameter is set to d. If parent context cancellation timeout is smaller
25+
// than d, parent context is returned.
26+
func WithCancelAfter(ctx context.Context, operationCancelAfter time.Duration) context.Context {
27+
if d, ok := CancelAfter(ctx); ok && operationCancelAfter >= d {
28+
// The current cancelation timeout is already smaller than the new one.
29+
return ctx
30+
}
31+
return context.WithValue(ctx, ctxOperationCancelAfterKey{}, operationCancelAfter)
32+
}
33+
34+
// Timeout returns the timeout within given context after which
35+
// YDB should try to cancel operation and return result regardless of the cancelation.
36+
func Timeout(ctx context.Context) (d time.Duration, ok bool) {
37+
d, ok = ctx.Value(ctxOperationTimeoutKey{}).(time.Duration)
38+
return
39+
}
40+
41+
// CancelAfter returns the timeout within given context after which
42+
// YDB should try to cancel operation and return result regardless of the cancellation.
43+
func CancelAfter(ctx context.Context) (d time.Duration, ok bool) {
44+
d, ok = ctx.Value(ctxOperationCancelAfterKey{}).(time.Duration)
45+
return
46+
}
47+
48+
func untilDeadline(ctx context.Context) (time.Duration, bool) {
49+
deadline, ok := ctx.Deadline()
50+
if ok {
51+
return time.Until(deadline), true
52+
}
53+
return 0, false
54+
}

internal/operation/params.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package operation
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
78
)
89

910
func Params(
11+
ctx context.Context,
1012
timeout time.Duration,
1113
cancelAfter time.Duration,
1214
mode Mode,
1315
) *Ydb_Operations.OperationParams {
16+
if d, ok := Timeout(ctx); ok {
17+
timeout = d
18+
}
19+
if d, ok := CancelAfter(ctx); ok {
20+
cancelAfter = d
21+
}
22+
if d, ok := untilDeadline(ctx); mode == ModeSync && ok && d < timeout {
23+
timeout = d
24+
}
1425
if timeout == 0 && cancelAfter == 0 && mode == 0 {
1526
return nil
1627
}

0 commit comments

Comments
 (0)