ratelimit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MiddlewareFunc

func MiddlewareFunc(requestsPerSecond float64, burstSize int) func(http.Handler) http.Handler

MiddlewareFunc is a convenience function that creates a rate limiting middleware.

Types

type Config

type Config struct {
	RequestsPerSecond float64       // Requests allowed per second
	BurstSize         int           // Maximum burst size
	CleanupInterval   time.Duration // How often to clean up old entries
	Enabled           bool          // Whether rate limiting is enabled
}

Config holds rate limiter configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default rate limiter configuration.

type Limiter

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

Limiter implements a per-IP token bucket rate limiter.

func NewLimiter

func NewLimiter(config *Config) *Limiter

NewLimiter creates a new rate limiter.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow checks if a request from the given key (usually IP) is allowed.

func (*Limiter) Middleware

func (l *Limiter) Middleware(next http.Handler) http.Handler

Middleware returns an HTTP middleware that applies rate limiting.

func (*Limiter) RemainingTokens

func (l *Limiter) RemainingTokens(key string) int

RemainingTokens returns the number of remaining tokens for a key.

func (*Limiter) ResetAfter

func (l *Limiter) ResetAfter(key string) time.Duration

ResetAfter returns the duration until the bucket is refilled.

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop stops the rate limiter cleanup goroutine.

Jump to

Keyboard shortcuts

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