Documentation
¶
Overview ¶
Package window provides the shared rate-limit window primitive used by every limiter domain (internal/rate and internal/limiters). It implements two counting algorithms behind one interface:
- Fixed: classic fixed-window counter. INCR plus a first-hit PEXPIRE, executed as a single Lua script so a crash between the two commands can never leave an orphaned counter without a TTL.
- Sliding: weighted two-bucket sliding-window approximation. Counts are kept in per-bucket keys (bucket length = the window) and the effective count is curr + floor(prev * (window-elapsed)/window), removing the fixed-window 2x boundary-burst weakness with O(1) memory per key.
Bucket indexes are computed from the client clock (injectable for tests); hosts are expected to be NTP-synced. Sliding bucket keys are hash-tagged ("{base}:s:<bucket>") so both buckets always share a Redis Cluster slot.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mode ¶
type Mode int
Mode selects the window-counting algorithm. The zero value is Fixed so existing call sites keep their behavior.
type Window ¶
type Window struct {
// contains filtered or unexported fields
}
Window counts events per key using the configured algorithm.
func New ¶
func New(rdb redis.UniversalClient, mode Mode) *Window
New creates a window counter over the given Redis client.
func (*Window) Count ¶
Count returns the current effective count for key without incrementing. Missing keys count as zero.