Help us shape the future of Go! Take the 2026 Go Developer Survey

memory

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package memory implements explicit single-process fixed-window rate limiting. It never evicts live quotas or becomes an automatic Redis failure fallback.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

Backend owns at most maxEntries live buckets. Key sizes and each bucket's state are bounded. Clock.Now must return promptly. Hot buckets are O(1); expired buckets are reclaimed lazily on access and, when a new bucket needs capacity, from an expiry-ordered heap. A backward clock reading is clamped to the latest reading already observed, so it can neither reopen old quota nor fail.

func New

func New(maxEntries int, source clock.Clock) (*Backend, error)

New creates a bounded in-memory backend using source as its clock; capacity must be positive.

func (*Backend) ClearRateLimit

func (b *Backend) ClearRateLimit(ctx context.Context, key ratelimit.Key) (bool, error)

ClearRateLimit removes the key's live bucket; true means one existed.

func (*Backend) Close

func (b *Backend) Close() error

Close releases this local authority. Stop its callers first; no background work is started by this adapter or the borrowing Store.

func (*Backend) FoundryAdapter

func (*Backend) FoundryAdapter(frameworkadapter.Seal)

FoundryAdapter marks the backend as a framework-owned adapter.

func (*Backend) PeekRateLimit

func (b *Backend) PeekRateLimit(ctx context.Context, key ratelimit.Key, limit ratelimit.Limit, cost uint32) (ratelimit.Decision, error)

PeekRateLimit reports the decision without consuming capacity or storing state.

func (*Backend) RateLimit

func (b *Backend) RateLimit(ctx context.Context, key ratelimit.Key, limit ratelimit.Limit, cost uint32) (ratelimit.Decision, error)

RateLimit atomically evaluates and, on admission, consumes cost in the key's fixed window.

Jump to

Keyboard shortcuts

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