ratelimiting

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimiting provides a per-key rate limiter interface using the token bucket algorithm.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RateLimiter

type RateLimiter interface {
	Allow(ctx context.Context, key string) (bool, error)
	Close() error
}

RateLimiter limits the rate of operations per key.

func NewInMemoryRateLimiter

func NewInMemoryRateLimiter(metricsProvider metrics.Provider, requestsPerSec float64, burstSize int) (RateLimiter, error)

NewInMemoryRateLimiter returns a RateLimiter that uses per-key limiters in memory.

Example
package main

import (
	"context"
	"fmt"

	"github.com/primandproper/platform/ratelimiting"
)

func main() {
	limiter, err := ratelimiting.NewInMemoryRateLimiter(nil, 10.0, 5)
	if err != nil {
		panic(err)
	}

	var allowed bool
	allowed, err = limiter.Allow(context.Background(), "user-123")
	if err != nil {
		panic(err)
	}

	fmt.Println(allowed)
}
Output:
true

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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