memory

package
v1.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package memory implements a fast, thread-safe, sharded in-memory rate limiting driver. It supports pluggable algorithm strategies with an automatic background janitor to prune expired rate limit keys per shard and prevent memory leaks.

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 using a 64-sharded lock architecture to minimize lock contention and prevent latency spikes during background cleanup.

Driver is thread-safe and safe for concurrent use by multiple goroutines.

func New

func New(ttl time.Duration) *Driver

New creates and starts a new sharded in-memory rate limiting Driver. The ttl parameter specifies how long inactive keys remain in memory before being pruned by the background janitor. Defaults to 5 minutes if ttl <= 0.

func (*Driver) Allow

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

Allow evaluates the rate limit for a key on its isolated shard without acquiring a global memory lock. It respects context cancellation and returns an error if ctx is cancelled.

func (*Driver) Close

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

Close gracefully stops the background cleanup worker and releases associated resources.

func (*Driver) Name added in v1.2.0

func (d *Driver) Name() string

Name returns the unique identifier string of the memory storage driver.

func (*Driver) Reset added in v1.1.1

func (d *Driver) Reset(ctx context.Context, key string) error

Reset clears the rate limit state entry for the specified key from its memory shard.

Jump to

Keyboard shortcuts

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