ANN: Priority channels: re-order values sent over a channel

150 views
Skip to first unread message

twp...@gmail.com

unread,
Oct 5, 2024, 8:42:14 PM10/5/24
to golang-nuts
Have you ever wanted a channel that partially sorts the values being passed through it? For example, you have some kind of work queue and you want to do the most important work first. Well, now you can!

github.com/twpayne/go-heap.PriorityChannel implements this: give it a channel and a comparison function and you get back a new channel that returns values in priority order until the original channel is closed.

Only want the highest priority value from the last N? Then use github.com/twpayne/go-heap.BufferedPriorityChannel.

Code is at https://github.com/twpayne/go-heap/blob/main/prioritychannel.go. It's a fun combination of generics, iterators, and channels.

Feedback welcome!

Regards,
Tom

Jason E. Aten

unread,
Oct 6, 2024, 3:58:43 PM10/6/24
to golang-nuts
Hi Tom,

This is an interesting project.

Per your request for feedback: on quick inspection, I would certainly want also a way to close down the priority channels too. 

Some sort of Close() method that would stop all running goroutines from behind-the-scenes, so they do not leak, would be important for any long running process.

Maybe even use of a context.Context to effect the shutdown?

You'll quickly realize that every go channel "raw" operation (not in a select) needs to be wrapped in a select{} in order to not be deadlocked when the time to shutdown arrives (usually on a channel itself). So for instance,


for value := range heap.All() {
outCh <- value
}

would never do. One would need to allow for shutdown, something like:

for value := range heap.All() {
                                               select {
case outCh <- value:
                                                case <- shutdownRequestedCh:
                                                       return // and presumably run other cleanup in a defer at the top of this func
                                                }
}

...and repeat such fixes for all other "raw" channel sends and receives.

Regards,
Jason




twp...@gmail.com

unread,
Oct 7, 2024, 12:47:51 PM10/7/24
to golang-nuts
Thanks Jason, this is great feedback.

I've added a context argument in https://github.com/twpayne/go-heap/pull/6.

Regards,
Tom

Reply all
Reply to author
Forward
0 new messages