From 41cd4cabd2763e22df6bf40a1a5eb7999ef5b679 Mon Sep 17 00:00:00 2001 From: Gustaw Lippa Date: Tue, 15 Feb 2022 12:54:39 +0100 Subject: [PATCH] Simplify wpool, add rdbms config defaults --- src/config/mongoose_config_spec.erl | 2 ++ src/wpool/mongoose_wpool.erl | 30 +++++++--------------------- src/wpool/mongoose_wpool_elastic.erl | 7 +------ src/wpool/mongoose_wpool_rdbms.erl | 4 ---- test/config_parser_helper.erl | 4 ++++ 5 files changed, 14 insertions(+), 33 deletions(-) diff --git a/src/config/mongoose_config_spec.erl b/src/config/mongoose_config_spec.erl index 4e7a38d412..808e6585d3 100644 --- a/src/config/mongoose_config_spec.erl +++ b/src/config/mongoose_config_spec.erl @@ -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(). diff --git a/src/wpool/mongoose_wpool.erl b/src/wpool/mongoose_wpool.erl index b23f2c1681..7424584494 100644 --- a/src/wpool/mongoose_wpool.erl +++ b/src/wpool/mongoose_wpool.erl @@ -9,12 +9,11 @@ -author("bartlomiej.gorny@erlang-solutions.com"). -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 @@ -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(), @@ -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. @@ -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 @@ -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. @@ -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 diff --git a/src/wpool/mongoose_wpool_elastic.erl b/src/wpool/mongoose_wpool_elastic.erl index 76622808f5..0ea3218324 100644 --- a/src/wpool/mongoose_wpool_elastic.erl +++ b/src/wpool/mongoose_wpool_elastic.erl @@ -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. diff --git a/src/wpool/mongoose_wpool_rdbms.erl b/src/wpool/mongoose_wpool_rdbms.erl index f7a9baca40..9811ff66f0 100644 --- a/src/wpool/mongoose_wpool_rdbms.erl +++ b/src/wpool/mongoose_wpool_rdbms.erl @@ -3,7 +3,6 @@ -export([init/0]). -export([start/4]). --export([default_opts/0]). -export([stop/2]). %% -------------------------------------------------------------- @@ -28,9 +27,6 @@ start(HostType, Tag, WpoolOpts, RdbmsOpts) -> Err -> {error, Err} end. -default_opts() -> - #{call_timeout => 60000}. - stop(_, _) -> ok. diff --git a/test/config_parser_helper.erl b/test/config_parser_helper.erl index ed65f7c442..1443ba1c52 100644 --- a/test/config_parser_helper.erl +++ b/test/config_parser_helper.erl @@ -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,