-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added
ydb.WithOperationTimeout
and ydb.WithOperationCancelAfter
…
… context modifiers
- Loading branch information
1 parent
0f2b757
commit 81caf5d
Showing
12 changed files
with
463 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ydb | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/operation" | ||
) | ||
|
||
// WithOperationTimeout returns a copy of parent context in which YDB operation timeout | ||
// parameter is set to d. If parent context timeout is smaller than d, parent context is returned. | ||
func WithOperationTimeout(ctx context.Context, operationTimeout time.Duration) context.Context { | ||
return operation.WithTimeout(ctx, operationTimeout) | ||
} | ||
|
||
// WithOperationCancelAfter returns a copy of parent context in which YDB operation | ||
// cancel after parameter is set to d. If parent context cancellation timeout is smaller | ||
// than d, parent context is returned. | ||
func WithOperationCancelAfter(ctx context.Context, operationCancelAfter time.Duration) context.Context { | ||
return operation.WithCancelAfter(ctx, operationCancelAfter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package operation | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type ( | ||
ctxOperationTimeoutKey struct{} | ||
ctxOperationCancelAfterKey struct{} | ||
) | ||
|
||
// WithTimeout returns a copy of parent context in which YDB operation timeout | ||
// parameter is set to d. If parent context timeout is smaller than d, parent context is returned. | ||
func WithTimeout(ctx context.Context, operationTimeout time.Duration) context.Context { | ||
if d, ok := Timeout(ctx); ok && operationTimeout >= d { | ||
// The current cancelation timeout is already smaller than the new one. | ||
return ctx | ||
} | ||
return context.WithValue(ctx, ctxOperationTimeoutKey{}, operationTimeout) | ||
} | ||
|
||
// WithCancelAfter returns a copy of parent context in which YDB operation | ||
// cancel after parameter is set to d. If parent context cancellation timeout is smaller | ||
// than d, parent context is returned. | ||
func WithCancelAfter(ctx context.Context, operationCancelAfter time.Duration) context.Context { | ||
if d, ok := CancelAfter(ctx); ok && operationCancelAfter >= d { | ||
// The current cancelation timeout is already smaller than the new one. | ||
return ctx | ||
} | ||
return context.WithValue(ctx, ctxOperationCancelAfterKey{}, operationCancelAfter) | ||
} | ||
|
||
// Timeout returns the timeout within given context after which | ||
// YDB should try to cancel operation and return result regardless of the cancelation. | ||
func Timeout(ctx context.Context) (d time.Duration, ok bool) { | ||
d, ok = ctx.Value(ctxOperationTimeoutKey{}).(time.Duration) | ||
return | ||
} | ||
|
||
// CancelAfter returns the timeout within given context after which | ||
// YDB should try to cancel operation and return result regardless of the cancellation. | ||
func CancelAfter(ctx context.Context) (d time.Duration, ok bool) { | ||
d, ok = ctx.Value(ctxOperationCancelAfterKey{}).(time.Duration) | ||
return | ||
} | ||
|
||
func untilDeadline(ctx context.Context) (time.Duration, bool) { | ||
deadline, ok := ctx.Deadline() | ||
if ok { | ||
return time.Until(deadline), true | ||
} | ||
return 0, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.