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 ¶
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 }
Output:
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 is the size to use for the in-memory map. Go will // automatically expand the buffer, but choosing higher number can trade // memory consumption for performance as it limits the number of times the map // needs to expand. The default value is 4096. InitialAlloc int }
Config is used as input to New. It defines the behavior of the storage system.
Click to show internal directories.
Click to hide internal directories.