Thanks Jason, this is great feedback. I've added a context argument in https://github.com/twpayne/go-heap/pull/6.
Regards, Tom On Sunday, October 6, 2024 at 9:58:43 PM UTC+2 Jason E. Aten wrote: > 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, > > https://github.com/twpayne/go-heap/blob/v0.0.2/prioritychannel.go#L89 > which says > > 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 > > > > > On Sunday, October 6, 2024 at 1:42:14 AM UTC+1 twp...@gmail.com wrote: > >> 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 >> <https://pkg.go.dev/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 >> <https://pkg.go.dev/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 >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/cf08ad9f-c47d-46b3-8653-7caf80ac31dcn%40googlegroups.com.