Last active
December 10, 2019 05:33
-
-
Save abhi-bit/eceab36fdbebc6513594071b8780c785 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
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) | |
} | |
} |
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
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