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

memorystore

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 68

Documentation

Overview

Package memorystore defines an in-memory storage system for limiting.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(c *Config) (limiter.Store, error)

New creates an in-memory rate limiter that uses a bucketing model to limit the number of permitted events over an interval. It's optimized for runtime and memory efficiency.

Example
package main

import (
	"context"
	"log"
	"time"

	"github.com/sethvargo/go-limiter/memorystore"
)

func main() {
	ctx := context.Background()

	store, err := memorystore.New(&memorystore.Config{
		Tokens:   15,
		Interval: time.Minute,
	})
	if err != nil {
		log.Fatal(err)
	}
	defer store.Close(ctx)

	limit, remaining, reset, ok, err := store.Take(ctx, "my-key")
	if err != nil {
		log.Fatal(err)
	}
	_, _, _, _ = limit, remaining, reset, ok
}

Types

type Config

type Config struct {
	// Tokens is the number of tokens to allow per interval. The default value is
	// 1.
	Tokens uint64

	// Interval is the time interval upon which to enforce rate limiting. The
	// default value is 1 second.
	Interval time.Duration

	// SweepInterval is the rate at which to run the garabage collection on stale
	// entries. Setting this to a low value will optimize memory consumption, but
	// will likely reduce performance and increase lock contention. Setting this
	// to a high value will maximum throughput, but will increase the memory
	// footprint. This can be tuned in combination with SweepMinTTL to control how
	// long stale entires are kept. The default value is 6 hours.
	SweepInterval time.Duration

	// SweepMinTTL is the minimum amount of time a session must be inactive before
	// clearing it from the entries. There's no validation, but this should be at
	// least as high as your rate limit, or else the data store will purge records
	// before they limit is applied. The default value is 12 hours.
	SweepMinTTL time.Duration

	// InitialAlloc previously pre-sized the in-memory map. It is retained for
	// backward compatibility but no longer has any effect: the store now uses a
	// sync.Map, which does not support pre-sizing.
	InitialAlloc int

	// DisablePurge disables the purge operation. WARNING: this will cause
	// unbounded memory growth. Do not enable unless you have a fixed number of
	// buckets.
	DisablePurge bool
}

Config is used as input to New. It defines the behavior of the storage system.

Jump to

Keyboard shortcuts

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