Can github.com/cockroachdb/circuitbreaker be used by multiple goroutines concurrently?

28 views
Skip to first unread message

Jingguo Yao

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

Can github.com/cockroachdb/circuitbreaker 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](https://github.com/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

unread,
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 cockroach-db...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cockroach-db/e9e747a3-a9a6-4e07-99ef-4dff8840da6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ajwe...@cockroachlabs.com

unread,
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
Forward
0 new messages