Phoenix.SessionProcess.ActionRateLimiter (phoenix_session_process v1.0.1)

Copy Markdown View Source

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

cancel_all_debounce_timers(state)

@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

parse_duration(duration)

@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(module, action, state)

@spec record_throttle(module(), term(), map()) :: map()

Record throttle execution timestamp.

Should be called after an action is executed to record when it happened.

Parameters

  • module - The reducer module
  • action - The action that was executed
  • state - The session process state

Returns

  • Updated state with recorded timestamp

schedule_debounce(module, action, session_pid, state)

@spec schedule_debounce(module(), term(), pid(), map()) :: map()

Schedule debounced action execution.

Cancels any existing timer for this action and schedules a new one.

Parameters

  • module - The reducer module
  • action - The action to schedule
  • session_pid - The session process PID
  • state - The session process state

Returns

  • Updated state with new debounce timer reference

should_debounce?(module, action)

@spec should_debounce?(module(), term()) :: boolean()

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 @debounce attributes
  • action - The action to check

Returns

  • true if action should be debounced (delayed)
  • false if action should execute immediately

should_throttle?(module, action, state)

@spec should_throttle?(module(), term(), map()) :: boolean()

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 @throttle attributes
  • action - The action to check
  • state - The session process state containing throttle tracking

Returns

  • true if action should be blocked (throttled)
  • false if action should be allowed