Skip to content

Commit 4b7a7de

Browse files
authored
Merge pull request #1667 from ydb-platform/with-done
Reverted implementation of internal/xcontext.WithDone
2 parents 359f36c + 96d2ccf commit 4b7a7de

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

internal/xcontext/done.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@ package xcontext
22

33
import (
44
"context"
5+
"time"
56
)
67

8+
type doneCtx <-chan struct{}
9+
10+
func (done doneCtx) Deadline() (deadline time.Time, ok bool) {
11+
return
12+
}
13+
14+
func (done doneCtx) Done() <-chan struct{} {
15+
return done
16+
}
17+
18+
func (done doneCtx) Err() error {
19+
select {
20+
case <-done:
21+
return context.Canceled
22+
default:
23+
return nil
24+
}
25+
}
26+
27+
func (done doneCtx) Value(key any) any {
28+
return nil
29+
}
30+
731
func WithDone(parent context.Context, done <-chan struct{}) (context.Context, context.CancelFunc) {
8-
ctx, cancel := WithCancel(parent)
32+
ctx, cancel := context.WithCancel(parent)
33+
934
select {
1035
case <-done:
1136
cancel()
@@ -14,13 +39,12 @@ func WithDone(parent context.Context, done <-chan struct{}) (context.Context, co
1439
default:
1540
}
1641

17-
go func() {
18-
select {
19-
case <-done:
20-
cancel()
21-
case <-parent.Done():
22-
}
23-
}()
42+
stop := context.AfterFunc(doneCtx(done), func() {
43+
cancel()
44+
})
2445

25-
return ctx, cancel
46+
return ctx, func() {
47+
stop()
48+
cancel()
49+
}
2650
}

internal/xcontext/done_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
)
99

1010
func TestWithDone(t *testing.T) {
11-
t.Run("CancelParent", func(t *testing.T) {
11+
t.Run("WithParentCancel", func(t *testing.T) {
1212
ctx, cancel := context.WithCancel(context.Background())
1313
done := make(chan struct{})
1414
ctx1, _ := WithDone(ctx, done)
1515
require.NoError(t, ctx1.Err())
1616
cancel()
1717
require.Error(t, ctx1.Err())
1818
})
19-
t.Run("StopWithDone", func(t *testing.T) {
19+
t.Run("WithExplicitCancel", func(t *testing.T) {
2020
ctx, cancel := context.WithCancel(context.Background())
2121
defer cancel()
2222
done := make(chan struct{})
@@ -26,7 +26,7 @@ func TestWithDone(t *testing.T) {
2626
require.NoError(t, ctx.Err())
2727
require.Error(t, ctx1.Err())
2828
})
29-
t.Run("CloseDone", func(t *testing.T) {
29+
t.Run("WithExplicitCloseDone", func(t *testing.T) {
3030
done := make(chan struct{})
3131
ctx, cancel := WithDone(context.Background(), done)
3232
require.NoError(t, ctx.Err())
@@ -35,14 +35,14 @@ func TestWithDone(t *testing.T) {
3535
require.Error(t, ctx.Err())
3636
cancel()
3737
})
38-
t.Run("ClosedDone", func(t *testing.T) {
38+
t.Run("WithClosedDone", func(t *testing.T) {
3939
done := make(chan struct{})
4040
close(done)
4141
ctx, cancel := WithDone(context.Background(), done)
4242
require.Error(t, ctx.Err())
4343
cancel()
4444
})
45-
t.Run("NilDone", func(t *testing.T) {
45+
t.Run("WithNilDone", func(t *testing.T) {
4646
var done chan struct{}
4747
ctx, cancel := WithDone(context.Background(), done)
4848
require.NoError(t, ctx.Err())

0 commit comments

Comments
 (0)