Skip to content

fix: treat negative LimitRule.RandomDelay as no delay#911

Open
Noethix55555 wants to merge 1 commit into
gocolly:masterfrom
Noethix55555:fix/limitrule-negative-randomdelay
Open

fix: treat negative LimitRule.RandomDelay as no delay#911
Noethix55555 wants to merge 1 commit into
gocolly:masterfrom
Noethix55555:fix/limitrule-negative-randomdelay

Conversation

@Noethix55555

Copy link
Copy Markdown

Fixes #910

Problem

LimitRule.RandomDelay is an exported time.Duration that users set directly. The delay path in http_backend.go feeds it to rand.Int63n:

if r.RandomDelay != 0 {
    randomDelay = time.Duration(rand.Int63n(int64(r.RandomDelay)))
}

rand.Int63n panics for any argument <= 0, so a negative RandomDelay panics with invalid argument to Int63n. In Async mode the panic happens inside the fetch goroutine and crashes the whole process, so Wait() never returns.

Fix

Change the guard from != 0 to > 0 so a negative RandomDelay is treated as no random delay. This mirrors the fact that a negative Delay is already a harmless no-op for time.Sleep.

Test

Added TestLimitRuleNegativeRandomDelay, which configures a rule with RandomDelay: -1 * time.Second, performs an async Visit against an httptest server, and asserts Wait() completes without panicking. It fails (panics) on the current code and passes with the fix. Full go test ./... passes.

A negative RandomDelay reaches rand.Int63n, which panics for any
argument <= 0. In Async mode the panic occurs inside the fetch
goroutine and crashes the process so Wait never returns. Guard the
random delay with > 0 so a negative value is treated as no delay.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative LimitRule.RandomDelay panics the process

1 participant