Skip to content

Instantly share code, notes, and snippets.

@aryszka
Created April 27, 2017 12:04
Show Gist options
  • Save aryszka/115fea73da80422a6d46ff058c8dcb0b to your computer and use it in GitHub Desktop.
Save aryszka/115fea73da80422a6d46ff058c8dcb0b to your computer and use it in GitHub Desktop.
Reproduce blocking breaker (https://github.com/rubyist/circuitbreaker/)
package main
import (
"time"
"math/rand"
"log"
circuit "github.com/rubyist/circuitbreaker"
)
func consume(c <-chan circuit.ListenerEvent) {
for {
select {
case e := <-c:
log.Println(e)
case <-time.After(time.Second):
panic("events blocked")
}
}
}
func produce(b *circuit.Breaker) {
for {
time.Sleep(time.Duration(rand.Intn(42000)) * time.Nanosecond)
if b.Tripped() {
b.Reset()
} else {
b.Trip()
}
}
}
func main() {
c := make(chan circuit.ListenerEvent, 1)
b := circuit.NewBreaker()
b.AddListener(c)
go consume(c)
go produce(b)
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment