catrate

package module
v0.0.0-...-ff69e4d Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package catrate implements multi-window rate limiting per (arbitrary) "category". Rates are applied independently, to all categories, with separate buckets per category. It uses a simple but potentially poorly optimized strategy, involving tracking discrete events, within a sliding window.

It is intended for use cases that don't lend themselves well to any of the more complex solutions, e.g. token buckets, sliding/fixed window counters, or probabilistic rate limiting (i.e. bloom filters).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

func NewLimiter

func NewLimiter(rates map[time.Duration]int) *Limiter

func (*Limiter) Allow

func (x *Limiter) Allow(category any) (time.Time, bool)

Allow is a non-blocking call that attempts to register an event for the given category. True indicates that an event was registered. In all cases, the returned time is the next time that an event can be registered for the given category. If at least one more event may be registered prior to a rate limit being applied (at the current system time), the time will be the zero value.

Jump to

Keyboard shortcuts

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