Skip to content

Commit 1851bdc

Browse files
committed
add context to BlockingPop()
1 parent b0bb444 commit 1851bdc

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

kpq/keyed_priority_queue.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package kpq
99

1010
import (
11+
"context"
1112
"fmt"
1213
"sync"
1314
)
@@ -143,15 +144,21 @@ func (pq *KeyedPriorityQueue[K, V]) Pop() (K, V, bool) {
143144
}
144145

145146
// BlockingPop removes and returns the highest priority key and value from the priority queue.
146-
// In case queue is empty, it blocks until next Push happens.
147-
func (pq *KeyedPriorityQueue[K, V]) BlockingPop() (K, V) {
147+
// In case queue is empty, it blocks until next Push happens or ctx is closed.
148+
func (pq *KeyedPriorityQueue[K, V]) BlockingPop(ctx context.Context) (K, V) {
148149
pq.mu.Lock()
149150

150151
if len(pq.pm) == 0 {
151152
pq.mu.Unlock()
152153

153-
pair := <-pq.fastTrack
154-
return pair.k, pair.v
154+
select {
155+
case <-ctx.Done():
156+
var k K
157+
var v V
158+
return k, v
159+
case pair := <-pq.fastTrack:
160+
return pair.k, pair.v
161+
}
155162
}
156163

157164
defer pq.mu.Unlock()

0 commit comments

Comments
 (0)