ratelimit

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2018 License: MIT Imports: 9 Imported by: 3

README

caddy-rate-limit

a rate limit plugin for caddy

Travis CI Go Report Card GoDoc

Syntax

Excessive requests will be terminated with an error 429 (Too Many Requests)! And X-RateLimit-RetryAfter header will be returned.

For single resource:

ratelimit methods path rate burst unit
  • methods are the request methods it will match (comma separately)

  • path is the file or directory to apply rate limit

  • rate is the limited request in every time unit (r/s, r/m, r/h) (e.g. 1)

  • burst is the maximum burst size client can exceed; burst >= rate (e.g. 2)

  • unit is the time interval (currently support: second, minute, hour)

For multiple resources:

ratelimit methods rate burst unit {
    whitelist CIDR
    resources
}
  • whitelist is the keyword for whitelist your trusted ips, CIDR is the IP range you don't want to perform rate limit. whitelist is a general rule, it won't target for specific resource.
  • resources is a list of files/directories to apply rate limit, one per line

Note: If you don't want to apply rate limit on some special resources, add ^ in front of the path.

Examples

Limit clients to 2 requests per second (bursts of 3) to any methods and any resources under /r:

ratelimit * /r 2 3 second

Don't perform rate limit if requests come from 1.2.3.4 or 192.168.1.0/30(192.168.1.0 ~ 192.168.1.3), for the listed paths, limit clients to 2 requests per minute (bursts of 2) if the request method is GET or POST and always ignore /dir/app.js:

ratelimit get,post 2 2 minute {
    whitelist 1.2.3.4/32
    whitelist 192.168.1.0/30
    /foo.html
    /api
    ^/dir/app.js
}

Download

curl https://getcaddy.com | bash -s personal http.ratelimit

Docker

docker run -d -p 2016:2016 -v `pwd`/Caddyfile:/go/src/github.com/xuqingfeng/caddy-rate-limit/Caddyfile --name ratelimit xuqingfeng/caddy-rate-limit

Inspired by

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

https://github.com/didip/tollbooth

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRemoteIP

func GetRemoteIP(r *http.Request) (string, error)

GetRemoteIP returns the ip of requester Doesn't care if the ip is real or not

func IsWhitelistIPAddress added in v1.3.0

func IsWhitelistIPAddress(address string, localIPNets []*net.IPNet) bool

IsWhitelistIPAddress check whether an ip is in whitelist

func MatchMethod added in v1.3.0

func MatchMethod(methods, method string) bool

MatchMethod check whether the request method is in the methods list

Types

type CaddyLimiter

type CaddyLimiter struct {
	Keys map[string]*rate.Limiter
	sync.Mutex
}

func NewCaddyLimiter

func NewCaddyLimiter() *CaddyLimiter

func (*CaddyLimiter) Allow

func (cl *CaddyLimiter) Allow(keys []string, rule Rule) bool

Allow is just a shortcut for AllowN

func (*CaddyLimiter) AllowN

func (cl *CaddyLimiter) AllowN(keys []string, rule Rule, n int) bool

AllowN check if n count are allowed for a specific key

func (*CaddyLimiter) RetryAfter

func (cl *CaddyLimiter) RetryAfter(keys []string) time.Duration

RetryAfter return a helper message for client

type RateLimit

type RateLimit struct {
	Next  httpserver.Handler
	Rules []Rule
}

RateLimit is an http.Handler that can limit request rate to specific paths or files

func (RateLimit) ServeHTTP

func (rl RateLimit) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

ServeHTTP is the method handling every request

type Rule

type Rule struct {
	Methods   string
	Rate      int64
	Burst     int
	Unit      string
	Whitelist []string
	Resources []string
}

Rule is a configuration for ratelimit

Jump to

Keyboard shortcuts

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