Skip to content

Commit

Permalink
Simplify wpool, add rdbms config defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Feb 15, 2022
1 parent af5d85e commit 0d8863f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ wpool_items() ->

wpool_defaults(<<"cassandra">>) ->
maps:merge(wpool_defaults(), #{<<"workers">> => 20});
wpool_defaults(<<"rdbms">>) ->
maps:merge(wpool_defaults(), #{<<"call_timeout">> => 60000});
wpool_defaults(_) ->
wpool_defaults().

Expand Down
30 changes: 7 additions & 23 deletions src/wpool/mongoose_wpool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
-author("[email protected]").
-include("mongoose.hrl").

-type call_timeout() :: pos_integer() | undefined.
-record(mongoose_wpool, {
name :: pool_name(),
atom_name :: wpool:name(),
strategy :: wpool:strategy() | undefined,
call_timeout :: call_timeout()
strategy :: wpool:strategy(),
call_timeout :: pos_integer()
}).

%% API
Expand Down Expand Up @@ -86,17 +85,15 @@
-export_type([pool_opts/0]).
-export_type([conn_opts/0]).

-type callback_fun() :: init | start | default_opts | is_supported_strategy | stop.
-type callback_fun() :: init | start | is_supported_strategy | stop.

-callback init() -> ok | {error, term()}.
-callback start(scope(), tag(), WPoolOpts :: pool_opts(), ConnOpts :: conn_opts()) ->
{ok, {pid(), proplists:proplist()}} | {ok, pid()} |
{external, pid()} | {error, Reason :: term()}.
-callback default_opts() -> conn_opts().
{ok, {pid(), proplists:proplist()}} | {ok, pid()} | {error, Reason :: term()}.
-callback is_supported_strategy(Strategy :: wpool:strategy()) -> boolean().
-callback stop(scope(), tag()) -> ok.

-optional_callbacks([default_opts/0, is_supported_strategy/1]).
-optional_callbacks([is_supported_strategy/1]).

ensure_started() ->
wpool:start(),
Expand Down Expand Up @@ -152,7 +149,7 @@ start(PoolType, HostType, Tag, PoolOpts) ->
pool_opts(), conn_opts()) -> start_result().
start(PoolType, HostType, Tag, PoolOpts, ConnOpts) ->
{Opts0, WpoolOptsIn} = proplists:split(PoolOpts, [strategy, call_timeout]),
Opts = lists:append(Opts0) ++ maps:to_list(default_opts(PoolType)),
Opts = lists:append(Opts0),
Strategy = proplists:get_value(strategy, Opts, best_worker),
CallTimeout = proplists:get_value(call_timeout, Opts, 5000),
%% If a callback doesn't explicitly blacklist a strategy, let's proceed.
Expand All @@ -165,7 +162,7 @@ start(PoolType, HostType, Tag, PoolOpts, ConnOpts) ->
end.

-spec start(pool_type(), host_type_or_global(), tag(),
pool_opts(), conn_opts(), wpool:strategy(), call_timeout()) ->
pool_opts(), conn_opts(), wpool:strategy(), pos_integer()) ->
start_result().
start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts, Strategy, CallTimeout) ->
case mongoose_wpool_mgr:start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts) of
Expand All @@ -175,11 +172,6 @@ start(PoolType, HostType, Tag, WpoolOptsIn, ConnOpts, Strategy, CallTimeout) ->
strategy = Strategy,
call_timeout = CallTimeout}),
{ok, Pid};
{external, Pid} ->
ets:insert(?MODULE, #mongoose_wpool{name = {PoolType, HostType, Tag},
atom_name = make_pool_name(PoolType, HostType, Tag)
}),
{ok, Pid};
Error ->
Error
end.
Expand Down Expand Up @@ -346,14 +338,6 @@ make_callback_module_name(PoolType) ->
Name = "mongoose_wpool_" ++ atom_to_list(PoolType),
list_to_atom(Name).

-spec default_opts(pool_type()) -> conn_opts().
default_opts(PoolType) ->
Mod = make_callback_module_name(PoolType),
case erlang:function_exported(Mod, default_opts, 0) of
true -> Mod:default_opts();
false -> #{}
end.

-spec expand_pools([pool_map_in()], [mongooseim:host_type()]) -> [pool_map()].
expand_pools(Pools, HostTypes) ->
%% First we select only pools for a specific vhost
Expand Down
7 changes: 1 addition & 6 deletions src/wpool/mongoose_wpool_elastic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ start(HostType, Tag, WpoolOptsIn, #{host := ElasticHost, port := Port}) ->
{overrun_handler, {error_logger, warning_report}},
{worker, {tirerl_worker, Opts}}
| WpoolOptsIn],
case mongoose_wpool:start_sup_pool(elastic, ProcName, WPoolOptions) of
{ok, Pid} ->
{external, Pid};
Other ->
Other
end.
mongoose_wpool:start_sup_pool(elastic, ProcName, WPoolOptions).

stop(_, _) ->
ok.
Expand Down
4 changes: 0 additions & 4 deletions src/wpool/mongoose_wpool_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

-export([init/0]).
-export([start/4]).
-export([default_opts/0]).
-export([stop/2]).

%% --------------------------------------------------------------
Expand All @@ -28,9 +27,6 @@ start(HostType, Tag, WpoolOpts, RdbmsOpts) ->
Err -> {error, Err}
end.

default_opts() ->
#{call_timeout => 60000}.

stop(_, _) ->
ok.

Expand Down
4 changes: 4 additions & 0 deletions test/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ default_pool_wpool_opts(cassandra) ->
#{workers => 20,
strategy => best_worker,
call_timeout => 5000};
default_pool_wpool_opts(rdbms) ->
#{workers => 10,
strategy => best_worker,
call_timeout => 60000};
default_pool_wpool_opts(_) ->
#{workers => 10,
strategy => best_worker,
Expand Down

0 comments on commit 0d8863f

Please sign in to comment.