Skip to content

Commit 3b72fbb

Browse files
committed
The grpcbox_client get_channel function adds support for hash and direct strategies
1 parent 988b5a9 commit 3b72fbb

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,
@@ -58,11 +59,19 @@ is_ready(Name) ->
5859
gen_statem:call(?CHANNEL(Name), is_ready).
5960

6061
%% @doc Picks a subchannel from a pool using the configured strategy.
61-
-spec pick(name(), unary | stream) -> {ok, {pid(), grpcbox_client:interceptor() | undefined}} |
62-
{error, undefined_channel | no_endpoints}.
62+
-spec pick(name(), unary | stream) ->
63+
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
64+
{error, undefined_channel | no_endpoints}.
6365
pick(Name, CallType) ->
66+
pick(Name, CallType, undefined).
67+
68+
%% @doc Picks a subchannel from a pool using the configured strategy.
69+
-spec pick(name(), unary | stream, term() | undefined) ->
70+
{ok, {pid(), grpcbox_client:interceptor() | undefined}} |
71+
{error, undefined_channel | no_endpoints}.
72+
pick(Name, CallType, Key) ->
6473
try
65-
case gproc_pool:pick_worker(Name) of
74+
case pick_worker(Name, Key) of
6675
false -> {error, no_endpoints};
6776
Pid when is_pid(Pid) ->
6877
{ok, {Pid, interceptor(Name, CallType)}}
@@ -72,6 +81,11 @@ pick(Name, CallType) ->
7281
{error, undefined_channel}
7382
end.
7483

84+
pick_worker(Name, undefined) ->
85+
gproc_pool:pick_worker(Name);
86+
pick_worker(Name, Key) ->
87+
gproc_pool:pick_worker(Name, Key).
88+
7589
-spec interceptor(name(), unary | stream) -> grpcbox_client:interceptor() | undefined.
7690
interceptor(Name, CallType) ->
7791
case ets:lookup(?CHANNELS_TAB, {Name, CallType}) of
@@ -177,4 +191,3 @@ start_workers(Pool, StatsHandler, Encoding, Endpoints) ->
177191
Encoding, StatsHandler),
178192
Pid
179193
end || Endpoint={Transport, Host, Port, EndpointOptions} <- Endpoints].
180-

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)