Skip to content

Commit 5b446e3

Browse files
committed
Merge branch 'use-custom-hackney-pool'
2 parents 1efe7f0 + b6429fc commit 5b446e3

4 files changed

Lines changed: 39 additions & 15 deletions

File tree

lib/twirp/client.ex

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,37 @@ defmodule Twirp.Client do
3838
end)
3939

4040
quote do
41+
def start(adapter_opts \\ []) do
42+
default_opts = [
43+
timeout: 150_000,
44+
max_connections: 60
45+
]
46+
47+
opts = Keyword.merge(default_opts, Keyword.take(adapter_opts, [:timeout, :max_connections]))
48+
# |> IO.inspect(label: "Pool options")
49+
name = Keyword.get(adapter_opts, :pool_name, __MODULE__.Pool)
50+
51+
:hackney_pool.start_pool(name, opts)
52+
end
53+
4154
# TODO - This should also allow you to configure the adapter
4255
# Maybe we should allow people to pass in the client or whatever as well.
43-
def client(content_type \\ :proto, base_url, middleware) when is_binary(base_url) do
56+
def client(content_type \\ :proto, base_url, middleware, adapter_opts) when is_binary(base_url) do
4457
base_middleware = [
4558
{Tesla.Middleware.BaseUrl, base_url},
4659
{Tesla.Middleware.Headers, [{"Content-Type", Twirp.Encoder.type(content_type)}]},
4760
]
4861

49-
Tesla.client(middleware ++ base_middleware, Tesla.Adapter.Hackney)
62+
default_adapter_opts = [
63+
recv_timeout: 1_000,
64+
pool: __MODULE__.Pool,
65+
connect_timeout: 500
66+
]
67+
68+
adapter_opts = Keyword.merge(default_adapter_opts, adapter_opts)
69+
# |> IO.inspect(label: "Adapter options")
70+
71+
Tesla.client(middleware ++ base_middleware, {Tesla.Adapter.Hackney, adapter_opts})
5072
end
5173

5274
def rpc(client, method, req, opts \\ []) do

lib/twirp/client/http.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ defmodule Twirp.Client.HTTP do
1616
encoded_payload = Encoder.encode(req, rpcdef.input, content_type)
1717

1818
case Tesla.post(client, path, encoded_payload, [opts: opts]) do
19-
{:error, :timeout} ->
20-
meta = %{
21-
timeout: Integer.to_string(get_in(opts, [:adapter, :recv_timeout]))
22-
}
19+
{:error, error} when error in [:timeout, :connect_timeout, :checkout_timeout] ->
20+
meta = %{error_type: Atom.to_string(error)}
2321
{:error, Error.deadline_exceeded("Deadline to receive data from the service was exceeded", meta)}
2422

2523
{:error, :econnrefused} ->

test/twirp/client_test.exs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Twirp.ClientTest do
1212
setup do
1313
service = Bypass.open()
1414
base_url = "http://localhost:#{service.port}"
15-
client = Client.client(base_url, [])
15+
client = Client.client(base_url, [], [])
1616

1717
{:ok, service: service, client: client}
1818
end
@@ -53,15 +53,15 @@ defmodule Twirp.ClientTest do
5353
|> Plug.Conn.resp(200, ~s|{"msg": "Test"}|)
5454
end)
5555

56-
client = Client.client(:json, "http://localhost:#{service.port}", [])
56+
client = Client.client(:json, "http://localhost:#{service.port}", [], [])
5757

5858
assert {:ok, resp} = Client.echo(client, Req.new(msg: "Test"))
5959
assert match?(%Resp{}, resp)
6060
assert resp.msg == "Test"
6161
end
6262

6363
test "if rpc is not defined return an error" do
64-
client = Client.client("", [])
64+
client = Client.client("", [], [])
6565
{:error, resp} = Client.rpc(client, :Undefined, Req.new(msg: "test"))
6666
assert match?(%Twirp.Error{code: :bad_route}, resp)
6767
end
@@ -150,4 +150,12 @@ defmodule Twirp.ClientTest do
150150
assert {:error, resp} = Client.echo(client, Req.new(msg: "test"))
151151
assert resp.code == :unavailable
152152
end
153+
154+
describe "pools" do
155+
test "start/1 starts a dedicated hackney pool" do
156+
assert :ok == Client.start()
157+
assert pid = :hackney_pool.find_pool(Client.Pool)
158+
assert is_pid(pid)
159+
end
160+
end
153161
end

test/twirp_test.exs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule TwirpTest do
3838
end
3939

4040
setup do
41-
client = Client.client("http://localhost:4002", [])
41+
client = Client.client("http://localhost:4002", [], [])
4242

4343
{:ok, client: client}
4444
end
@@ -55,10 +55,6 @@ defmodule TwirpTest do
5555

5656
assert {:error, resp} = Client.slow_echo(client, req, timeout: 5)
5757
assert resp.code == :deadline_exceeded
58-
assert resp.meta.timeout == "5"
58+
assert resp.meta.error_type == "timeout"
5959
end
60-
61-
# test "test what happens if the client hits a service endpoint that doesn't exist" do
62-
# flunk "Not Implemented"
63-
# end
6460
end

0 commit comments

Comments
 (0)