From 4840f0eae819479f815e41a898bfef0a0d10d255 Mon Sep 17 00:00:00 2001 From: m-okeefe Date: Tue, 29 Jan 2019 14:37:56 -0800 Subject: [PATCH] env var is now time.Duration --- src/productcatalogservice/README.md | 5 ++--- src/productcatalogservice/server.go | 30 ++++++++++++----------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/productcatalogservice/README.md b/src/productcatalogservice/README.md index 6f1f8ed..a68b398 100644 --- a/src/productcatalogservice/README.md +++ b/src/productcatalogservice/README.md @@ -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. diff --git a/src/productcatalogservice/server.go b/src/productcatalogservice/server.go index 8005366..136f00f 100644 --- a/src/productcatalogservice/server.go +++ b/src/productcatalogservice/server.go @@ -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() {