Documentation ¶
Index ¶
- Constants
- Variables
- func GetIP(r *http.Request, options ...Options) net.IP
- func GetIPWithMask(r *http.Request, options ...Options) net.IP
- type Context
- type Limiter
- func (limiter *Limiter) Get(ctx context.Context, key string) (Context, error)
- func (limiter *Limiter) GetIP(r *http.Request) net.IP
- func (limiter *Limiter) GetIPKey(r *http.Request) string
- func (limiter *Limiter) GetIPWithMask(r *http.Request) net.IP
- func (limiter *Limiter) Increment(ctx context.Context, key string, count int64) (Context, error)
- func (limiter *Limiter) Peek(ctx context.Context, key string) (Context, error)
- func (limiter *Limiter) Reset(ctx context.Context, key string) (Context, error)
- type Option
- type Options
- type Rate
- type Store
- type StoreOptions
Constants ¶
const ( // DefaultPrefix is the default prefix to use for the key in the store. DefaultPrefix = "limiter" // DefaultMaxRetry is the default maximum number of key retries under // race condition (mainly used with database-based stores). DefaultMaxRetry = 3 // DefaultCleanUpInterval is the default time duration for cleanup. DefaultCleanUpInterval = 30 * time.Second )
Variables ¶
var ( // DefaultIPv4Mask defines the default IPv4 mask used to obtain user IP. DefaultIPv4Mask = net.CIDRMask(32, 32) // DefaultIPv6Mask defines the default IPv6 mask used to obtain user IP. DefaultIPv6Mask = net.CIDRMask(128, 128) )
Functions ¶
func GetIP ¶ added in v3.1.0
GetIP returns IP address from request. If options is defined and either TrustForwardHeader is true or ClientIPHeader is defined, it will lookup IP in HTTP headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
func GetIPWithMask ¶ added in v3.1.0
GetIPWithMask returns IP address from request by applying a mask. If options is defined and either TrustForwardHeader is true or ClientIPHeader is defined, it will lookup IP in HTTP headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
Types ¶
type Limiter ¶
Limiter is the limiter instance.
func (*Limiter) GetIP ¶
GetIP returns IP address from request. If options is defined and either TrustForwardHeader is true or ClientIPHeader is defined, it will lookup IP in HTTP headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
func (*Limiter) GetIPKey ¶
GetIPKey extracts IP from request and returns hashed IP to use as store key. If options is defined and either TrustForwardHeader is true or ClientIPHeader is defined, it will lookup IP in HTTP headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
func (*Limiter) GetIPWithMask ¶
GetIPWithMask returns IP address from request by applying a mask. If options is defined and either TrustForwardHeader is true or ClientIPHeader is defined, it will lookup IP in HTTP headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
func (*Limiter) Increment ¶ added in v3.9.0
Increment increments the limit by given count & gives back the new limit for given identifier
type Option ¶
type Option func(*Options)
Option is a functional option.
func WithClientIPHeader ¶ added in v3.10.0
WithClientIPHeader will configure the limiter to use a custom header to obtain user IP. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
func WithIPv4Mask ¶
WithIPv4Mask will configure the limiter to use given mask for IPv4 address.
func WithIPv6Mask ¶
WithIPv6Mask will configure the limiter to use given mask for IPv6 address.
func WithTrustForwardHeader ¶
WithTrustForwardHeader will configure the limiter to trust X-Real-IP and X-Forwarded-For headers. Please be advised that using this option could be insecure (ie: spoofed) if your reverse proxy is not configured properly to forward a trustworthy client IP. Please read the section "Limiter behind a reverse proxy" in the README for further information.
type Options ¶
type Options struct { // IPv4Mask defines the mask used to obtain a IPv4 address. IPv4Mask net.IPMask // IPv6Mask defines the mask used to obtain a IPv6 address. IPv6Mask net.IPMask // TrustForwardHeader enable parsing of X-Real-IP and X-Forwarded-For headers to obtain user IP. // Please be advised that using this option could be insecure (ie: spoofed) if your reverse // proxy is not configured properly to forward a trustworthy client IP. // Please read the section "Limiter behind a reverse proxy" in the README for further information. TrustForwardHeader bool // ClientIPHeader defines a custom header (likely defined by your CDN or Cloud provider) to obtain user IP. // If configured, this option will override "TrustForwardHeader" option. // Please be advised that using this option could be insecure (ie: spoofed) if your reverse // proxy is not configured properly to forward a trustworthy client IP. // Please read the section "Limiter behind a reverse proxy" in the README for further information. ClientIPHeader string }
Options are limiter options.
type Rate ¶
Rate is the rate.
func NewRateFromFormatted ¶
NewRateFromFormatted returns the rate from the formatted version.
type Store ¶
type Store interface { // Get returns the limit for given identifier. Get(ctx context.Context, key string, rate Rate) (Context, error) // Peek returns the limit for given identifier, without modification on current values. Peek(ctx context.Context, key string, rate Rate) (Context, error) // Reset resets the limit to zero for given identifier. Reset(ctx context.Context, key string, rate Rate) (Context, error) // Increment increments the limit by given count & gives back the new limit for given identifier Increment(ctx context.Context, key string, count int64, rate Rate) (Context, error) }
Store is the common interface for limiter stores.
type StoreOptions ¶
type StoreOptions struct { // Prefix is the prefix to use for the key. Prefix string // MaxRetry is the maximum number of retry under race conditions on redis store. // Deprecated: this option is no longer required since all operations are atomic now. MaxRetry int // CleanUpInterval is the interval for cleanup (run garbage collection) on stale entries on memory store. // Setting this to a low value will optimize memory consumption, but will likely // reduce performance and increase lock contention. // Setting this to a high value will maximum throughput, but will increase the memory footprint. CleanUpInterval time.Duration }
StoreOptions are options for store.