ratelimit

package
v0.13.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2026 License: GPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package ratelimit provides a sliding-window event counter. It is a usage tracker, not a token bucket: callers record events and ask how many events fall inside the configured window ending now. Useful for transmit governor rate caps where we want to say "no more than N packets/min on this channel" without ever blocking a submit call.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Window

type Window struct {
	// contains filtered or unexported fields
}

Window tracks events over a fixed duration. Not safe for concurrent use without the internal lock held; all exported methods take it.

func New

func New(window time.Duration) *Window

New returns a Window of the given duration using time.Now as its clock. Panics if window <= 0.

func NewWithClock

func NewWithClock(window time.Duration, now func() time.Time) *Window

NewWithClock returns a Window with a caller-supplied clock. Panics if window <= 0 or now == nil.

func (*Window) Count

func (w *Window) Count() int

Count returns the number of events within the window ending now. As a side effect it drops events older than the window so a caller that Records frequently does not leak memory.

func (*Window) Record

func (w *Window) Record()

Record adds an event at the current time.

func (*Window) Reset

func (w *Window) Reset()

Reset clears all recorded events.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL