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) 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 ¶
View Source
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 ¶
View Source
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.
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) }
Store is the common interface for limiter stores.
Click to show internal directories.
Click to hide internal directories.