Created
April 27, 2017 12:04
-
-
Save aryszka/115fea73da80422a6d46ff058c8dcb0b to your computer and use it in GitHub Desktop.
Reproduce blocking breaker (https://github.com/rubyist/circuitbreaker/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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