Go to file
Sam Fredrickson ab364c31bb Implement a true priority queue.
* Add a binary max-heap implementation, `binheap`.
* Rename `precise` package to `mq`.
2023-03-01 19:29:15 -08:00
binheap Implement a true priority queue. 2023-03-01 19:29:15 -08:00
circ Initial commit. 2023-02-28 20:33:22 -08:00
mq Implement a true priority queue. 2023-03-01 19:29:15 -08:00
go.mod Implement a true priority queue. 2023-03-01 19:29:15 -08:00
go.sum Implement a true priority queue. 2023-03-01 19:29:15 -08:00
lib_test.go Implement a true priority queue. 2023-03-01 19:29:15 -08:00
lib.go Implement a true priority queue. 2023-03-01 19:29:15 -08:00
LICENSE Initial commit. 2023-02-28 20:33:22 -08:00
README.md Implement a true priority queue. 2023-03-01 19:29:15 -08:00

priorityq - generic prioritized queues in Go

This module was inspired by a reddit post wherein /u/zandery23 asked how to implement a prioritized message queue in Go. A fantastic solution was provided by /u/Ploobers. That's probably right for 99 out of 100 use cases, but it's not completely precise.

Particularly, the second select block does not guarantee that an item from the prioritized queue will be taken if there is also an item in the regular queue.

select {
case job := <-mq.priorityQueue:
    // ...
case job := <-mq.regularQueue:
    // ...
// ...
}

From the Go Language Specification:

If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection.

Thus, it is possible for the second case to be chosen even if the first case is also ready.

The mq package in this module implements a concurrent, prioritized message queue that guarantees receipt of a high-priority items before low-priority ones. This is primarily a fun exercise, I cannot recommend that anyone actually use this in a real project.

Additionally, the root priorityq package implements a concurrent priority queue, using a binary max-heap. This is more general than mq, because it allows multiple levels of priority, instead of just "high" and "low". This, of course, also makes operations slower.