Skip to content

Instantly share code, notes, and snippets.

@jacoelho
Last active August 16, 2016 08:34
Show Gist options
  • Save jacoelho/fab6e288d892a27b801eae45cec73da7 to your computer and use it in GitHub Desktop.
Save jacoelho/fab6e288d892a27b801eae45cec73da7 to your computer and use it in GitHub Desktop.
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