Rate limiting for Redux actions via throttle and debounce.
Throttle
Execute action immediately, then block subsequent calls for the specified duration. Useful for limiting API calls, preventing spam clicks, etc.
Debounce
Delay action execution until the specified duration has passed since the last call. Useful for search inputs, auto-save, etc.
Duration Format
Durations can be specified as strings:
"500ms"- 500 milliseconds"1s"- 1 second"5m"- 5 minutes"1h"- 1 hour
Summary
Functions
Cancel all debounce timers for cleanup.
Parse time string like "3000ms", "1s", "5m", "1h" to milliseconds.
Record throttle execution timestamp.
Schedule debounced action execution.
Check if action should be debounced based on module's debounce configuration.
Check if action should be throttled based on module's throttle configuration.
Functions
@spec cancel_all_debounce_timers(map()) :: :ok
Cancel all debounce timers for cleanup.
Should be called when session process terminates.
Parameters
state- The session process state
Returns
:ok
@spec parse_duration(String.t()) :: non_neg_integer()
Parse time string like "3000ms", "1s", "5m", "1h" to milliseconds.
Examples
iex> ActionRateLimiter.parse_duration("500ms")
500
iex> ActionRateLimiter.parse_duration("1s")
1000
iex> ActionRateLimiter.parse_duration("5m")
300_000
iex> ActionRateLimiter.parse_duration("1h")
3_600_000
Record throttle execution timestamp.
Should be called after an action is executed to record when it happened.
Parameters
module- The reducer moduleaction- The action that was executedstate- The session process state
Returns
- Updated state with recorded timestamp
Schedule debounced action execution.
Cancels any existing timer for this action and schedules a new one.
Parameters
module- The reducer moduleaction- The action to schedulesession_pid- The session process PIDstate- The session process state
Returns
- Updated state with new debounce timer reference
Check if action should be debounced based on module's debounce configuration.
Debounce: Delay execution, reset timer on new call.
Parameters
module- The reducer module with@debounceattributesaction- The action to check
Returns
trueif action should be debounced (delayed)falseif action should execute immediately
Check if action should be throttled based on module's throttle configuration.
Throttle: Execute immediately on first call, then block for duration.
Parameters
module- The reducer module with@throttleattributesaction- The action to checkstate- The session process state containing throttle tracking
Returns
trueif action should be blocked (throttled)falseif action should be allowed