Documentation
¶
Overview ¶
Package memory implements a fast, thread-safe, sharded in-memory rate limiting driver. It supports pluggable algorithm strategies with an automatic background janitor to prune expired rate limit keys per shard and prevent memory leaks.
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 using a 64-sharded lock architecture to minimize lock contention and prevent latency spikes during background cleanup.
Driver is thread-safe and safe for concurrent use by multiple goroutines.
func New ¶
New creates and starts a new sharded in-memory rate limiting Driver. The ttl parameter specifies how long inactive keys remain in memory before being pruned by the background janitor. Defaults to 5 minutes if ttl <= 0.
func (*Driver) Allow ¶
func (d *Driver) Allow(ctx context.Context, key string, limit int64, window time.Duration, alg algorithm.Algorithm) (algorithm.Result, error)
Allow evaluates the rate limit for a key on its isolated shard without acquiring a global memory lock. It respects context cancellation and returns an error if ctx is cancelled.
func (*Driver) Close ¶
Close gracefully stops the background cleanup worker and releases associated resources.