Skip to content

Commit

Permalink
Merge pull request #30 from savonarola/0229-start-client-under-custom…
Browse files Browse the repository at this point in the history
…-sup

feat: add option to start client pool under an external sup
  • Loading branch information
savonarola authored Mar 5, 2024
2 parents e144c7a + 4c792ac commit 0bf1f88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/client/grpc_client_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
-behaviour(supervisor).

-export([ create_channel_pool/3
, spec/3
, stop_channel_pool/1
]).

Expand All @@ -29,16 +30,26 @@

-define(APP_SUP, grpc_sup).

-type name() :: term().

-type options() :: grpc_client:client_opts()
| #{pool_size => non_neg_integer()}.

%%--------------------------------------------------------------------
%% APIs
%%--------------------------------------------------------------------

-spec create_channel_pool(term(), uri_string:uri_string(), options())
-spec create_channel_pool(name(), uri_string:uri_string(), options())
-> supervisor:startchild_ret().
create_channel_pool(Name, URL, Opts) ->
case spec(Name, URL, Opts) of
{ok, Spec} -> supervisor:start_child(?APP_SUP, Spec);
{error, Reason} -> {error, Reason}
end.

-spec spec(name(), uri_string:uri_string(), options())
-> {ok, supervisor:child_spec()} | {error, term()}.
spec(Name, URL, Opts) ->
case uri_string:parse(URL) of
#{scheme := Scheme, host := Host, port := Port} ->
Server = {Scheme, Host, Port},
Expand All @@ -48,11 +59,11 @@ create_channel_pool(Name, URL, Opts) ->
shutdown => infinity,
type => supervisor,
modules => [?MODULE]},
supervisor:start_child(?APP_SUP, Spec);
{ok, Spec};
{error, Reason, _} -> {error, Reason}
end.

-spec stop_channel_pool(term()) -> ok | {error, term()}.
-spec stop_channel_pool(name()) -> ok | {error, term()}.
stop_channel_pool(Name) ->
case supervisor:terminate_child(?APP_SUP, Name) of
ok ->
Expand Down
18 changes: 15 additions & 3 deletions src/grpc.appup.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
%% -*- mode: erlang -*-
{"0.6.13",
[{"0.6.12", [{load_module, grpc_client, brutal_purge, soft_purge, []}]},
{"0.6.14",
[
{"0.6.13",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []}]},
{"0.6.12",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []}]},
{"0.6.11",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []},
Expand Down Expand Up @@ -44,7 +50,13 @@
{load_module, grpc_client_sup, brutal_purge, soft_purge, []},
{load_module, grpc, brutal_purge, soft_purge, []}]},
{<<".*">>, []}],
[{"0.6.12", [{load_module, grpc_client, brutal_purge, soft_purge, []}]},
[
{"0.6.13",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []}]},
{"0.6.12",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []}]},
{"0.6.11",
[{load_module, grpc_client, brutal_purge, soft_purge, []},
{load_module, grpc_client_sup, brutal_purge, soft_purge, []},
Expand Down

0 comments on commit 0bf1f88

Please sign in to comment.