Foundation.Dispatch (foundation v0.2.1)

Copy Markdown View Source

Layered dispatch limiter combining concurrency, throttling, and byte budgets.

Summary

Functions

Returns a specification to start this module under a supervisor.

Set a backoff window (in milliseconds) and mark the dispatch as recently throttled.

Return a snapshot of the dispatch state.

Execute fun while holding the layered dispatch semaphores.

Types

option()

@type option() ::
  {:name, GenServer.name()}
  | {:key, term()}
  | {:registry, Foundation.Semaphore.Counting.registry()}
  | {:limiter, Foundation.RateLimit.BackoffWindow.limiter()}
  | {:concurrency, pos_integer()}
  | {:throttled_concurrency, pos_integer()}
  | {:byte_budget, pos_integer()}
  | {:backoff_window_ms, pos_integer()}
  | {:byte_penalty_multiplier, pos_integer()}
  | {:acquire_backoff, Foundation.Backoff.Policy.t() | keyword()}
  | {:sleep_fun, (non_neg_integer() -> any())}

snapshot()

@type snapshot() :: %{
  concurrency: %{name: term(), limit: pos_integer()},
  throttled: %{name: term(), limit: pos_integer()},
  bytes: Foundation.Semaphore.Weighted.t(),
  backoff_active?: boolean(),
  acquire_backoff: Foundation.Backoff.Policy.t(),
  registry: Foundation.Semaphore.Counting.registry(),
  sleep_fun: (non_neg_integer() -> any()),
  byte_penalty_multiplier: pos_integer()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

set_backoff(dispatch, duration_ms)

@spec set_backoff(GenServer.server(), non_neg_integer()) :: :ok

Set a backoff window (in milliseconds) and mark the dispatch as recently throttled.

snapshot(dispatch)

@spec snapshot(GenServer.server()) :: snapshot()

Return a snapshot of the dispatch state.

start_link(opts \\ [])

@spec start_link([option()]) :: GenServer.on_start()

with_rate_limit(dispatch, estimated_bytes, fun)

@spec with_rate_limit(GenServer.server(), non_neg_integer(), (-> result)) :: result
when result: any()

Execute fun while holding the layered dispatch semaphores.