Documentation
¶
Overview ¶
Package memory implements a fast, thread-safe, in-memory rate limiting driver. It uses a lock-free token bucket implementation with atomic Compare-And-Swap (CAS) operations and a background janitor to prune expired rate limit keys.
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. It manages rate limits in-memory using a sync.Map containing atomic token buckets. A background janitor cleans up inactive buckets periodically to prevent memory leaks.
Driver is safe for concurrent use by multiple goroutines.
func New ¶
New creates and starts a new in-memory rate limiting Driver. The cleanupInterval parameter specifies how often the background janitor runs to scan and delete stale buckets that haven't been refilled or accessed recently. If cleanupInterval is <= 0, it defaults to 1 minute.
func (*Driver) Allow ¶
func (d *Driver) Allow(ctx context.Context, key string, limit int64, window time.Duration) (distlimit.Result, error)
Allow evaluates the rate limit for a key. It retrieves the token bucket corresponding to the key, refines/refills the tokens if the window has passed since the last refill, and decrements a token lock-freely using atomic Compare-And-Swap.