4 ms·
Show HN: Super Simple Worker Queue in Go
- gravypod 10y agoCould you implement this with a linked list underneath to remove the max queue size?
- Xeoncross 10y agoSo a mutex-protected list that feeds the queue?
- gravypod 10y agoOr just have each goroutine read from a shared channel. From what I understand they are thread safe (granted I'm not a go person) and then expose the channel ass the method for adding. So all of the threads just read from the channel and rather then adding to a queue you write to the gochannel they are all reading from.
- Xeoncross 10y agoMaybe I'm missing something, but that is how I'm doing it. The Queue struct has a 'Queue.Jobs chan string' that is the public method for adding to the "Queue" and then the go worker()'s read from that buffered channel. Basically, this simple Queue just adds some niceties around a buffered channel X number of go func() are reading from.
- gravypod 10y agoOk well then can you back it by a linked list? That way there is no size.
- kixpanganiban 10y agoChannels are inherently FIFO queues. So why not just use the usual `select`/`case` method and cast the `struct` you need into a chan with `make(chan, struct)`? See: https://gobyexample.com/non-blocking-channel-operations https://gobyexample.com/non-blocking-channel-operations
- Xeoncross 10y agoAre you talking about doing this instead of buffering the channel? The reason I buffer it is because I don't want whatever is providing the worker with a message to block while waiting to send to the workers.