Skip to content

Commit fafd40d

Browse files
authored
Merge pull request #237 from IBM/introduce-random-sleep-time
Introduced random sleep time and success rate to the helloworld sample
2 parents d88e004 + e41fb72 commit fafd40d

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

helloworld/helloworld.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"io"
2020
"log"
21+
"math/rand/v2"
2122
"net/http"
2223
"os"
2324
"os/exec"
@@ -30,6 +31,11 @@ import (
3031
"time"
3132
)
3233

34+
const (
35+
SLEEP_MIN int = 10
36+
SLEEP_MAX int = 30
37+
)
38+
3339
func Curl(url string) (string, error) {
3440
cmd := exec.Command("curl", "--http0.9", "-s", url)
3541
res, err := cmd.CombinedOutput()
@@ -186,6 +192,32 @@ func IsBucketMounted() bool {
186192
return err == nil && info.IsDir()
187193
}
188194

195+
func ShouldCrash() bool {
196+
197+
// If the 'CRASH' or 'FAIL' env vars are set then crash!
198+
if os.Getenv("CRASH") != "" || os.Getenv("FAIL") != "" {
199+
return true
200+
}
201+
202+
// If the env var SUCCESS_RATE is set the decision to succeed or failed is being calculated randomly
203+
if os.Getenv("SUCCESS_RATE") != "" {
204+
successRate, err := strconv.ParseFloat(os.Getenv("SUCCESS_RATE"), 64)
205+
if err != nil || successRate > 1 || successRate < 0 {
206+
// non-parsable values lead to a 50% success rate
207+
successRate = 0.5
208+
}
209+
210+
// Check whether the successRate is higher than a randomly generated number between 0 and 1
211+
random := rand.Float64()
212+
if successRate > random {
213+
return false
214+
}
215+
216+
return true
217+
}
218+
return false
219+
}
220+
189221
func NewBucketRouter() http.Handler {
190222
mux := http.NewServeMux()
191223

@@ -252,34 +284,6 @@ func main() {
252284
os.Setenv("z", "Set env var 'SHOW' to see all variables")
253285
}
254286

255-
// If env var CRASH is set then crash immediately.
256-
// If its value is of the form HH:MM then crash at the specified time
257-
// time. The time is based on time returned from: http://time.nist.gov:13
258-
// This is useful for testing what happens if the app crashes during
259-
// startup. And the 'time' aspect of it allows for only certain instances
260-
// of the app to crash - for example, we want the app to be created ok
261-
// but then after a minute have any new instances crash.
262-
if date := os.Getenv("CRASH"); date != "" { // Just crash!
263-
// get time: curl http://time.nist.gov:13
264-
// result : 58859 20-01-11 21:28:24 00 0 0 129.3 UTC(NIST) *
265-
if len(date) > 3 && date[2] == ':' {
266-
if now, err := Curl("http://time.nist.gov:13"); err == nil {
267-
parts := strings.SplitN(now, " ", 4)
268-
if len(parts) > 3 {
269-
now = parts[2] // Just time part
270-
now = now[:len(date)]
271-
if now > date {
272-
os.Exit(1)
273-
}
274-
}
275-
} else {
276-
Debug(true, "Curl: %s\n%s", now, err)
277-
}
278-
} else {
279-
os.Exit(1)
280-
}
281-
}
282-
283287
// Figure out what message we want to print. You can override this
284288
// via the "MSG" environment variable. Or, you can just change who
285289
// it says hello to via the "TARGET" environment variable
@@ -307,7 +311,9 @@ func main() {
307311

308312
sleep := os.Getenv("SLEEP")
309313
sleepDuration := 0
310-
if sleep != "" {
314+
if sleep == "RANDOM" {
315+
sleepDuration = rand.IntN(SLEEP_MAX-SLEEP_MIN) + SLEEP_MIN
316+
} else {
311317
sleepDuration, _ = strconv.Atoi(sleep)
312318
}
313319

@@ -321,7 +327,7 @@ func main() {
321327
PrintMessage(os.Stdout, os.Getenv("SHOW") == "")
322328

323329
// If the 'CRASH' or 'FAIL' env vars are set then crash!
324-
if os.Getenv("CRASH") != "" || os.Getenv("FAIL") != "" {
330+
if ShouldCrash() {
325331
fmt.Printf("Crashing...")
326332
os.Exit(1)
327333
}
@@ -334,9 +340,11 @@ func main() {
334340
for {
335341
sleep := os.Getenv("SLEEP")
336342
sleepDuration := 0
337-
if sleep != "" {
343+
if sleep == "RANDOM" {
344+
sleepDuration = rand.IntN(SLEEP_MAX-SLEEP_MIN) + SLEEP_MIN
345+
} else if sleep != "" {
338346
sleepDuration, _ = strconv.Atoi(sleep)
339-
} else if jobMode == "daemon" {
347+
} else if sleep == "" && jobMode == "daemon" {
340348
// Sleep for 60 seconds and then re-do the execution
341349
sleepDuration = 60
342350
}
@@ -351,7 +359,7 @@ func main() {
351359
PrintMessage(os.Stdout, os.Getenv("SHOW") == "")
352360

353361
// If the 'CRASH' or 'FAIL' env vars are set then crash!
354-
if os.Getenv("CRASH") != "" || os.Getenv("FAIL") != "" {
362+
if ShouldCrash() {
355363
fmt.Printf("Crashing...")
356364
os.Exit(1)
357365
}

0 commit comments

Comments
 (0)