memory

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package memory implements a fast, thread-safe, in-memory rate limiting driver. It uses a lock-free token bucket implementation with atomic Compare-And-Swap (CAS) operations and a background janitor to prune expired rate limit keys.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

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

Driver implements the distlimit.Driver interface. It manages rate limits in-memory using a sync.Map containing atomic token buckets. A background janitor cleans up inactive buckets periodically to prevent memory leaks.

Driver is safe for concurrent use by multiple goroutines.

func New

func New(cleanupInterval time.Duration, opts ...Option) *Driver

New creates and starts a new in-memory rate limiting Driver. The cleanupInterval parameter specifies how often the background janitor runs to scan and delete stale buckets that haven't been refilled or accessed recently. If cleanupInterval is <= 0, it defaults to 1 minute.

func (*Driver) Allow

func (d *Driver) Allow(ctx context.Context, key string, limit int64, window time.Duration) (distlimit.Result, error)

Allow evaluates the rate limit for a key. It retrieves the token bucket corresponding to the key, refines/refills the tokens if the window has passed since the last refill, and decrements a token lock-freely using atomic Compare-And-Swap.

func (*Driver) Close

func (d *Driver) Close(ctx context.Context) error

Close stops the background janitor goroutine and cleans up resources.

type Option

type Option func(*Driver)

Option configures functional parameters for the memory Driver.

Jump to

Keyboard shortcuts

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