Skip to content

Commit 71dcfd7

Browse files
committed
Fix range checking for sleep function
When Min and Max were the same, it caused a panic due to a range error. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 2dfbcb8 commit 71dcfd7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

sleep/handler.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ func Handle(w http.ResponseWriter, r *http.Request) {
5252
minMs := minSleep.Milliseconds()
5353
maxMs := maxSleep.Milliseconds()
5454

55-
randMs := random.Int63n(maxMs-minMs) + minMs
55+
// Normalize and handle edge cases to avoid panic in Int63n
56+
if maxMs < minMs {
57+
minMs, maxMs = maxMs, minMs
58+
}
59+
60+
var randMs int64
61+
rangeMs := maxMs - minMs
62+
if rangeMs <= 0 {
63+
randMs = minMs // min == max, use fixed duration
64+
} else {
65+
randMs = random.Int63n(rangeMs+1) + minMs // inclusive of max
66+
}
5667

5768
sleepDuration, _ := time.ParseDuration(fmt.Sprintf("%dms", randMs))
5869

0 commit comments

Comments
 (0)