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 ¶
Types ¶
type Limiter ¶
Limiter is the limiter instance.
func (*Limiter) GetIPKey ¶
GetIPKey extracts IP from request and returns hashed IP to use as store key.
func (*Limiter) GetIPWithMask ¶
GetIPWithMask returns IP address from request by applying a mask.
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 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.
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.
TrustForwardHeader bool
}
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.