Documentation ¶
Index ¶
- Constants
- type RateLimiter
- func (r RateLimiter) BucketTimeout() time.Duration
- func (r RateLimiter) CallsPerInterval() int64
- func (r RateLimiter) Interval() time.Duration
- func (r *RateLimiter) RateLimit(fn RateLimiterFn) error
- func (r *RateLimiter) RateLimitContext(ctx context.Context, fn RateLimiterFn) error
- func (r *RateLimiter) SetBucketTimeout(timeout time.Duration) *RateLimiter
- func (r *RateLimiter) SetCallsPerInterval(n int64) *RateLimiter
- func (r *RateLimiter) SetInterval(interval time.Duration) *RateLimiter
- func (r *RateLimiter) Wrap(fn RateLimiterFn) WrappedFn
- func (r *RateLimiter) WrapContext(fn RateLimiterFn) WrappedFnContext
- type RateLimiterFn
- type WrappedFn
- type WrappedFnContext
Constants ¶
const DefaultBucketTimeout = time.Minute
DefaultBucketTimeout is the default amount of time that a call to RateLimiter.RateLimit will wait to acquire some bucket space before timing out and returning a context.DeadlineExceeded error.
Note that this does *not* apply to RateLimiter.RateLimitContext when called with a programmer-created context.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is a fancy little struct which can do everything you want it to and more in the realm of rate limiting.
func NewRateLimiter ¶
func NewRateLimiter(callsPerInterval int64, interval time.Duration) *RateLimiter
NewRateLimiter returns a RateLimiter configured with the number of allowed function calls per interval, as well as the interval.
Examples: - api A allows 250 calls per second. Call NewRateLimiter(250, time.Second). - api B allowed 50 calls per 5 minutes. Call NewRateLimiter(50, 5*time.Minute).
func (RateLimiter) BucketTimeout ¶
func (r RateLimiter) BucketTimeout() time.Duration
BucketTimeout returns the current configured timeout duration used for non-contexed calls to RateLimit, after which deadline the context will return a context.DeadlineExceeded error.
By default, this is set to DefaultBucketTimeout (1 minute).
func (RateLimiter) CallsPerInterval ¶
func (r RateLimiter) CallsPerInterval() int64
CallsPerInterval returns the current number of calls allowed to be made during a RateLimiter's interval.
func (RateLimiter) Interval ¶
func (r RateLimiter) Interval() time.Duration
Interval returns the current interval for a RateLimiter.
func (*RateLimiter) RateLimit ¶
func (r *RateLimiter) RateLimit(fn RateLimiterFn) error
RateLimit calls RateLimitContext using a "default" context, which is a context.Context using the RateLimiter's current bucket timeout as the time.Duration passed to context.WithTimeout (by default, this is set to DefaultBucketTimeout).
Param fn: function with no parameters which returns an error (see RateLimiterFn). This is usually wrapped around the function call one is actually making.
Return: error, if one occurs either from calling fn() or from the context. Otherwise, any error returned from calling fn() is returned.
func (*RateLimiter) RateLimitContext ¶
func (r *RateLimiter) RateLimitContext(ctx context.Context, fn RateLimiterFn) error
RateLimitContext calls the passed function fn using the passed context.Context. If the passed context's deadline is exceeded while waiting to acquire bucket space, a wrapped error is returned. Otherwise, the result of calling fn() is returned.
func (*RateLimiter) SetBucketTimeout ¶
func (r *RateLimiter) SetBucketTimeout(timeout time.Duration) *RateLimiter
SetBucketTimeout sets the timeout duration used for non-contexed calls to RateLimit, after which deadline the context will return a context.DeadlineExceeded error. Note that this function will block the RateLimiter's other functions until it finishes.
func (*RateLimiter) SetCallsPerInterval ¶
func (r *RateLimiter) SetCallsPerInterval(n int64) *RateLimiter
SetCallsPerInterval sets the number of calls allowed to be made during the configured interval. Note that this function will block the RateLimiter's other functions until it finishes.
func (*RateLimiter) SetInterval ¶
func (r *RateLimiter) SetInterval(interval time.Duration) *RateLimiter
SetInterval sets the RateLimiter's interval within which the RateLimiter's callsPerInterval number of calls are allowed to be made. Note that this function will block the RateLimiter's other functions until it finishes.
func (*RateLimiter) Wrap ¶
func (r *RateLimiter) Wrap(fn RateLimiterFn) WrappedFn
Wrap wraps the passed function within another function, which no parameters and calls RateLimit using fn(). Helpful for building functions which are "preloaded" with a rate limiter, rather than calling RateLimit around every function call.
func (*RateLimiter) WrapContext ¶
func (r *RateLimiter) WrapContext(fn RateLimiterFn) WrappedFnContext
WrapContext wraps the passed function within another function, which takes a single context.Context parameter and calls RateLimitContext using fn() and that context. Helpful for building functions which are "preloaded" with a rate limiter, rather than calling RateLimitContext around every function call.
type RateLimiterFn ¶
type RateLimiterFn func() error
RateLimiterFn is a function which takes no parameters, and returns an error. It represents the function type passed to RateLimiter.RateLimit and RateLimiter.RateLimitContext.
type WrappedFn ¶
type WrappedFn func() error
WrappedFn represents a function wrapped by a call to RateLimiter.RateLimit.
type WrappedFnContext ¶
WrappedFnContext represents a function wrapped by a call to RateLimiter.RateLimitContext.