Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/conn_grpc/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ defmodule ConnGRPC.Pool do
channel: [address: "localhost:50051", opts: []]
end

You can also use the `:otp_app` option to load configuration from your application's config:

defmodule DemoPool do
use ConnGRPC.Pool, otp_app: :my_app
end

This will load configuration from `Application.get_env(:my_app, DemoPool, [])` and merge it
with the options passed to `child_spec/1`.

The format of `address` and `opts` is the same used by
[`GRPC.Stub.connect/2`](https://hexdocs.pm/grpc/0.5.0/GRPC.Stub.html#connect/2)

Expand Down Expand Up @@ -99,8 +108,17 @@ defmodule ConnGRPC.Pool do
def get_all_pids, do: ConnGRPC.Pool.get_all_pids(__MODULE__)

def child_spec(opts) do
{otp_app, use_opts} = Keyword.pop(unquote(use_opts), :otp_app)

app_env_opts =
case otp_app do
nil -> []
app -> Application.get_env(app, __MODULE__, [])
end

[name: __MODULE__]
|> Keyword.merge(unquote(use_opts))
|> Keyword.merge(use_opts)
|> Keyword.merge(app_env_opts)
|> Keyword.merge(opts)
|> ConnGRPC.Pool.child_spec()
|> Supervisor.child_spec(id: __MODULE__)
Expand Down
38 changes: 38 additions & 0 deletions test/conn_grpc/pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,44 @@ defmodule ConnGRPC.PoolTest do
assert {:ok, %GRPC.Channel{}} = UsingTestPool.get_channel()
assert is_list(UsingTestPool.get_all_pids())
end

test "loads configuration from Application.get_env when otp_app is specified" do
defmodule UsingTestPoolWithConfig do
use ConnGRPC.Pool, otp_app: :test_app
end

Application.put_env(:test_app, UsingTestPoolWithConfig,
pool_size: 2,
channel: [address: "test_address", opts: [adapter: GRPC.Client.TestAdapters.Success]]
)

child_spec = UsingTestPoolWithConfig.child_spec([])
{_module, _function, [opts]} = child_spec.start

assert opts[:pool_size] == 2
assert opts[:channel][:address] == "test_address"
assert opts[:name] == UsingTestPoolWithConfig

Application.delete_env(:test_app, UsingTestPoolWithConfig)
end

test "merges Application.get_env config with child_spec opts" do
defmodule UsingTestPoolWithMerge do
use ConnGRPC.Pool, otp_app: :test_app
end

Application.put_env(:test_app, UsingTestPoolWithMerge,
pool_size: 2,
channel: [address: "test_address", opts: [adapter: GRPC.Client.TestAdapters.Success]]
)

child_spec = UsingTestPoolWithMerge.child_spec(pool_size: 3)
{_module, _function, [opts]} = child_spec.start

assert Keyword.get(opts, :pool_size) == 3

Application.delete_env(:test_app, UsingTestPoolWithMerge)
end
end

defp send_disconnect_msg(pid), do: send(pid, {:gun_down, fake_pid(), :http2, :normal, []})
Expand Down