Can be used by multiple goroutines concurrently?

Skip to first unread message

Jingguo Yao

Feb 2, 2019, 4:57:28 AM2/2/19
to CockroachDB
Since it seems that is abandoned, I ask this question here.

Can be used by multiple goroutines concurrently? I.e., does it support the following usage?

cb := circuit.NewThresholdBreaker(10)

events := cb.Subscribe()
go func() {
  for {
    e := <-events
    // Monitor breaker events

go cb.Call(func() error {
// A remote call goes here
}, 0)

go cb.Call(func() error {
// Another remote call goes here
}, 0)

I checked the source code [cockroachdb circuitbreaker]( I found that it uses atomic operations and mutexes when updating its fields. So it is possible for the circuitbreaker to support the above usage. But there is documentation mentioning it. And I browsed the CockroachDB source code. It seems that CockroachDB does not use circuit.Breaker concurrently either.

Andrei Matei

Feb 6, 2019, 3:52:06 PM2/6/19
to Jingguo Yao, Andrew Werner, CockroachDB
+ Andrew Werner

Perhaps Andrew would know?

You received this message because you are subscribed to the Google Groups "CockroachDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit

Feb 6, 2019, 4:27:03 PM2/6/19
to CockroachDB
Indeed it should be safe to use the Breaker concurrently and cockroachdb absolutely takes advantage of this property. It is also safe to use it with a Subscribe call and two concurrent goroutines like you suggest. Note however that cockroachdb does not use the Event functionality of the library. I somewhat recently submitted a PR (see #4) that fixed a deadlock in the upstream for the Subscribe and Listener methods. That being said, please heed the following comment on the Subscribe and AddListener methods:

Note that events may be dropped or not sent so clients should not rely on events for program correctness.
Using AddListener (instead of Subscribe) with a large buffer and ensuring that the reading side of the channel is fast will likely mitigate missing events. Events will only be dropped if the channel is full. Feel free to file an issue if you run into any problems.
Reply all
Reply to author
0 new messages