topk

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SketchParams added in v0.2.0

type SketchParams struct {
	// K is the number of top items to keep track of in the sketch.
	K int
	// WindowSize is the size of the sliding window, measured in ticks. The total
	// theoretical capacity of the window is `WindowSize * TickSize`. For example,
	// if WindowSize is 10 and TickSize is 100, the window capacity is 1000 requests.
	WindowSize int
	// Width is the width of the underlying Count-Min sketch. A larger width
	// reduces the probability of over-counting but increases memory usage.
	Width int
	// Depth is the depth of the underlying Count-Min sketch. A larger depth
	// also reduces over-counting at the cost of more memory.
	Depth int
	// TickSize is the number of requests that constitute a single "tick". After
	// this many requests, the sketch's internal clock advances.
	TickSize uint64
	// MaxSharePercent is the maximum percentage of the total window capacity that
	// a single IP can consume before being considered for blocking. This logic prevents
	// server breakdown by allowing a higher share for lower traffic levels (where a
	// dominant IP is not a threat) and a lower, more aggressive share for higher
	// traffic levels. For example, at the 'medium' level (35% share, 1000 request
	// capacity), an IP is blocked if it exceeds 350 requests within the window.
	MaxSharePercent int
	// ActivationRPS is the requests-per-second threshold that must be met for the
	// blocker to become active. Its primary purpose is to act as a gate, ensuring
	// the blocker does nothing during periods of low server load. For example, at the
	// 'medium' level (100 request TickSize, 500 RPS activation), a tick must occur
	// in 200ms or less for the blocker to engage.
	ActivationRPS int
}

SketchParams holds the configuration for creating a new TopKSketch.

type TopKSketch

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

TopKSketch provides a thread-safe wrapper around a sliding window sketch for tracking frequent items and managing ticking.

func New added in v0.2.0

func New(params SketchParams) *TopKSketch

New creates a new thread-safe sketch wrapper. It initializes the underlying sliding window sketch with the given parameters.

func (*TopKSketch) ProcessTick

func (cs *TopKSketch) ProcessTick(ip string) []string

ProcessTick increments the count for the given item. If a tick completes, it checks against the provided thresholds and returns a list of IPs to block.

Jump to

Keyboard shortcuts

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