golazy.dev
–
golazy.dev
/
lazylimit
Index
|
Files
package lazylimit ¶
import "golazy.dev/lazylimit"
Functions ¶
func Middleware ¶
Middleware installs limiter in a standard HTTP stack.
func Middleware(limiter Limiter, key func(*http.Request) string) func(http.Handler) http.Handler
Types ¶
type Limiter ¶
Limiter checks whether key can consume one unit.
type Limiter interface {
Allow(context.Context, string) (Result, error)
}
type MemoryLimiter ¶
MemoryLimiter is a fixed-window in-memory limiter.
type MemoryLimiter struct {
Limit int
Window time.Duration
Now func() time.Time
// contains filtered or unexported fields
}
func NewMemory ¶
NewMemory creates an in-memory fixed-window limiter.
func NewMemory(limit int, window time.Duration) *MemoryLimiter
func (limiter *MemoryLimiter) Allow ¶
Allow implements Limiter.
func (limiter *MemoryLimiter) Allow(_ context.Context, key string) (Result, error)
type Result ¶
Result describes one rate-limit check.
type Result struct {
Allowed bool
Remaining int
ResetAfter time.Duration
}
Package lazylimit provides generic rate limiting primitives.
Limiters are keyed by caller-provided strings, so the same package can back HTTP middleware, OAuth endpoints, MCP tools, or application services. The in-memory limiter is intended as a development and single-process backend; applications can provide another Store-compatible limiter for distributed deployments.