algorithm

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package algorithm defines core interfaces and shared data structures for rate limiting algorithm strategies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Algorithm

type Algorithm interface {
	// Name returns the unique identifier string of the algorithm strategy.
	Name() string

	// EvaluateMemory evaluates the rate limit state in local RAM.
	EvaluateMemory(now time.Time, state *State, limit int64, window time.Duration) Result

	// RedisScript returns the atomic Lua script for Redis distribution.
	RedisScript() string
}

Algorithm defines the interface for pluggable rate limiting algorithms. Algorithms provide both a local in-memory evaluation implementation and an atomic Redis Lua script.

type Result

type Result struct {
	// Allowed indicates whether the current request is permitted under the rate limit quota.
	Allowed bool
	// Limit is the maximum request capacity configured for the time window.
	Limit int64
	// Remaining is the remaining request quota available in the active window.
	Remaining int64
	// ResetIn specifies the duration until the rate limit quota resets or recovers.
	ResetIn time.Duration
}

Result represents the outcome of a rate limit evaluation.

type State

type State struct {
	Count      int64
	PrevCount  int64
	LastUpdate time.Time
	Tokens     float64
	Timestamps []int64
}

State stores transient state for a rate limit key evaluated in memory.

Directories

Path Synopsis
Package fixedwindow implements the Fixed Window rate limiting algorithm.
Package fixedwindow implements the Fixed Window rate limiting algorithm.
Package leakybucket implements the Leaky Bucket rate limiting algorithm for traffic shaping.
Package leakybucket implements the Leaky Bucket rate limiting algorithm for traffic shaping.
Package slidingcounter implements the Sliding Window Counter rate limiting algorithm.
Package slidingcounter implements the Sliding Window Counter rate limiting algorithm.
Package slidinglog implements the Sliding Window Log rate limiting algorithm.
Package slidinglog implements the Sliding Window Log rate limiting algorithm.
Package tokenbucket implements the Token Bucket rate limiting algorithm.
Package tokenbucket implements the Token Bucket rate limiting algorithm.

Jump to

Keyboard shortcuts

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