env var is now time.Duration

This commit is contained in:
m-okeefe 2019-01-29 14:37:56 -08:00
parent 33921e7ddb
commit 4840f0eae8
2 changed files with 14 additions and 21 deletions

View file

@ -32,8 +32,7 @@ kubectl exec \
## Latency injection
This service has an "inject sleep" toggle that will sleep for `n` seconds on every call to
This service has an `EXTRA_LATENCY` environment variable. This will inject a sleep for the specified [time.Duration](https://golang.org/pkg/time/#ParseDuration) on every call to
to the server.
You can set this mode as an environment variable. For instance, to sleep for 3 seconds,
use `SLEEP_SECONDS=3`.
For example, use `EXTRA_LATENCY="5s500ms"` to sleep for 5.5 seconds on every request.

View file

@ -23,7 +23,6 @@ import (
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
@ -46,10 +45,10 @@ import (
)
var (
cat pb.ListProductsResponse
catalogMutex *sync.Mutex
log *logrus.Logger
sleepDuration time.Duration
cat pb.ListProductsResponse
catalogMutex *sync.Mutex
log *logrus.Logger
sleep time.Duration
port = flag.Int("port", 3550, "port to listen at")
@ -147,26 +146,21 @@ func initStats(exporter *stackdriver.Exporter) {
// sets the sleepSeconds variable, if env variable provided
func initSleep() {
sleepTmp := os.Getenv("SLEEP_SECONDS")
if sleepTmp == "" {
sleepDuration = time.Duration(0)
s := ""
if s = os.Getenv("EXTRA_LATENCY"); s == "" {
sleep = time.Duration(0)
return
}
sleepInt, err := strconv.Atoi(sleepTmp)
v, err := time.ParseDuration(s)
if err != nil {
log.Error("invalid SLEEP_SECONDS var")
return
panic("Invalid EXTRA_LATENCY var, must be time.Duration")
}
if sleepInt < 1 {
log.Error("invalid SLEEP_SECONDS var, must be nonzero int")
return
}
sleepDuration = time.Second * time.Duration(sleepInt)
log.Infof("sleep enabled (%d seconds)", sleepInt)
sleep = v
log.Infof("extra latency enabled (duration: %v)", sleep)
}
func injectSleep() {
time.Sleep(sleepDuration)
time.Sleep(sleep)
}
func initStackDriverTracing() {