View Source speed_trap_dynamic (speed_trap v1.1.1)
speed_trap_dynamic - Dynamic rate limiter based on rejection rates
This module implements a dynamic rate limiter that automatically adjusts bucket_size based on rejection rates. It monitors the rejection rate over a configurable time interval and scales bucket_size up or down accordingly.
Key features: - Automatic upscaling when rejection rate exceeds threshold - Automatic downscaling when rejection rate is below threshold - Configurable min/max bucket_size, scaling time intervals, and adjustment increments - Gradual bucket_size changes to allow downstream systems to scale - Uses atomics for high-performance rejection/acceptance trackingSummary
Types
-type dynamic_state() :: #dynamic_state{id :: speed_trap:id(), counters :: atomics:atomics_ref(), timer_ref :: timer:tref() | undefined}.
-type state() :: #{speed_trap:id() => dynamic_state()}.
Functions
-spec init([]) -> {ok, state()}.
-spec record_acceptance(speed_trap:id()) -> ok.
-spec record_rejection(speed_trap:id()) -> ok.
-spec register_dynamic_limiter(speed_trap:id(), speed_trap:options()) -> ok | {error, already_registered}.
-spec unregister_dynamic_limiter(speed_trap:id()) -> ok.