Skip to content

Commit

Permalink
Use AfterFunc instead implementing our own
Browse files Browse the repository at this point in the history
  • Loading branch information
ppopth committed Aug 14, 2024
1 parent 0c507ef commit 97fa9b0
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions rpc_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,16 @@ func (q *rpcQueue) Pop(ctx context.Context) (*RPC, error) {
return nil, ErrQueueClosed
}

finished := make(chan struct{})
done := make(chan struct{})
go func() {
select {
case <-finished:
case <-ctx.Done():
// Wake up all the waiting routines. The only routine that correponds
// to this Pop call will return from the function. Note that this can
// be expensive, if there are too many waiting routines.
q.dataAvailable.Broadcast()
done <- struct{}{}
}
}()

defer func() {
// Tell the other routine that this function is finished.
select {
case finished <- struct{}{}:
default:
}
}()
unregisterAfterFunc := context.AfterFunc(ctx, func() {
// Wake up all the waiting routines. The only routine that correponds
// to this Pop call will return from the function. Note that this can
// be expensive, if there are too many waiting routines.
q.dataAvailable.Broadcast()
done <- struct{}{}
})
defer unregisterAfterFunc()

for q.queue.Len() == 0 {
select {
Expand Down

0 comments on commit 97fa9b0

Please sign in to comment.