Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread Aleksey Tulinov
If you're looking for efficiency, try to sync on a single mutex. The best kind of syncing is the one that didn't happen, depending on your circumstances, pthreads mutex can beat channels even taking into account context switching because syncing on channel isn't free either. Implementing thread-sa

Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread Robert Engels
If any can be dropped than clearly all are not needed. What is needed differs greatly if you have a billion events versus 10. Without more information on the system you cannot create an appropriate design. It is my guess that a simple hand off queue will suffice in this case. (Otherwise you nee

Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread Kurtis Rader
On Sun, Nov 15, 2020 at 7:21 PM Robert Engels wrote: > I will argue if it is ok to drop the oldest then it is ok to drop all but > the latest. You need more metrics on volume and latency requirements to > know which is the proper approach. > If true your solution implies the O.P.'s question is a

Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread Robert Engels
I will argue if it is ok to drop the oldest then it is ok to drop all but the latest. You need more metrics on volume and latency requirements to know which is the proper approach. > On Nov 15, 2020, at 8:01 PM, Kurtis Rader wrote: > >  >> On Sun, Nov 15, 2020 at 3:45 PM 陶青云 wrote: > >> >

Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread Kurtis Rader
On Sun, Nov 15, 2020 at 3:45 PM 陶青云 wrote: > > 在2020年11月15日星期日 UTC+8 下午12:19:07 写道: > >> On Sat, Nov 14, 2020 at 7:54 PM Robert Engels >> wrote: >> >>> 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 p

Re: [go-nuts] How to drop old value in a channel salety

2020-11-15 Thread 陶青云
在2020年11月15日星期日 UTC+8 下午12:19:07 写道: > On Sat, Nov 14, 2020 at 7:54 PM Robert Engels > wrote: > >> 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 co

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Tamás Gulácsi
If you read more than write, then you can use a sync.RWMutex an RLock when reading, Lock when writing. Kurtis Rader a következőt írta (2020. november 15., vasárnap, 6:13:03 UTC+1): > On Sat, Nov 14, 2020 at 8:34 PM Robert Engels > wrote: > >> That wasn’t my take on the OPs need. He said the c

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Kurtis Rader
On Sat, Nov 14, 2020 at 8:34 PM Robert Engels wrote: > That wasn’t my take on the OPs need. He said the consumer is very > expensive - implying to me they only want to process the latest. If > dropping the oldest is viable you might as well drop all old entries and > use the latest. > The O.P. w

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Robert Engels
That wasn’t my take on the OPs need. He said the consumer is very expensive - implying to me they only want to process the latest. If dropping the oldest is viable you might as well drop all old entries and use the latest. Pretty common in trading systems with a price feed - in many cases only

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Kurtis Rader
On Sat, Nov 14, 2020 at 7:54 PM Robert Engels wrote: > 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 it

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Robert Engels
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

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread Kurtis Rader
On Sat, Nov 14, 2020 at 5:43 PM 陶青云 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 t

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread 陶青云
在2020年11月14日星期六 UTC+8 上午3:09:24 写道: > If I understand what you're trying to do, I'd approach it this way, using > a generously buffered channel and discarding the extras at the consumer, as > shown below, instead of at the producer: > > result <- c // wait for result to appear > for len(c) > 0

Re: [go-nuts] How to drop old value in a channel salety

2020-11-14 Thread 陶青云
在2020年11月13日星期五 UTC+8 下午11:19:46 写道: > On Thu, Nov 12, 2020 at 11:13 PM 陶青云 wrote: > > > > > > It is not a one-length buffered channel. I need to drop because the > recveiver do heavy work that can't process as quickly as sender. > > Don't use channels for this. It's not what they are for.

Re: [go-nuts] How to drop old value in a channel salety

2020-11-13 Thread Gregg Townsend
If I understand what you're trying to do, I'd approach it this way, using a generously buffered channel and discarding the extras at the consumer, as shown below, instead of at the producer: result <- c // wait for result to appear for len(c) > 0 { // there is a newer result available re

Re: [go-nuts] How to drop old value in a channel salety

2020-11-13 Thread Tarmigan
I see some smart people suggesting that what OP is doing is not correct. I am going to cautiously disagree with some of those replies and suggest that a similar pattern can be useful for some applications. We have an application where we have a similar need. Specifically, there is a periodic tic

Re: [go-nuts] How to drop old value in a channel salety

2020-11-13 Thread Aleksey Tulinov
Try to use two channels: one to signal that the receiver needs a new value and another one to send new value to the receiver. Supposedly the sender won't block if you're using `select` to check what receivers need values, and the receiver can block until a new value is arrived at the input channel.

Re: [go-nuts] How to drop old value in a channel salety

2020-11-13 Thread Jesper Louis Andersen
On Fri, Nov 13, 2020 at 8:12 AM 陶青云 wrote: > > It is not a one-length buffered channel. I need to drop because the > recveiver do heavy work that can't process as quickly as sender. > You need to write a flow control scheme then. Channels may be part of the solution, but they can't solve it on

Re: [go-nuts] How to drop old value in a channel salety

2020-11-13 Thread Ian Lance Taylor
On Thu, Nov 12, 2020 at 11:13 PM 陶青云 wrote: > > > It is not a one-length buffered channel. I need to drop because the > recveiver do heavy work that can't process as quickly as sender. Don't use channels for this. It's not what they are for. Ian > 在2020年11月13日星期五 UTC+8 下午2:36:13 写道: >> >>

Re: [go-nuts] How to drop old value in a channel salety

2020-11-12 Thread 陶青云
It is not a one-length buffered channel. I need to drop because the recveiver do heavy work that can't process as quickly as sender. 在2020年11月13日星期五 UTC+8 下午2:36:13 写道: > On Thu, Nov 12, 2020 at 9:32 PM 陶青云 wrote: > >> Thanks. I want the receiver always get the relately new vaule, I don't >>

Re: [go-nuts] How to drop old value in a channel salety

2020-11-12 Thread Kurtis Rader
On Thu, Nov 12, 2020 at 9:32 PM 陶青云 wrote: > Thanks. I want the receiver always get the relately new vaule, I don't > want the sender blocked and I either choose drop the current value or the > first value of the channel. But I don't find a way to safely drop the first > value from the channel.

Re: [go-nuts] How to drop old value in a channel salety

2020-11-12 Thread 陶青云
Thanks. I want the receiver always get the relately new vaule, I don't want the sender blocked and I either choose drop the current value or the first value of the channel. But I don't find a way to safely drop the first value from the channel. Maybe like this ? ``` // c is buffered channel s

Re: [go-nuts] How to drop old value in a channel salety

2020-11-12 Thread Kurtis Rader
On Thu, Nov 12, 2020 at 6:19 PM 陶青云 wrote: > ``` > // c is buffered channel > select { > case c <- result: > default: > // full, drop old one > <-c > c <- result > } > ``` > > I have a code like this, but there may be a race condition in the default > case. I enconter a > dead

Re: [go-nuts] How to drop old value in a channel salety

2020-11-12 Thread Ian Lance Taylor
On Thu, Nov 12, 2020 at 6:19 PM 陶青云 wrote: > > ``` > // c is buffered channel > select { > case c <- result: > default: > // full, drop old one > <-c > c <- result > } > ``` > > I have a code like this, but there may be a race condition in the default > case. I enconter a > de

[go-nuts] How to drop old value in a channel salety

2020-11-12 Thread 陶青云
``` // c is buffered channel select { case c <- result: default: // full, drop old one <-c c <- result } ``` I have a code like this, but there may be a race condition in the default case. I enconter a deadlock that the goroutine block on 'c<'. -- You received this message