I don’t think a ring buffer works. At least in a traditional ring buffer the producer cannot pass the consumer - if the buffer is full the producer blocks or drops. Trying to ensure the consumer always reads the most recent available item is better (and more simply) served with a shared hand off queue.
> On Nov 14, 2020, at 8:48 PM, Kurtis Rader <kra...@skepticism.us> wrote: > > > On Sat, Nov 14, 2020 at 5:43 PM 陶青云 <qingyu...@gmail.com> wrote: >>> Don't use channels for this. It's not what they are for. >>> >>> Ian >> I see some examples using the buffered channel as a job queue. Could you >> give me some advise on this. > > If you search for terms like "go channel job queue" you'll find examples that > at first glance appear relevant to your problem. Such as > https://www.opsdash.com/blog/job-queues-in-go.html. But none of them, AFAICT, > implement the behavior you want. If you don't need compatibility with Go's > `select` statement probably the simplest solution is a ring (circular) > buffer. Search for "go ring buffer" for some examples; although, implementing > a ring buffer is simple enough it can be used as an interview question. So I > leave the details as an exercise for the student. > > You could use a Go channel if you added an explicit mutex that has to be > acquired by both the channel consumer(s) and producer(s) before popping > and/or inserting a value into the channel. But that is suboptimal since the > channel send/receive ops already use an implicit mutex to make those > individual operations concurrent safe. The only purpose of the explicit mutex > is to make it possible for the producer(s) in your example to atomically drop > an "old" entry before inserting a "new" entry. As Ian said a Go channel is > the wrong mechanism for your problem. > > -- > Kurtis Rader > Caretaker of the exceptional canines Junior and Hank > -- > 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/CABx2%3DD-PBF_LCEWy_BGfG-bu1v%2BTNP5m_s_dM9Ld8ducwnY3%3Dg%40mail.gmail.com. -- 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/D276E597-D379-48AD-AA1D-A1154D34739E%40ix.netcom.com.