Page MenuHomePhorge

No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/retry.ex b/lib/tesla/middleware/retry.ex
index 23d974e..eede27f 100644
--- a/lib/tesla/middleware/retry.ex
+++ b/lib/tesla/middleware/retry.ex
@@ -1,48 +1,47 @@
defmodule Tesla.Middleware.Retry do
@behaviour Tesla.Middleware
@moduledoc """
Retry few times in case of connection refused error.
### Example
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Retry, delay: 500, max_retries: 10
end
```
### Options
- `:delay` - number of milliseconds to wait before retrying (defaults to 1000)
- `:max_retries` - maximum number of retries (defaults to 5)
"""
@defaults [
delay: 1000,
max_retries: 5
]
def call(env, next, opts) do
opts = opts || []
delay = Keyword.get(opts, :delay, @defaults[:delay])
max_retries = Keyword.get(opts, :max_retries, @defaults[:max_retries])
retry(env, next, delay, max_retries)
end
defp retry(env, next, _delay, retries) when retries <= 1 do
Tesla.run(env, next)
end
defp retry(env, next, delay, retries) do
- Tesla.run(env, next)
- rescue
- Tesla.Error ->
- :timer.sleep(delay)
- retry(env, next, delay, retries - 1)
-
- error ->
- reraise error, System.stacktrace()
+ case Tesla.run(env, next) do
+ {:ok, env} ->
+ {:ok, env}
+ {:error, reason} ->
+ :timer.sleep(delay)
+ retry(env, next, delay, retries - 1)
+ end
end
end
diff --git a/test/tesla/middleware/retry_test.exs b/test/tesla/middleware/retry_test.exs
index 5f0693c..4b95053 100644
--- a/test/tesla/middleware/retry_test.exs
+++ b/test/tesla/middleware/retry_test.exs
@@ -1,65 +1,58 @@
defmodule Tesla.Middleware.RetryTest do
use ExUnit.Case, async: false
defmodule LaggyAdapter do
def start_link, do: Agent.start_link(fn -> 0 end, name: __MODULE__)
def call(env, _opts) do
Agent.get_and_update(__MODULE__, fn retries ->
response =
case env.url do
- "/ok" -> env
+ "/ok" -> {:ok, env}
"/maybe" when retries < 5 -> {:error, :econnrefused}
- "/maybe" -> env
+ "/maybe" -> {:ok, env}
"/nope" -> {:error, :econnrefused}
end
{response, retries + 1}
end)
- |> case do
- {:error, :econnrefused} ->
- raise %Tesla.Error{message: "adapter error: :econnrefused}", reason: :econnrefused}
-
- env ->
- env
- end
end
end
defmodule Client do
use Tesla
plug Tesla.Middleware.Retry, delay: 10, max_retries: 10
adapter LaggyAdapter
end
setup do
{:ok, _} = LaggyAdapter.start_link()
:ok
end
test "pass on successful request" do
- assert %Tesla.Env{url: "/ok", method: :get} = Client.get("/ok")
+ assert {:ok, %Tesla.Env{url: "/ok", method: :get}} = Client.get("/ok")
end
test "finally pass on laggy request" do
- assert %Tesla.Env{url: "/maybe", method: :get} = Client.get("/maybe")
+ assert {:ok, %Tesla.Env{url: "/maybe", method: :get}} = Client.get("/maybe")
end
test "raise if max_retries is exceeded" do
- assert_raise Tesla.Error, fn -> Client.get("/nope") end
+ assert {:error, :econnrefused} = Client.get("/nope")
end
defmodule DefunctClient do
use Tesla
plug Tesla.Middleware.Retry
adapter fn _ -> raise "runtime-error" end
end
test "raise in case or unexpected error" do
assert_raise RuntimeError, fn -> DefunctClient.get("/blow") end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 26, 12:23 AM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40133
Default Alt Text
(3 KB)

Event Timeline