ratewindow

package
v0.76.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package ratewindow provides bounded, externally synchronized fixed-window counters for peer admission and misbehavior tracking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decision

type Decision uint8

Decision describes why a fixed-window admission was denied.

const (
	Admitted Decision = iota
	PeerBudgetExceeded
	ProcessBudgetExceeded
	PeerTrackingCapacityExceeded
)

type FixedWindow

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

FixedWindow tracks per-peer counts in one shared fixed-duration window. Callers must serialize access to a FixedWindow with their own mutex.

func NewFixedWindow

func NewFixedWindow(
	window time.Duration,
	perPeerLimit int,
	processLimit int,
	maxPeers int,
) *FixedWindow

NewFixedWindow creates a fixed-window counter. A zero limit disables that limit; maxPeers bounds the number of peer keys retained in one window.

func (*FixedWindow) Admit

func (w *FixedWindow) Admit(peer string, now time.Time) Decision

Admit reserves one unit if both the per-peer and process-wide limits allow it. It does not increment counters when admission is denied.

func (*FixedWindow) Record

func (w *FixedWindow) Record(peer string, now time.Time) (int, bool)

Record increments one peer's counter. It returns false when the peer map is full and the key is new; this keeps misbehavior tracking bounded without scanning stale entries on each update.

Jump to

Keyboard shortcuts

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