Skip to content

Commit 4a64857

Browse files
committed
The grpcbox_client get_channel function adds support for hash and direct strategies
1 parent a5aa6e9 commit 4a64857

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/grpcbox_channel.erl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
-export([start_link/3,
66
is_ready/1,
77
pick/2,
8+
pick/3,
89
stop/1,
910
stop/2]).
1011
-export([init/1,
@@ -55,11 +56,19 @@ is_ready(Name) ->
5556
gen_statem:call(?CHANNEL(Name), is_ready).
5657

5758
%% @doc Picks a subchannel from a pool using the configured strategy.
58-
-spec pick(name(), unary | stream) -> {ok, {pid(), grpcbox_client:interceptor() | undefined}} |
59-
{error, undefined_channel | no_endpoints}.
59+
-spec pick(name(), unary | stream) ->
60+
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
61+
{error, undefined_channel | no_endpoints}.
6062
pick(Name, CallType) ->
63+
pick(Name, CallType, undefined).
64+
65+
%% @doc Picks a subchannel from a pool using the configured strategy.
66+
-spec pick(name(), unary | stream, term() | undefined) ->
67+
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
68+
{error, undefined_channel | no_endpoints}.
69+
pick(Name, CallType, Key) ->
6170
try
62-
case gproc_pool:pick_worker(Name) of
71+
case pick_worker(Name, Key) of
6372
false -> {error, no_endpoints};
6473
Pid when is_pid(Pid) ->
6574
{ok, {Pid, interceptor(Name, CallType)}}
@@ -69,6 +78,11 @@ pick(Name, CallType) ->
6978
{error, undefined_channel}
7079
end.
7180

81+
pick_worker(Name, undefined) ->
82+
gproc_pool:pick_worker(Name);
83+
pick_worker(Name, Key) ->
84+
gproc_pool:pick_worker(Name, Key).
85+
7286
-spec interceptor(name(), unary | stream) -> grpcbox_client:interceptor() | undefined.
7387
interceptor(Name, CallType) ->
7488
case ets:lookup(?CHANNELS_TAB, {Name, CallType}) of
@@ -175,4 +189,3 @@ start_workers(Pool, StatsHandler, Encoding, Endpoints) ->
175189
Encoding, StatsHandler),
176190
Pid
177191
end || Endpoint={Transport, Host, Port, SSLOptions} <- Endpoints].
178-

src/grpcbox_client.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747

4848
get_channel(Options, Type) ->
4949
Channel = maps:get(channel, Options, default_channel),
50-
grpcbox_channel:pick(Channel, Type).
50+
Key = maps:get(key, Options, undefined),
51+
grpcbox_channel:pick(Channel, Type, Key).
5152

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

0 commit comments

Comments
 (0)