Boing.TeslaMiddleware (boing v0.1.0)

View Source

Middleware for Tesla that provides debouncing of GET requests.

Usage

  1. Configure Tesla to use the debouncer middleware:
    client = Tesla.client([Boing.TeslaMiddleware])
  2. Concurrent requests to the same URL are debounced:
    for _ <- 1..3, do: spawn(fn -> dbg(Tesla.get(client, "http://example.com")) end)
    #=> [#PID<0.208.0>, #PID<0.209.0>, #PID<0.210.0>]
    Tesla.get(client, "http://example.com") #=> {:error, [debounced: "http://example.com"]}
    Tesla.get(client, "http://example.com") #=> {:error, [debounced: "http://example.com"]}
    Tesla.get(client, "http://example.com") #=> {:ok, %Tesla.Env{body: ...}}

Custom timeout

The :debounce_timeout option can be supplied to customize the timeout, which defaults to 1000ms.

client = Tesla.client([{Boing.TeslaMiddleware, debounce_timeout: 500}])
for _ <- 1..2, do: Process.sleep(600) && spawn(fn -> dbg(Tesla.get!(client, "http://example.com")) end)
#=> {:ok, %Tesla.Env{...}}
#=> {:ok, %Tesla.Env{...}}

In this example, both requests return {:ok, %Tesla.Env{...}} and are not debounced, due to the shorter timeout.