From dab6684e256ba1ef76ccacdaa401071a220f25d2 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Thu, 12 Dec 2024 15:19:34 +0000 Subject: [PATCH] Implement random sleep durations Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- sleep/handler.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sleep/handler.go b/sleep/handler.go index b396d1b..992b02e 100644 --- a/sleep/handler.go +++ b/sleep/handler.go @@ -3,13 +3,44 @@ package function import ( "fmt" "log" + "math/rand" "os" "time" ) +var r *rand.Rand + +func init() { + + r = rand.New(rand.NewSource(time.Now().Unix())) +} + // Handle a serverless request +// 1. When no headers are given, sleep for the environment variable: sleep_duration. +// 2. When an X-Sleep header is given, sleep for that amount of time. +// 3. When the X-Min-Sleep and X-Max-Sleep headers are given, sleep for a random amount +// of time between those two figures func Handle(req []byte) string { + if minV, ok := os.LookupEnv("Http_X_Min_Sleep"); ok && len(minV) > 0 { + if maxV, ok := os.LookupEnv("Http_X_Max_Sleep"); ok && len(maxV) > 0 { + minSleep, _ := time.ParseDuration(minV) + maxSleep, _ := time.ParseDuration(maxV) + + minMs := minSleep.Milliseconds() + maxMs := maxSleep.Milliseconds() + + randMs := r.Int63n(maxMs-minMs) + minMs + + sleepDuration, _ := time.ParseDuration(fmt.Sprintf("%dms", randMs)) + + log.Printf("Start sleep for: %fs\n", sleepDuration.Seconds()) + time.Sleep(sleepDuration) + log.Printf("Sleep done for: %fs\n", sleepDuration.Seconds()) + return fmt.Sprintf("Slept for: %fs", sleepDuration.Seconds()) + } + } + sleepDuration := time.Second * 2 if val, ok := os.LookupEnv("Http_X_Sleep"); ok && len(val) > 0 {