Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash and direct pick strategies #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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