Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F114232
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/middleware/fuse.ex b/lib/tesla/middleware/fuse.ex
index 7f053f7..4128d14 100644
--- a/lib/tesla/middleware/fuse.ex
+++ b/lib/tesla/middleware/fuse.ex
@@ -1,61 +1,61 @@
if Code.ensure_loaded?(:fuse) do
defmodule Tesla.Middleware.Fuse do
@behaviour Tesla.Middleware
@moduledoc """
Circuit Breaker middleware using [fuse](https://github.com/jlouis/fuse)
Remember to add `{:fuse, "~> 2.4"}` to dependencies (and `:fuse` to applications in `mix.exs`)
Also, you need to recompile tesla after adding `:fuse` dependency:
```
mix deps.clean tesla
mix deps.compile tesla
```
### Example usage
```
defmodule MyClient do
use Tesla
plug Tesla.Middleware.Fuse, opts: {{:standard, 2, 10_000}, {:reset, 60_000}}
end
```
### Options
- `:name` - fuse name (defaults to module name)
- `:opts` - fuse options (see fuse docs for reference)
"""
# options borrowed from http://blog.rokkincat.com/circuit-breakers-in-elixir/
# most probably not valid for your use case
@defaults {{:standard, 2, 10_000}, {:reset, 60_000}}
def call(env, next, opts) do
opts = opts || []
name = Keyword.get(opts, :name, env.__module__)
case :fuse.ask(name, :sync) do
:ok ->
run(env, next, name)
:blown ->
{:error, :unavailable}
{:error, :not_found} ->
:fuse.install(name, Keyword.get(opts, :opts, @defaults))
run(env, next, name)
end
end
defp run(env, next, name) do
- try do
- Tesla.run(env, next)
- rescue
- _error ->
+ case Tesla.run(env, next) do
+ {:ok, env} ->
+ {:ok, env}
+ {:error, reason} ->
:fuse.melt(name)
{:error, :unavailable}
end
end
end
end
diff --git a/test/tesla/middleware/fuse_test.exs b/test/tesla/middleware/fuse_test.exs
index 54dc45d..4816e4e 100644
--- a/test/tesla/middleware/fuse_test.exs
+++ b/test/tesla/middleware/fuse_test.exs
@@ -1,50 +1,50 @@
defmodule Tesla.Middleware.FuseTest do
use ExUnit.Case, async: false
defmodule Client do
use Tesla
plug Tesla.Middleware.Fuse
plug :report
def report(env, next) do
send(self(), :request_made)
Tesla.run(env, next)
end
adapter fn env ->
case env.url do
"/ok" ->
- env
+ {:ok, env}
"/unavailable" ->
- raise %Tesla.Error{message: "adapter error: :econnrefused}", reason: :econnrefused}
+ {:error, :econnrefused}
end
end
end
setup do
Application.ensure_all_started(:fuse)
:fuse.reset(Client)
:ok
end
test "regular endpoint" do
- assert %Tesla.Env{url: "/ok"} = Client.get("/ok")
+ assert {:ok, %Tesla.Env{url: "/ok"}} = Client.get("/ok")
end
test "unavailable endpoint" do
assert {:error, :unavailable} = Client.get("/unavailable")
assert_receive :request_made
assert {:error, :unavailable} = Client.get("/unavailable")
assert_receive :request_made
assert {:error, :unavailable} = Client.get("/unavailable")
assert_receive :request_made
assert {:error, :unavailable} = Client.get("/unavailable")
refute_receive :request_made
assert {:error, :unavailable} = Client.get("/unavailable")
refute_receive :request_made
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 26, 12:15 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40123
Default Alt Text
(3 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment