hecate_plugin_ratelimit (hecate_sdk v0.6.2)
View SourceToken bucket rate limiter for plugins.
In-memory, per-process rate limiting using ETS. Suitable for API endpoint protection.
Usage: %% Create a bucket: 10 requests per second hecate_plugin_ratelimit:new(my_api_limit, 10, 1000).
%% Check rate limit (returns ok or {error, retry_after_ms}) case hecate_plugin_ratelimit:check(my_api_limit, ClientId) of ok -> handle_request(); {error, RetryAfterMs} -> reply_429(RetryAfterMs) end.
Summary
Functions
Check if a request is allowed for a given key. Returns 'ok' if allowed, or {error, RetryAfterMs} if rate limited.
Create a new rate limiter bucket. - Name: atom identifier for this limiter - MaxTokens: maximum tokens (requests) allowed per window - WindowMs: time window in milliseconds for token refill
Reset rate limit for a specific key.
Functions
-spec check(Name :: atom(), Key :: term()) -> ok | {error, pos_integer()}.
Check if a request is allowed for a given key. Returns 'ok' if allowed, or {error, RetryAfterMs} if rate limited.
-spec new(Name :: atom(), MaxTokens :: pos_integer(), WindowMs :: pos_integer()) -> ok.
Create a new rate limiter bucket. - Name: atom identifier for this limiter - MaxTokens: maximum tokens (requests) allowed per window - WindowMs: time window in milliseconds for token refill
Reset rate limit for a specific key.