ratelimit

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Example (NewBurstLimiter)
now, _ := time.Parse(time.RFC3339, "2024-09-24T10:00:00.00Z")
{
	cfg := conf.Rate{Events: 10, OverTime: time.Second * 20}
	rl := NewBurstLimiter(cfg)
	cur := now
	for i := 0; i < 20; i++ {
		allowed := rl.AllowAt(cur)
		fmt.Printf("%-5v @ %v\n", allowed, cur)
		cur = cur.Add(time.Second * 5)
	}
}
Output:
true  @ 2024-09-24 10:00:00 +0000 UTC
true  @ 2024-09-24 10:00:05 +0000 UTC
true  @ 2024-09-24 10:00:10 +0000 UTC
true  @ 2024-09-24 10:00:15 +0000 UTC
true  @ 2024-09-24 10:00:20 +0000 UTC
true  @ 2024-09-24 10:00:25 +0000 UTC
true  @ 2024-09-24 10:00:30 +0000 UTC
true  @ 2024-09-24 10:00:35 +0000 UTC
true  @ 2024-09-24 10:00:40 +0000 UTC
true  @ 2024-09-24 10:00:45 +0000 UTC
true  @ 2024-09-24 10:00:50 +0000 UTC
true  @ 2024-09-24 10:00:55 +0000 UTC
true  @ 2024-09-24 10:01:00 +0000 UTC
false @ 2024-09-24 10:01:05 +0000 UTC
false @ 2024-09-24 10:01:10 +0000 UTC
false @ 2024-09-24 10:01:15 +0000 UTC
true  @ 2024-09-24 10:01:20 +0000 UTC
false @ 2024-09-24 10:01:25 +0000 UTC
false @ 2024-09-24 10:01:30 +0000 UTC
false @ 2024-09-24 10:01:35 +0000 UTC
Example (NewIntervalLimiter)
now, _ := time.Parse(time.RFC3339, "2024-09-24T10:00:00.00Z")
cfg := conf.Rate{Events: 100, OverTime: time.Hour * 24}
rl := NewIntervalLimiter(cfg)
rl.last = now

cur := now
allowed := 0

for days := 0; days < 2; days++ {
	// First 100 events succeed.
	for i := 0; i < 100; i++ {
		allow := rl.allowAt(cur)
		cur = cur.Add(time.Second)

		if !allow {
			fmt.Printf("false @ %v after %v events... [FAILED]\n", cur, allowed)
			return
		}
		allowed++
	}
	fmt.Printf("true  @ %v for last %v events...\n", cur, allowed)

	// We try hourly until it allows us to make requests again.
	denied := 0
	for i := 0; i < 23; i++ {
		cur = cur.Add(time.Hour)
		allow := rl.AllowAt(cur)
		if allow {
			fmt.Printf("true  @ %v before quota reset... [FAILED]\n", cur)
			return
		}
		denied++
	}
	fmt.Printf("false @ %v for last %v events...\n", cur, denied)

	cur = cur.Add(time.Hour)
}
Output:
true  @ 2024-09-24 10:01:40 +0000 UTC for last 100 events...
false @ 2024-09-25 09:01:40 +0000 UTC for last 23 events...
true  @ 2024-09-25 10:03:20 +0000 UTC for last 200 events...
false @ 2024-09-26 09:03:20 +0000 UTC for last 23 events...

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BurstLimiter

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

BurstLimiter wraps the golang.org/x/time/rate package.

func NewBurstLimiter

func NewBurstLimiter(r conf.Rate) *BurstLimiter

NewBurstLimiter returns a rate limiter configured using the given conf.Rate.

The returned Limiter will be configured with a token bucket containing a single token, which will fill up at a rate of 1 event per r.OverTime with an initial burst amount of r.Events.

For example:

  • 1/10s is 1 events per 10 seconds with burst of 1.
  • 1/2s is 1 events per 2 seconds with burst of 1.
  • 10/10s is 1 events per 10 seconds with burst of 10.

If Rate.Events is <= 0, the burst amount will be set to 1.

See Example_newBurstLimiter for a visualization.

func (*BurstLimiter) Allow

func (l *BurstLimiter) Allow() bool

Allow implements Limiter by calling AllowAt with the current time.

func (*BurstLimiter) AllowAt

func (l *BurstLimiter) AllowAt(at time.Time) bool

AllowAt implements Limiter by calling the underlying x/time/rate.Limiter with the given time.

type IntervalLimiter

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

IntervalLimiter will limit the number of calls to Allow per interval.

func NewIntervalLimiter

func NewIntervalLimiter(r conf.Rate) *IntervalLimiter

NewIntervalLimiter returns a rate limiter using the given conf.Rate.

func (*IntervalLimiter) Allow

func (rl *IntervalLimiter) Allow() bool

Allow implements Limiter by calling AllowAt with the current time.

func (*IntervalLimiter) AllowAt

func (rl *IntervalLimiter) AllowAt(at time.Time) bool

AllowAt implements Limiter by checking if the current number of permitted events within this interval would permit 1 additional event at the current time.

When called with a time outside the current active interval the counter is reset, meaning it can be vulnerable at the edge of it's intervals so avoid small intervals.

type Limiter

type Limiter interface {

	// Allow should return true if an event should be allowed at the time
	// which it was called, or false otherwise.
	Allow() bool

	// AllowAt should return true if an event should be allowed at the given
	// time, or false otherwise.
	AllowAt(at time.Time) bool
}

Limiter is the interface implemented by rate limiters.

Implementations of Limiter must be safe for concurrent use.

func New

func New(r conf.Rate) Limiter

New returns a new Limiter based on the given config.

When the type is conf.BurstRateType it returns a BurstLimiter, otherwise New returns an IntervalLimiter.

Jump to

Keyboard shortcuts

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