Documentation
¶
Overview ¶
Package rl - Because sometimes you need to tell your code "slow down there, buddy"
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecuteRateLimited ¶
func ExecuteRateLimited[T any](rl *RateLimiter, ctx context.Context, operation func() (T, error)) (T, error)
ExecuteRateLimited is like Execute but for functions that actually return something
Types ¶
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is like a bouncer for your function calls Keeps them in line and makes sure they don't cause a scene
func DefaultRateLimiter ¶
func DefaultRateLimiter() *RateLimiter
DefaultRateLimiter creates a rate limiter for the indecisive 1-second interval, 10 passes because why not
func DefaultRateLimiterWithContext ¶
func DefaultRateLimiterWithContext(ctx context.Context) *RateLimiter
DefaultRateLimiterWithContext is like DefaultRateLimiter but with a curfew
func NewRateLimiter ¶
func NewRateLimiter(interval time.Duration, batchSize int) *RateLimiter
NewRateLimiter creates a new function traffic controller interval: how often we hand out passes batchSize: how many passes we give out at once
func NewRateLimiterWithContext ¶
func NewRateLimiterWithContext(ctx context.Context, interval time.Duration, batchSize int) *RateLimiter
NewRateLimiterWithContext is like NewRateLimiter but with a bedtime
func (*RateLimiter) BatchSize ¶
func (rl *RateLimiter) BatchSize() int
BatchSize tells you how many get in at once
func (*RateLimiter) Ctx ¶
func (rl *RateLimiter) Ctx() context.Context
Ctx returns the rate limiter's context In case you need more ways to shut things down
func (*RateLimiter) Execute ¶
func (rl *RateLimiter) Execute(ctx context.Context, operation func() error) error
Execute runs your function when it's allowed to Return an error when your function misbehaves
func (*RateLimiter) Interval ¶
func (rl *RateLimiter) Interval() time.Duration
Interval tells you how long you have to wait
func (*RateLimiter) Tokens ¶
func (rl *RateLimiter) Tokens() chan struct{}
Tokens returns the channel controlling access But seriously, don't mess with this directly