ratelimit

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package ratelimit implements rate limiting of HTTP requests.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidRate is used when the rate is less than 1 request per second.
	ErrInvalidRate = errors.New("invalid rate; needs to be between 1 and 3600 seconds")
)

Functions

func IPBucket

func IPBucket(prefix string, filter func([]string) (string, bool)) func(*http.Request) string

IPBucket is a generator of rate limit buckets based on client's IP address. Optional filter function can be passed in, defaults to finding first public IP, see: https://godoc.org/github.com/ripexz/rip

func RateLimit

func RateLimit(opts Config) func(http.Handler) http.Handler

RateLimit limits requests for a key provided by getKey function. If ignore function returns true, rate limit is bypassed. grantOnErr argument defines if it should grant access when Redis is down.

func SetRate

func SetRate(n int, d time.Duration) error

SetRate set the rate limit rate. (10, time.Second) is 10 requests per second

Types

type Config

type Config struct {
	Pool       redisPool
	GrantOnErr bool
	ErrorLog   func(error, string)

	// GetKey generates bucket keys.
	GetKey func(*http.Request) string

	// Ignore rate limit verification for this request if this returns true.
	Ignore func(*http.Request) bool

	// Rates returns the number of API calls (to all endpoints) that can be made
	// by the client considering the request. If null global values will be used.
	Rates func(*http.Request) (perPeriod, periodSeconds int)
}

Config for RateLimit

Jump to

Keyboard shortcuts

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