sliding_window_counter

package
v0.0.0-...-77fbaad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package fixed_window_counter provides a rate limiter implemented by sliding window counter algorithm.

Index

Constants

Inf is the infinite rate limit; it allows all events.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limit

type Limit int64

Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter controls how frequently events are allowed to happen. The zero value is a valid Limiter, but it will reject all events.

RateLimiter has two main method: Allow and Wait

If the count of actionKey is greater or equal to the limit in a period time, Allow returns false

If the count of actionKey is greater or equal to the limit in a period time, Wait blocks until the count of actionKey is less than the limit or its associated context.Context is canceled.

func NewRateLimiter

func NewRateLimiter(client *redis.Client, limit Limit, actionKey string, period time.Duration,
	interval time.Duration) *RateLimiter

NewRateLimiter returns a new RateLimiter that allows the action up to happening limit times in a period time.

func (*RateLimiter) Allow

func (limiter *RateLimiter) Allow(ctx context.Context) bool

Allow reports whether the action exceed the rate limit. Use this method if you intend to drop / skip action that exceed the rate limit. Otherwise use Wait.

func (*RateLimiter) Wait

func (limiter *RateLimiter) Wait(ctx context.Context) bool

Wait reports whether the action exceed the rate limit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL