-
-
Save peterbourgon/f7084edbeefdd1b40334 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
ugh ~/tmp/hystr cat example.go | |
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") | |
} | |
ugh ~/tmp/hystr cd $GOPATH/src/github.com/afex/hystrix-go ; git checkout 5b79165 ; cd - | |
HEAD is now at 5b79165... use buffered client for statsd collector to reduce TotalDuration overhead | |
ugh ~/tmp/hystr rm -rf $GOPATH/pkg/darwin_amd64/github.com/afex/hystrix-go ; go run example.go | |
priming with 40 successful requests | |
switching to errors... | |
now the next few requests should give us our error: kaboom | |
got expected error 1 | |
got expected error 2 | |
the circuit should have opened by now | |
got unexpected error at request 1: kaboom | |
exit status 1 | |
ugh ~/tmp/hystr cd $GOPATH/src/github.com/afex/hystrix-go ; git checkout be7b59e ; cd - | |
Previous HEAD position was 5b79165... use buffered client for statsd collector to reduce TotalDuration overhead | |
HEAD is now at be7b59e... Merge branch 'master' into loadtest | |
ugh ~/tmp/hystr rm -rf $GOPATH/pkg/darwin_amd64/github.com/afex/hystrix-go ; go run example.go | |
priming with 40 successful requests | |
switching to errors... | |
now the next few requests should give us our error: kaboom | |
got expected error 1 | |
got expected error 2 | |
the circuit should have opened by now | |
hystrix-go: opening circuit my-endpoint | |
got expected error: hystrix: circuit open | |
got expected error: hystrix: circuit open | |
got expected error: hystrix: circuit open | |
got expected error: hystrix: circuit open | |
got expected error: hystrix: circuit open | |
everything works as expected | |
ugh ~/tmp/hystr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment