Skip to content

Releases

Monotonic clock, sub-millisecond windows, negative weight fix

Choose a tag to compare

@bw19 bw19 released this 17 Oct 10:55

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.Duration with nanosecond precision. A window of zero or less is treated as 1ns.
  • AllowN and PeekN refuse 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 Throttle struct is now 72 bytes. The README has been updated to match.

Peek, SetLimit, return observed

Choose a tag to compare

@bw19 bw19 released this 22 May 01:58

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

Choose a tag to compare

@bw19 bw19 released this 13 Jul 02:55

Also improved comments

Redone with mutex

Choose a tag to compare

@bw19 bw19 released this 09 Jul 21:46
v0.2.0

Redone with mutex

First release

First release Pre-release
Pre-release

Choose a tag to compare

@bw19 bw19 released this 09 Jul 16:58
v0.1.0

Merge branch 'main' of https://github.com/microbus-io/throttle