https://github.com/bschaeffer/timeout
A module for configurable timeouts in Elixir.
https://github.com/bschaeffer/timeout
elixir timeouts
Last synced: 4 months ago
JSON representation
A module for configurable timeouts in Elixir.
- Host: GitHub
- URL: https://github.com/bschaeffer/timeout
- Owner: bschaeffer
- License: mit
- Created: 2017-09-25T17:55:45.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-02T15:00:16.000Z (over 8 years ago)
- Last Synced: 2025-10-21T14:50:52.765Z (8 months ago)
- Topics: elixir, timeouts
- Language: Elixir
- Homepage: https://hexdocs.pm/timeout
- Size: 12.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Timeout
[](https://circleci.com/gh/bschaeffer/timeout)
[](https://hex.pm/packages/timeout)
`Timeout` is an api for managing and manipulating configurable timeouts. It was
mainly built as a library to configure a timeout once, then start scheduling
messages based on the configuration. It's features include:
* API for retrieving and iterating timeouts.
* Timeout backoff with optional max.
* Randomizing within a given percent of a desired range.
* Timer management utilizing the above configuration.
Read the docs at https://hexdocs.pm/timeout.
## Example Usage
A simple example using a `GenServer` process polling a remote service for work:
```elixir
defmodule MyPoller do
use GenServer
require Logger
def start_link(backend) do
GenServer.start_link(__MODULE__, [backend])
end
def init(backend) do
timeout =
Timeout.new(50, backoff: 1.25, backoff_max: 1_250, random: 0.1)
|> Timeout.send_after(self(), :poll)
{:ok, %{backend, timeout}}
end
def handle_info(:poll, {backend, timeout}) do
case backend.poll() do
{:ok, job} ->
# Process job. Reset timeout to poll at the initial interval.
timeout = Timeout.reset() |> Timeout.send_after!(:poll)
{:noreply, {backend, timeout}}
:empty ->
# No work to do. Send after automatically increases the backoff
{timeout, delay} = timeout |> Timeout.send_after(:poll)
Logger.debug("No work. Retrying in #{delay}ms")
{:noreply, {backend, timeout}}
end
end
def handle_info({:new_job, job}, {backend, timeout}) do
Timeout.cancel_timer!(timeout)
# Process job
timeout = timeout |> Timeout.reset() |> Timeout.send_after!(:poll)
{:noreply, {backend, timeout}}
end
end
```
See the docs for more information: https://hexdocs.pm/timeout.
## Installation
Add `timeout` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:timeout, "~> 0.2.0"}
]
end
```
[thp]: https://en.wikipedia.org/wiki/Thundering_herd_problem