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 tracking

Summary

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

Link to this function

handle_call(Request, From, State)

View Source
-spec handle_call(term(), {pid(), term()}, state()) -> {reply, term(), state()}.
Link to this function

handle_cast(Request, State)

View Source
-spec handle_cast(term(), state()) -> {noreply, state()}.
Link to this function

handle_info(Info, State)

View Source
-spec handle_info(term(), state()) -> {noreply, state()}.
-spec init([]) -> {ok, state()}.
-spec record_acceptance(speed_trap:id()) -> ok.
-spec record_rejection(speed_trap:id()) -> ok.
Link to this function

register_dynamic_limiter(Id, Options)

View Source
-spec register_dynamic_limiter(speed_trap:id(), speed_trap:options()) ->
                                  ok | {error, already_registered}.
-spec start_link() -> {ok, pid()} | {error, term()}.
Link to this function

unregister_dynamic_limiter(Id)

View Source
-spec unregister_dynamic_limiter(speed_trap:id()) -> ok.