Skip to content

Instantly share code, notes, and snippets.

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