Repository navigation
Releases
Monotonic clock, sub-millisecond windows, negative weight fix
This release fixes three correctness bugs. The public API is unchanged.
Fixes
- Clock changes no longer stall the throttle. Time used to come from the wall clock (
time.Now().UnixMilli()). If the clock jumped back, for example from an NTP correction or a VM resuming, the throttle refused nearly every request until the clock caught up. It now uses the monotonic clock and reads it under the lock, so concurrent callers see time in the same order they update the counters. - Windows shorter than 1ms now work. The window used to be stored in whole milliseconds, so a window under 1ms became 0. That caused a divide by zero, which refused everything after the first fill on arm64 and admitted everything on amd64. Fractional windows were also rounded down. The window is now a
time.Durationwith nanosecond precision. A window of zero or less is treated as 1ns. AllowNandPeekNrefuse negative weights. A negative weight used to lower the counter, which let later operations exceed the limit.
Other
- The accuracy tests now run on simulated time, so they no longer fail when the machine is busy. New tests cover sub-millisecond windows, fractional windows and negative weights.
- The
Throttlestruct is now 72 bytes. The README has been updated to match.
Peek, SetLimit, return observed
Allow and AllowN now return (admit bool, observed int) instead of just bool, where observed is the sliding window's load measured before the call — useful for anchoring downstream rejection responses (e.g. a 429) to the actually emitted rate. Two new non-mutating methods, Peek and PeekN, answer the same admit-or-not question without consuming a slot, suitable for filtering candidates or preempting work based on current load. The throttle's limit is now runtime-adjustable via SetLimit and readable via Limit; counters are preserved across changes so the new limit applies against the existing sliding-window history rather than resetting it. Existing call sites must be updated to destructure the new return value (admit, _ := th.Allow()).
Avoid chance of overflow in rare situation
Also improved comments
Redone with mutex
First release
v0.1.0 Merge branch 'main' of https://github.com/microbus-io/throttle