# `ExResilience.Hedge`
[🔗](https://github.com/joshrotenberg/ex_resilience/blob/v0.4.0/lib/ex_resilience/hedge.ex#L1)

Tail latency reduction by racing redundant requests.

Starts the given function in a Task, waits for `:delay` milliseconds,
and if no result has arrived, fires up to `:max_hedged` additional
Tasks running the same function. The first successful result wins.

This is useful when a backend has variable latency and you would rather
pay the cost of a redundant request than wait for a slow one.

## Options

  * `:name` -- optional atom for telemetry metadata. Default `:hedge`.
  * `:delay` -- milliseconds to wait before firing hedge requests. Required.
  * `:max_hedged` -- max additional requests to fire. Default `1`.

## Examples

    iex> ExResilience.Hedge.call(fn -> {:ok, 42} end, delay: 100)
    {:ok, 42}

# `option`

```elixir
@type option() ::
  {:name, atom()} | {:delay, non_neg_integer()} | {:max_hedged, pos_integer()}
```

# `call`

```elixir
@spec call((-&gt; term()), [option()]) :: term()
```

Executes `fun` with hedge logic for tail latency reduction.

Starts `fun` as the primary request. If it does not complete within
`:delay` milliseconds, fires up to `:max_hedged` additional copies.
Returns the first successful result, or the first error if all fail.

## Examples

    iex> ExResilience.Hedge.call(fn -> {:ok, :fast} end, delay: 50)
    {:ok, :fast}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
