Skip to content

Commit

Permalink
update cancellablewrite.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahrav committed Aug 2, 2023
1 parent 06081d6 commit b47d0db
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ func IsDone(ctx context.Context) bool {
// write would succeed, either operation will be performed randomly.
func CancellableWrite[T any](ctx context.Context, ch chan<- T, item T) error {
select {
case ch <- item:
return nil
case <-ctx.Done():
case <-ctx.Done(): // priority to context cancellation
return ctx.Err()
default:
select {
case <-ctx.Done():
return ctx.Err()
case ch <- item:
return nil
}
}
}

0 comments on commit b47d0db

Please sign in to comment.