GnuplotEx.Channel.RateLimiter (gnuplot_ex v0.5.0)
Token bucket rate limiter for plot streaming.
Provides backpressure for high-frequency plot updates to prevent overwhelming clients or consuming excessive resources.
Algorithm
Uses a token bucket algorithm:
- Each plot_id has its own bucket
- Tokens regenerate at
rateper second - Bucket can hold up to
bursttokens - Each update consumes one token
Configuration
config :gnuplot_ex,
channel_rate: 10, # updates per second
channel_burst: 20 # max burst sizeExample
# Check if allowed (returns remaining tokens or error)
case RateLimiter.check("sensor-1") do
{:ok, remaining} -> push_update(...)
{:error, :rate_limited, retry_after_ms} -> send_rate_limit_error(...)
end
# Always allow (for admin/bypass)
RateLimiter.allow("admin-plot")
Summary
Functions
Force allow an update, bypassing rate limits.
Check if a plot update is allowed under rate limits.
Returns a specification to start this module under a supervisor.
Reset rate limit state for a plot.
Start the rate limiter process.
Get rate limiter stats.
Get current token count for a plot.
Functions
Force allow an update, bypassing rate limits.
Useful for admin actions or priority updates.
@spec check( String.t(), keyword() ) :: {:ok, non_neg_integer()} | {:error, :rate_limited, pos_integer()}
Check if a plot update is allowed under rate limits.
Returns:
{:ok, remaining_tokens}- Update allowed{:error, :rate_limited, retry_after_ms}- Rate limited, wait before retry
Example
case RateLimiter.check("my-plot") do
{:ok, _} -> proceed_with_update()
{:error, :rate_limited, ms} -> schedule_retry(ms)
end
Returns a specification to start this module under a supervisor.
See Supervisor.
Reset rate limit state for a plot.
Restores tokens to full burst capacity.
Start the rate limiter process.
Get rate limiter stats.
@spec tokens( String.t(), keyword() ) :: non_neg_integer() | nil
Get current token count for a plot.
Returns nil if plot has no rate limit state.