Skip to content

Commit 18ee309

Browse files
authored
grpcsync: use context.AfterFunc to close buffer after context canceled in CallbackSerializer (#8489)
[The current minimum supported Go version is now 1.23](https://github.com/grpc/grpc-go/blob/62ec29fd9b3f9ea3cea6dc08a31e837aa92678b7/go.mod#L3). `context.AfterFunc` is available for all of grpc-go's latest version users. Thus we can do this pending TODO. `context.AfterFunc` would invoke the given function for both _immediate_ context cancelation and timer-based context cancelation (`WithTimeout`, `WithDeadline`). So I think this change is safe. RELEASE NOTES: N/A
1 parent 19c720f commit 18ee309

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

internal/grpcsync/callback_serializer.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,11 @@ func (cs *CallbackSerializer) ScheduleOr(f func(ctx context.Context), onFailure
8080
func (cs *CallbackSerializer) run(ctx context.Context) {
8181
defer close(cs.done)
8282

83-
// TODO: when Go 1.21 is the oldest supported version, this loop and Close
84-
// can be replaced with:
85-
//
86-
// context.AfterFunc(ctx, cs.callbacks.Close)
87-
for ctx.Err() == nil {
88-
select {
89-
case <-ctx.Done():
90-
// Do nothing here. Next iteration of the for loop will not happen,
91-
// since ctx.Err() would be non-nil.
92-
case cb := <-cs.callbacks.Get():
93-
cs.callbacks.Load()
94-
cb.(func(context.Context))(ctx)
95-
}
96-
}
97-
98-
// Close the buffer to prevent new callbacks from being added.
99-
cs.callbacks.Close()
83+
// Close the buffer when the context is canceled
84+
// to prevent new callbacks from being added.
85+
context.AfterFunc(ctx, cs.callbacks.Close)
10086

101-
// Run all pending callbacks.
87+
// Run all callbacks.
10288
for cb := range cs.callbacks.Get() {
10389
cs.callbacks.Load()
10490
cb.(func(context.Context))(ctx)

xds/internal/clients/internal/syncutil/callback_serializer.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,11 @@ func (cs *CallbackSerializer) ScheduleOr(f func(ctx context.Context), onFailure
8080
func (cs *CallbackSerializer) run(ctx context.Context) {
8181
defer close(cs.done)
8282

83-
// TODO: when Go 1.21 is the oldest supported version, this loop and Close
84-
// can be replaced with:
85-
//
86-
// context.AfterFunc(ctx, cs.callbacks.Close)
87-
for ctx.Err() == nil {
88-
select {
89-
case <-ctx.Done():
90-
// Do nothing here. Next iteration of the for loop will not happen,
91-
// since ctx.Err() would be non-nil.
92-
case cb := <-cs.callbacks.Get():
93-
cs.callbacks.Load()
94-
cb.(func(context.Context))(ctx)
95-
}
96-
}
97-
98-
// Close the buffer to prevent new callbacks from being added.
99-
cs.callbacks.Close()
83+
// Close the buffer when the context is canceled
84+
// to prevent new callbacks from being added.
85+
context.AfterFunc(ctx, cs.callbacks.Close)
10086

101-
// Run all pending callbacks.
87+
// Run all callbacks.
10288
for cb := range cs.callbacks.Get() {
10389
cs.callbacks.Load()
10490
cb.(func(context.Context))(ctx)

0 commit comments

Comments
 (0)