Documentation
¶
Index ¶
- Variables
- type AutoLimiter
- func (e *AutoLimiter) Add(key string, opts ...AutoLimiterOption) error
- func (e *AutoLimiter) AddAndTake(key string, opts ...AutoLimiterOption) error
- func (e *AutoLimiter) Remove(key string)
- func (e *AutoLimiter) SetRate(key string, maxCount uint, duration time.Duration) error
- func (e *AutoLimiter) Stop(keys ...string)
- func (e *AutoLimiter) Take(key string) error
- func (e *AutoLimiter) TakeContext(ctx context.Context, key string) error
- type AutoLimiterOption
- type Limiter
- type MultiLimiter
- type Options
- type Strategy
Constants ¶
This section is empty.
Variables ¶
var ( ErrAutoKeyAlreadyExists = errors.New("key already exists") ErrAutoKeyMissing = errors.New("key does not exist") )
var ( ErrKeyAlreadyExists = errkit.New("multilimiter: key already exists") ErrKeyMissing = errkit.New("multilimiter: key does not exist") )
var ErrLimiterStopped = errors.New("limiter stopped")
ErrLimiterStopped is returned by TakeContext when the limiter stopped before a token was taken.
Functions ¶
This section is empty.
Types ¶
type AutoLimiter ¶ added in v0.0.83
type AutoLimiter struct {
// contains filtered or unexported fields
}
AutoLimiter is an improved version of MultiLimiter with better memory management
func NewAutoLimiter ¶ added in v0.0.83
func NewAutoLimiter(ctx context.Context, opts ...AutoLimiterOption) *AutoLimiter
NewAutoLimiter creates a new auto limiter instance using functional options
func (*AutoLimiter) Add ¶ added in v0.0.83
func (e *AutoLimiter) Add(key string, opts ...AutoLimiterOption) error
Add creates a new rate limiter with custom settings (only for keys that need specific limits)
func (*AutoLimiter) AddAndTake ¶ added in v0.0.83
func (e *AutoLimiter) AddAndTake(key string, opts ...AutoLimiterOption) error
AddAndTake adds a key with custom settings if not present and then takes a token
func (*AutoLimiter) Remove ¶ added in v0.0.83
func (e *AutoLimiter) Remove(key string)
Remove completely removes a key and its options
func (*AutoLimiter) SetRate ¶ added in v0.1.0
SetRate changes key's rate to maxCount tokens per duration in place, creating the key if absent. Takes already waiting on the key stay on its bucket and receive tokens at the new rate. Like Limiter.SetLimit, the new count applies from the bucket's next refill.
func (*AutoLimiter) Stop ¶ added in v0.0.83
func (e *AutoLimiter) Stop(keys ...string)
Stop internal limiters with defined keys or all if no key is provided
func (*AutoLimiter) Take ¶ added in v0.0.83
func (e *AutoLimiter) Take(key string) error
Take one token from bucket - creates limiter automatically if it doesn't exist. It returns ErrLimiterStopped if the key's limiter stops while it waits.
func (*AutoLimiter) TakeContext ¶ added in v0.1.0
func (e *AutoLimiter) TakeContext(ctx context.Context, key string) error
TakeContext takes one token from key's bucket like Take, or returns when ctx is canceled.
type AutoLimiterOption ¶ added in v0.0.83
type AutoLimiterOption func(*AutoLimiter)
AutoLimiterOption is a function that configures the AutoLimiter
func WithDuration ¶ added in v0.0.83
func WithDuration(duration time.Duration) AutoLimiterOption
WithDuration sets the duration for the rate limiter
func WithMaxCount ¶ added in v0.0.83
func WithMaxCount(maxCount uint) AutoLimiterOption
WithMaxCount sets the maximum count for the rate limiter
func WithUnlimited ¶ added in v0.0.83
func WithUnlimited() AutoLimiterOption
WithUnlimited sets the limiter to unlimited mode
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter allows a burst of request during the defined duration
func NewLeakyBucket ¶ added in v0.0.43
NewLeakyBucket creates a limiter that uses golang.org/x/time/rate.
func NewUnlimited ¶
NewUnlimited creates a limiter whose Take never waits and CanTake returns true until SetLimit or SetDuration is called. It creates no timer, token channel, or background goroutine until SetLimit or SetDuration configures a finite bucket. GetLimit initially returns math.MaxUint32.
func (*Limiter) SetDuration ¶ added in v0.0.33
SetDuration changes the refill interval. For burst buckets, it panics if d is not positive. On an unlimited limiter, it starts a finite bucket using the current limit.
func (*Limiter) SetLimit ¶ added in v0.0.33
SetLimit changes the rate limit. Burst buckets apply it at the next refill. On an unlimited limiter, it starts a finite bucket with a 1 ms interval unless SetDuration has already changed the interval.
func (*Limiter) Stop ¶ added in v0.0.4
func (limiter *Limiter) Stop()
Stop the rate limiter canceling the internal context
type MultiLimiter ¶ added in v0.0.3
type MultiLimiter struct {
// contains filtered or unexported fields
}
MultiLimiter is wrapper around Limiter than can limit based on a key
func NewMultiLimiter ¶ added in v0.0.3
func NewMultiLimiter(ctx context.Context, opts *Options) (*MultiLimiter, error)
NewMultiLimiter : Limits
func (*MultiLimiter) Add ¶ added in v0.0.3
func (m *MultiLimiter) Add(opts *Options) error
Add new bucket with key
func (*MultiLimiter) AddAndTake ¶ added in v0.0.6
func (m *MultiLimiter) AddAndTake(opts *Options)
AddAndTake adds key if not present and then takes token from bucket
func (*MultiLimiter) CanTake ¶ added in v0.0.9
func (m *MultiLimiter) CanTake(key string) bool
CanTake checks if the rate limiter with the given key has any token
func (*MultiLimiter) GetLimit ¶ added in v0.0.3
func (m *MultiLimiter) GetLimit(key string) (uint, error)
GetLimit returns current ratelimit of given key
func (*MultiLimiter) Stop ¶ added in v0.0.4
func (m *MultiLimiter) Stop(keys ...string)
Stop internal limiters with defined keys or all if no key is provided
func (*MultiLimiter) Take ¶ added in v0.0.3
func (m *MultiLimiter) Take(key string) error
Take one token from bucket returns error if key not present