From 33921e7ddb03fb52af0621f3fd5aa75cbac77c2e Mon Sep 17 00:00:00 2001 From: m-okeefe Date: Tue, 29 Jan 2019 14:23:27 -0800 Subject: [PATCH] Added SLEEP_SECONDS env for product catalog --- src/productcatalogservice/README.md | 8 ++++++ src/productcatalogservice/server.go | 38 ++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/productcatalogservice/README.md b/src/productcatalogservice/README.md index 13ff37c..6f1f8ed 100644 --- a/src/productcatalogservice/README.md +++ b/src/productcatalogservice/README.md @@ -29,3 +29,11 @@ kubectl exec \ $(kubectl get pods -l app=productcatalogservice -o jsonpath='{.items[0].metadata.name}') \ -c server -- kill -USR2 1 ``` + +## Latency injection + +This service has an "inject sleep" toggle that will sleep for `n` seconds 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`. diff --git a/src/productcatalogservice/server.go b/src/productcatalogservice/server.go index 4fb7018..8005366 100644 --- a/src/productcatalogservice/server.go +++ b/src/productcatalogservice/server.go @@ -23,6 +23,7 @@ import ( "net" "os" "os/signal" + "strconv" "strings" "sync" "syscall" @@ -45,9 +46,10 @@ import ( ) var ( - cat pb.ListProductsResponse - catalogMutex *sync.Mutex - log *logrus.Logger + cat pb.ListProductsResponse + catalogMutex *sync.Mutex + log *logrus.Logger + sleepDuration time.Duration port = flag.Int("port", 3550, "port to listen at") @@ -73,6 +75,7 @@ func init() { } func main() { + go initSleep() go initTracing() go initProfiling("productcatalogservice", "1.0.0") flag.Parse() @@ -117,7 +120,6 @@ func initJaegerTracing() { log.Info("jaeger initialization disabled.") return } - // Register the Jaeger exporter to be able to retrieve // the collected spans. exporter, err := jaeger.NewExporter(jaeger.Options{ @@ -143,6 +145,30 @@ 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) + return + } + sleepInt, err := strconv.Atoi(sleepTmp) + if err != nil { + log.Error("invalid SLEEP_SECONDS var") + return + } + 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) +} + +func injectSleep() { + time.Sleep(sleepDuration) +} + func initStackDriverTracing() { // TODO(ahmetb) this method is duplicated in other microservices using Go // since they are not sharing packages. @@ -222,14 +248,17 @@ func parseCatalog() []*pb.Product { } func (p *productCatalog) Check(ctx context.Context, req *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) { + injectSleep() return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil } func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) { + injectSleep() return &pb.ListProductsResponse{Products: parseCatalog()}, nil } func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.Product, error) { + injectSleep() var found *pb.Product for i := 0; i < len(parseCatalog()); i++ { if req.Id == parseCatalog()[i].Id { @@ -243,6 +272,7 @@ func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductReque } func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProductsRequest) (*pb.SearchProductsResponse, error) { + injectSleep() // Intepret query as a substring match in name or description. var ps []*pb.Product for _, p := range parseCatalog() {