Last active
August 16, 2016 08:34
-
-
Save jacoelho/fab6e288d892a27b801eae45cec73da7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| func (c *Circuit) WithCircuit(call func() error, fallback func()) error { | |
| if !c.isOpen() { | |
| wait := make(chan error, 1) | |
| // run function with timeout | |
| go func() { wait <- call() }() | |
| select { | |
| case err := <-wait: | |
| if err != nil { | |
| break | |
| } | |
| return nil | |
| case <-time.After(time.Second * c.TimeOut): | |
| break | |
| } | |
| } | |
| fallback() | |
| return ErrorCircuitTripped | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment