Skip to content

Commit

Permalink
The grpcbox_client get_channel function adds support for hash and dir…
Browse files Browse the repository at this point in the history
…ect strategies
  • Loading branch information
heshaoqiong committed Jun 5, 2023
1 parent 988b5a9 commit 3b72fbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/grpcbox_channel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-export([start_link/3,
is_ready/1,
pick/2,
pick/3,
stop/1,
stop/2]).
-export([init/1,
Expand Down Expand Up @@ -58,11 +59,19 @@ is_ready(Name) ->
gen_statem:call(?CHANNEL(Name), is_ready).

%% @doc Picks a subchannel from a pool using the configured strategy.
-spec pick(name(), unary | stream) -> {ok, {pid(), grpcbox_client:interceptor() | undefined}} |
{error, undefined_channel | no_endpoints}.
-spec pick(name(), unary | stream) ->
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
{error, undefined_channel | no_endpoints}.
pick(Name, CallType) ->
pick(Name, CallType, undefined).

%% @doc Picks a subchannel from a pool using the configured strategy.
-spec pick(name(), unary | stream, term() | undefined) ->
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
{error, undefined_channel | no_endpoints}.
pick(Name, CallType, Key) ->
try
case gproc_pool:pick_worker(Name) of
case pick_worker(Name, Key) of
false -> {error, no_endpoints};
Pid when is_pid(Pid) ->
{ok, {Pid, interceptor(Name, CallType)}}
Expand All @@ -72,6 +81,11 @@ pick(Name, CallType) ->
{error, undefined_channel}
end.

pick_worker(Name, undefined) ->
gproc_pool:pick_worker(Name);
pick_worker(Name, Key) ->
gproc_pool:pick_worker(Name, Key).

-spec interceptor(name(), unary | stream) -> grpcbox_client:interceptor() | undefined.
interceptor(Name, CallType) ->
case ets:lookup(?CHANNELS_TAB, {Name, CallType}) of
Expand Down Expand Up @@ -177,4 +191,3 @@ start_workers(Pool, StatsHandler, Encoding, Endpoints) ->
Encoding, StatsHandler),
Pid
end || Endpoint={Transport, Host, Port, EndpointOptions} <- Endpoints].

3 changes: 2 additions & 1 deletion src/grpcbox_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

get_channel(Options, Type) ->
Channel = maps:get(channel, Options, default_channel),
grpcbox_channel:pick(Channel, Type).
Key = maps:get(key, Options, undefined),
grpcbox_channel:pick(Channel, Type, Key).

unary(Ctx, Service, Method, Input, Def, Options) ->
unary(Ctx, filename:join([<<>>, Service, Method]), Input, Def, Options).
Expand Down

0 comments on commit 3b72fbb

Please sign in to comment.