ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

Rate Limiter

Rate Limiter is a request rate limiting middleware. It uses a Limiter to do the heavy lifting.

Example

rl := ratelimit.New(
    ratelimit.WithLimiter(locallimit.New(
        locallimit.WithTargetRate(10),
        locallimit.WithSleepInterval(100*time.Millisecond))))

Documentation

Overview

Package ratelimit provides middleware for rate limiting HTTP requests.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidLimiter = errors.New("invalid limiter")

ErrInvalidLimiter is returned when the given limiter is invalid.

View Source
var ErrNilOption = errors.New("option cannot be nil")

ErrNilOption is returned when an option is nil.

Functions

func New

func New(options ...func(*Handler) error) (defs.Middleware, error)

New creates a new rate limiter middleware.

func WithLimiter

func WithLimiter(l Limiter) func(h *Handler) error

WithLimiter sets the Limiter to use.

func WithLogLevel

func WithLogLevel(level slog.Level) func(h *Handler) error

WithLogLevel configures the log level to use with the logger.

func WithLogger

func WithLogger(log *slog.Logger) func(h *Handler) error

WithLogger configures the logger to use.

Types

type Handler

type Handler struct {
	defs.MWBase

	Limit Limiter
}

Handler holds the internal rate limiter information.

func (*Handler) GetMWBase

func (h *Handler) GetMWBase() *defs.MWBase

GetMWBase returns the MWBase instance of the handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP limits the requests using the internal Limiter.

type Limiter

type Limiter interface {
	Limit() bool
}

Limiter is the interface a limiter has to implement to be used in the rate limiter middleware.

Directories

Path Synopsis
Package locallimit provides a process-local rate limiter.
Package locallimit provides a process-local rate limiter.

Jump to

Keyboard shortcuts

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