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 ## 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. to the server.
You can set this mode as an environment variable. For instance, to sleep for 3 seconds, For example, use `EXTRA_LATENCY="5s500ms"` to sleep for 5.5 seconds on every request.
use `SLEEP_SECONDS=3`.

View file

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