rate

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rate offers a rate limiter middleware

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LimitByIP

func LimitByIP(c router.Context) interface{}

LimitByIP is a Key that uses the IP address

Types

type Bucket

type Bucket struct {
	RefillDate      time.Time
	AvailableTokens int
}

Bucket holds data about the state of a bucket

func (*Bucket) Pick

func (b *Bucket) Pick(now time.Time, limit BucketLimit) bool

Pick tries to pick a token from the bucket

type BucketLimit

type BucketLimit struct {
	Delay time.Duration
	Count int
}

BucketLimit defines how a bucket is filled

type Key

type Key func(c router.Context) interface{}

Key is a way to extract an identifier from a request

type RateLimiter

type RateLimiter struct {
	Limits []BucketLimit

	Key Key
	// contains filtered or unexported fields
}

RateLimiter allows rate limiting requests by IP

The basic algorithm is a bucket filled with tokens: - every `Delay` `Count` tokens are added into the bucket - every time a request is made it takes a token from the bucket

func New

func New(key Key, size int, limits ...BucketLimit) (*RateLimiter, error)

New creates a new ratelimiter

func (*RateLimiter) Middleware

func (l *RateLimiter) Middleware(next router.Route) router.Route

Middleware is a router middleware

func (*RateLimiter) Pick

func (l *RateLimiter) Pick(now time.Time, key interface{}) bool

Pick gets a token from the bucket, refilling it if needed

Jump to

Keyboard shortcuts

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