Last active
August 16, 2016 08:41
-
-
Save jacoelho/fe0a84af30f2e7c786cf6e48a79d7972 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 | |
| } | |
| c.close() | |
| return nil | |
| case <-time.After(time.Second * c.TimeOut): | |
| break | |
| } | |
| } | |
| c.incrementFailures() | |
| fallback() | |
| return ErrorCircuitTripped | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment