Skip to content

Instantly share code, notes, and snippets.

@abhi-bit
Last active December 10, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhi-bit/eceab36fdbebc6513594071b8780c785 to your computer and use it in GitHub Desktop.
Save abhi-bit/eceab36fdbebc6513594071b8780c785 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"log"
"net/http"
_ "net/http/pprof"
"time"
"github.com/afex/hystrix-go/hystrix"
)
const (
hystrixCmd = "sample_command"
)
func main() {
hystrix.ConfigureCommand(hystrixCmd, hystrix.CommandConfig{
Timeout: 1000,
MaxConcurrentRequests: 5,
SleepWindow: 1000,
ErrorPercentThreshold: 1,
})
http.HandleFunc("/fn", handlerFn)
_ = http.ListenAndServe(":8080", nil)
}
var counter int
func handlerFn(w http.ResponseWriter, r *http.Request) {
resultCh := make(chan []byte)
errCh := hystrix.Go(hystrixCmd, func() error {
counter++
if counter < 10 {
return errors.New("forced failure")
}
time.Sleep(time.Hour)
resultCh <- []byte("success")
return nil
}, nil)
select {
case <-resultCh:
log.Println("Success")
w.WriteHeader(http.StatusOK)
case err := <-errCh:
log.Println("Err:", err, counter)
w.WriteHeader(http.StatusServiceUnavailable)
}
}
for i in `seq 1 2`; do for i in `seq 1 10`; do curl http://localhost:8080/fn &; done; sleep 11; done;
for i in `seq 1 100`; do for i in `seq 1 10`; do curl http://localhost:8080/fn &; done; sleep 2; done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment