-
Notifications
You must be signed in to change notification settings - Fork 104
Topics: extend connectTimeout to cover InitRequest + InitResponse
#1922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kprokopenko
wants to merge
10
commits into
master
Choose a base branch
from
topic_connect_timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d43f412
connectTimeout include initStream
kprokopenko 6ba6e72
Merge branch 'master' into topic_connect_timeout
kprokopenko 7c9e1e1
add changelog
kprokopenko 6374144
fix lint
kprokopenko 1c1be1c
fix
kprokopenko c872b71
Merge branch 'master' into topic_connect_timeout
kprokopenko a7140ed
fix from review
kprokopenko 41eab2a
Merge branch 'master' into topic_connect_timeout
kprokopenko 2847689
fix from review
kprokopenko 3776dde
Merge branch 'master' into topic_connect_timeout
kprokopenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| package xcontext | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| ) | ||
|
|
||
| // WithStoppableTimeoutCause returns a copy of the parent context that is cancelled with | ||
| // the specified cause after timeout elapses, and a stop function. Calling the stop function | ||
| // prevents the timeout from canceling the context and releases resources associated with it. | ||
| // The cause error will be used when the timeout triggers cancellation. | ||
| // | ||
| // The returned stop function returns a boolean value: | ||
| // - true if the timeout was successfully stopped before it fired (context was not cancelled by timeout) | ||
| // - false if the timeout already fired and the context was cancelled with the specified cause | ||
| func WithStoppableTimeoutCause(ctx context.Context, timeout time.Duration, cause error) (context.Context, func() bool) { | ||
| ctxWithCancel, cancel := context.WithCancelCause(ctx) | ||
| timeoutCtx, cancelTimeout := WithTimeout(ctx, timeout) | ||
|
|
||
| stop := context.AfterFunc(timeoutCtx, func() { cancel(cause) }) | ||
|
|
||
| return ctxWithCancel, func() bool { | ||
| defer cancelTimeout() | ||
|
|
||
| return stop() | ||
| } | ||
| } |
This file contains hidden or 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,67 @@ | ||
| //go:build go1.25 | ||
|
|
||
| package xcontext_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
| "testing/synctest" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext" | ||
| ) | ||
|
|
||
| func TestWithStoppableTimeoutCause(t *testing.T) { | ||
| wantErr := errors.New("some error") | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| ctx, _ := xcontext.WithStoppableTimeoutCause(context.Background(), 10*time.Second, wantErr) | ||
| select { | ||
| case <-time.After(100500 * time.Second): | ||
| t.Fatal("context should be done") | ||
| case <-ctx.Done(): | ||
| assert.ErrorIs(t, context.Cause(ctx), wantErr) | ||
| } | ||
| }) | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| ctx, stop := xcontext.WithStoppableTimeoutCause(context.Background(), 10*time.Second, wantErr) | ||
|
|
||
| stop() | ||
|
|
||
| select { | ||
| case <-time.After(100500 * time.Second): | ||
| case <-ctx.Done(): | ||
| t.Fatal("context shouldn't be canceled") | ||
| } | ||
| }) | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| _, stop := xcontext.WithStoppableTimeoutCause(context.Background(), 10*time.Second, wantErr) | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
|
|
||
| assert.True(t, stop()) | ||
| }) | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| _, stop := xcontext.WithStoppableTimeoutCause(context.Background(), 10*time.Second, wantErr) | ||
|
|
||
| time.Sleep(1 * time.Second) | ||
|
|
||
| stop() | ||
|
|
||
| assert.False(t, stop()) | ||
| }) | ||
|
|
||
| synctest.Test(t, func(t *testing.T) { | ||
| _, stop := xcontext.WithStoppableTimeoutCause(context.Background(), 10*time.Second, wantErr) | ||
|
|
||
| time.Sleep(15 * time.Second) | ||
|
|
||
| assert.False(t, stop()) | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вот тут будет утечка cancel-контекстов при ошибках.
Это может быть стать проблемой при пропадании сети - когда мы быстро перебираем все endpoint-ы по-очереди многократно в попытках до кого-то достучаться, особенно если таймаут - вечный