Created
August 16, 2016 08:56
-
-
Save jacoelho/bf92a7184d14c6a5fc187b3d1eb5ee27 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 fetchWeather(location string) string { | |
time.Sleep(time.Duration(rand.Intn(10)) * time.Second) | |
return "sunny" | |
} | |
func fallbackWeather() string { | |
return "raining" | |
} | |
func main() { | |
fuse := &Circuit{ | |
TimeOut: 3, | |
FailThreshold: 5, | |
RetryThreshold: 5, | |
} | |
var localWeather string | |
for { | |
fuse.WithCircuit(func() error { | |
localWeather = fetchWeather("London") | |
return nil | |
}, | |
func() { | |
localWeather = fallbackWeather() | |
}) | |
fmt.Println(localWeather) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment