memory

package
v2.6.7 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package memory provides an in-process limiter backend.

It implements the same token bucket semantics as the redis backend and is intended for single-process scenarios, tests and local degradation. Buckets are kept in a sharded map and expired buckets are evicted periodically.

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
}

Limiter is an in-process token bucket limiter for one scene.

It is stateless with respect to dimension keys: the same Limiter can be shared by concurrent callers and different keys.

func (*Limiter) Allow

func (l *Limiter) Allow(_ context.Context, key string, n ...int) (limiter.Result, error)

Allow takes n tokens from the bucket identified by key and reports the decision. n defaults to 1. A denied request is not an error: it is reported by Result.Allowed.

type Maker

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

Maker is a memory limiter maker.

func NewMaker

func NewMaker(opts ...Option) *Maker

NewMaker creates a memory limiter maker and starts the expired bucket cleanup.

A cleanup interval of zero or less is clamped to the default of one minute. The cleanup goroutine stops when Close is called.

func (*Maker) Close

func (m *Maker) Close() error

Close closes the maker and waits for the cleanup goroutine to exit.

func (*Maker) Make

func (m *Maker) Make(name string, rule limiter.Rule) (limiter.Limiter, error)

Make creates a limiter for the named scene and rule.

The name identifies the scene and must be stable for the lifetime of the maker. A scene keeps the rule it was created with, so creating it again with the same normalized rule reuses the existing bucket state, while a different rule reports errors.ErrConflictLimiterRule.

type Option

type Option func(o *options)

Option configures the memory limiter maker.

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) Option

WithCleanupInterval sets the interval of the expired bucket cleanup. A value of zero or less is clamped to the default of one minute.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the key prefix. The final key is prefix + ":" + name + ":" + key; an empty prefix disables the prefix.

Jump to

Keyboard shortcuts

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