Collapses concurrent duplicate calls into one supervised execution per key.
one_flight is not a cache. It only shares work that is already in progress;
after a flight completes, the result is discarded.
Example
iex> {:ok, _pid} = start_supervised({OneFlight, name: ExampleFlights})
iex> OneFlight.run(:key, fn -> :value end, name: ExampleFlights)
{:ok, :value}
Summary
Functions
Starts a flight and returns a handle that can be awaited.
Waits for an async flight result.
Detaches key so the next caller starts a fresh flight.
Runs fun once for concurrent callers using the same key.
Runs fun once and unwraps a successful result.
Returns local in-flight stats for key.
Types
@type key() :: term()
@type option() :: {:name, GenServer.server()} | {:timeout, timeout()} | {:on_abandoned, :cancel | :complete} | {:scope, :node}
Functions
@spec async(key(), (-> term()), [option()]) :: OneFlight.Flight.t()
Starts a flight and returns a handle that can be awaited.
@spec await(OneFlight.Flight.t(), timeout()) :: {:ok, term()} | {:error, OneFlight.Error.t()}
Waits for an async flight result.
Detaches key so the next caller starts a fresh flight.
@spec run(key(), (-> term()), [option()]) :: {:ok, term()} | {:error, OneFlight.Error.t()}
Runs fun once for concurrent callers using the same key.
Runs fun once and unwraps a successful result.
Raises OneFlight.Error when the non-bang API would return an error tuple.
@spec stats(key(), [option()]) :: %{ in_flight?: boolean(), waiters_count: non_neg_integer() }
Returns local in-flight stats for key.