Boing.ReqPlugin (boing v0.1.0)

View Source

Req plugin that provides debouncing of GET requests.

Usage

  1. Configure Req to use the plugin:
    req = Req.new() |> Boing.ReqPlugin.attach()
  2. Concurrent requests to the same URL are debounced:
    for _ <- 1..3, do: spawn(fn -> dbg(Req.get(client, url: "http://example.com")) end)
    #=> [#PID<0.208.0>, #PID<0.209.0>, #PID<0.210.0>]
    Req.get(client, url: "http://example.com") #=> {:error, %Boing.ReqPlugin.DebouncedException{url: "http://example.com"}}
    Req.get(client, url: "http://example.com") #=> {:error, %Boing.ReqPlugin.DebouncedException{url: "http://example.com"}}
    Req.get(client, url: "http://example.com") #=> {:ok, %Req.Response{body: ...}}

Custom timeout

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

client = Req.new() |> Boing.ReqPlugin.attach(debounce_timeout: 500)
for _ <- 1..2, do: Process.sleep(600) && spawn(fn -> dbg(Req.get(client, url: "http://example.com")) end)
#=> {:ok, %Req.Response{}}
#=> {:ok, %Req.Response{}}

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

Summary

Functions

Attach the plugin to the Req client.

Functions

attach(request, options \\ [])

Attach the plugin to the Req client.

req = Req.new() |> Boing.ReqPlugin.attach()