Created
August 14, 2015 17:54
-
-
Save peterbourgon/7dd431c532e62975f931 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"errors" | |
"log" | |
"github.com/afex/hystrix-go/hystrix" | |
) | |
func main() { | |
log.SetFlags(0) | |
const ( | |
commandName = "my-endpoint" | |
errorPercent = 5 | |
maxConcurrent = 1000 | |
) | |
hystrix.ConfigureCommand(commandName, hystrix.CommandConfig{ | |
ErrorPercentThreshold: errorPercent, | |
MaxConcurrentRequests: maxConcurrent, | |
}) | |
var ( | |
primeWith = hystrix.DefaultVolumeThreshold * 2 | |
shouldPass = func(n int) bool { return (float64(n) / float64(primeWith+n)) <= (float64(errorPercent-1) / 100.0) } | |
) | |
var myErr error | |
run := func() error { return myErr } | |
log.Printf("priming with %d successful requests", primeWith) | |
for i := 0; i < primeWith; i++ { | |
if err := hystrix.Do(commandName, run, nil); err != myErr { | |
log.Fatalf("initial request %d failed: %v", i+1, err) | |
} | |
} | |
log.Printf("switching to errors...") | |
myErr = errors.New("kaboom") | |
log.Printf("now the next few requests should give us our error: %v", myErr) | |
for i := 0; shouldPass(i); i++ { | |
if err := hystrix.Do(commandName, run, nil); err != myErr { | |
log.Fatalf("post-error request %d failed: %v", i+1, err) | |
} | |
log.Printf("got expected error %d", i+1) | |
} | |
log.Printf("the circuit should have opened by now") | |
for i := 0; i < 5; i++ { | |
if err := hystrix.Do(commandName, run, nil); err != hystrix.ErrCircuitOpen { | |
log.Fatalf("got unexpected error at request %d: %v", i+1, err) | |
} | |
log.Printf("got expected error: %v", hystrix.ErrCircuitOpen) | |
} | |
log.Printf("everything works as expected") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment