ratelimit

package
v0.13.4 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP returns client ip from request

Types

type ConcurrentQueueStrategy

type ConcurrentQueueStrategy struct {
	Capacity int // max concurrent at a time
	Size     int // queue size
	// contains filtered or unexported fields
}

ConcurrentQueueStrategy implements Strategy that allow only max concurrent requests at a time other requests will queue, until queue full then the request wil drop

func (*ConcurrentQueueStrategy) After

After always return 0, since we don't know when request will finish

func (*ConcurrentQueueStrategy) Put

func (b *ConcurrentQueueStrategy) Put(key string)

Put removes requests count

func (*ConcurrentQueueStrategy) Take

func (b *ConcurrentQueueStrategy) Take(key string) bool

Take returns true if current requests less than capacity

type ConcurrentStrategy

type ConcurrentStrategy struct {
	Capacity int // Max concurrent at a time
	// contains filtered or unexported fields
}

ConcurrentStrategy implements Strategy that allow only max concurrent requests at a time other requests will drop

func (*ConcurrentStrategy) After

After always return 0, since we don't know when request will finish

func (*ConcurrentStrategy) Put

func (b *ConcurrentStrategy) Put(key string)

Put removes requests count

func (*ConcurrentStrategy) Take

func (b *ConcurrentStrategy) Take(key string) bool

Take returns true if current requests less than capacity

type ExceededHandler

type ExceededHandler func(w http.ResponseWriter, r *http.Request, after time.Duration)

ExceededHandler type

type FixedWindowStrategy

type FixedWindowStrategy struct {
	Max  int           // Max token per window
	Size time.Duration // Window size
	// contains filtered or unexported fields
}

FixedWindowStrategy implements Strategy using fixed window algorithm

func (*FixedWindowStrategy) After

func (b *FixedWindowStrategy) After(key string) time.Duration

After returns next time that can take again

func (*FixedWindowStrategy) Put

func (b *FixedWindowStrategy) Put(string)

Put does nothing

func (*FixedWindowStrategy) Take

func (b *FixedWindowStrategy) Take(key string) bool

Take takes a token from bucket, return true if token available to take

type LeakyBucketStrategy

type LeakyBucketStrategy struct {
	PerRequest time.Duration // time per request
	Size       int           // queue size
	// contains filtered or unexported fields
}

LeakyBucketStrategy implements Strategy using leaky bucket algorithm

func (*LeakyBucketStrategy) After

func (b *LeakyBucketStrategy) After(key string) time.Duration

After returns time that can take again

func (*LeakyBucketStrategy) Put

func (b *LeakyBucketStrategy) Put(string)

Put do nothing

func (*LeakyBucketStrategy) Take

func (b *LeakyBucketStrategy) Take(key string) bool

Take waits until token can be take, unless queue full will return false

type RateLimiter

type RateLimiter struct {
	Key                  func(r *http.Request) string
	ExceededHandler      ExceededHandler
	Strategy             Strategy
	ReleaseOnWriteHeader bool // release token when write response's header
	ReleaseOnHijacked    bool // release token when hijacked
}

RateLimiter middleware

func Concurrent

func Concurrent(capacity int) *RateLimiter

Concurrent creates new concurrent rate limiter

func ConcurrentQueue

func ConcurrentQueue(capacity, size int) *RateLimiter

ConcurrentQueue creates new concurrent queue rate limiter

func FixedWindow

func FixedWindow(rate int, unit time.Duration) *RateLimiter

FixedWindow creates new fixed window rate limiter

func FixedWindowPerHour

func FixedWindowPerHour(rate int) *RateLimiter

FixedWindowPerHour creates new rate limiter per hour

func FixedWindowPerMinute

func FixedWindowPerMinute(rate int) *RateLimiter

FixedWindowPerMinute creates new rate limiter per minute

func FixedWindowPerSecond

func FixedWindowPerSecond(rate int) *RateLimiter

FixedWindowPerSecond creates new rate limiter per second

func LeakyBucket

func LeakyBucket(perRequest time.Duration, size int) *RateLimiter

LeakyBucket creates new leaky bucket rate limiter

func New

func New(strategy Strategy) *RateLimiter

New creates new rate limiter

func (RateLimiter) ServeHandler

func (m RateLimiter) ServeHandler(h http.Handler) http.Handler

ServeHandler implements middleware interface

type Strategy

type Strategy interface {
	// Take returns true if success
	Take(key string) bool

	// Put calls after finished
	Put(key string)

	// After returns next time that can take again
	After(key string) time.Duration
}

Strategy interface

Jump to

Keyboard shortcuts

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