ratelimit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 7 Imported by: 11

Documentation

Overview

Ratelimit implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultIdentifier

func DefaultIdentifier(r *http.Request) string

func SetupState

func SetupState(s *RLState)

Types

type Limit

type Limit struct {
	// Exceeded is true if the ratelimit has been exceeded
	Exceeded bool
	// Made is the number of requests made in the ratelimit
	Made int
	// Remaining is the number of requests remaining in the ratelimit
	Remaining int
	// TimeToReset is the time remaining until the ratelimit resets
	TimeToReset time.Duration
	// GotIdentifier is the identifier of the ratelimit
	GotIdentifier string
	// MaxRequests is the maximum number of requests allowed in the interval specified by Expiry for the bucket
	MaxRequests int
	// Bucket is the bucket to use for the ratelimit
	Bucket string
}

Limit is used to check if the ratelimit has been exceeded

func (Limit) Headers

func (l Limit) Headers() map[string]string

type RLState

type RLState struct {
	HotCache hotcache.HotCache[int]
}
var State *RLState

type Ratelimit

type Ratelimit struct {
	// Expiry is the time for the ratelimit to expire
	Expiry time.Duration
	// MaxRequests is the maximum number of requests allowed in the interval specified by Expiry for the bucket
	MaxRequests int
	// Bucket is the bucket to use for the ratelimit
	Bucket string
	// Identifier is the identifier of the ratelimit, otherwise DefaultIdentifier is used
	Identifier func(r *http.Request) string
}

func (Ratelimit) Limit

func (rl Ratelimit) Limit(ctx context.Context, r *http.Request) (Limit, error)

Jump to

Keyboard shortcuts

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