Skip to content

Commit

Permalink
Add defaults for cassandra pool in config
Browse files Browse the repository at this point in the history
  • Loading branch information
gustawlippa committed Feb 15, 2022
1 parent e3266cc commit f33acaa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
14 changes: 11 additions & 3 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ outgoing_pool(Type) ->
process = fun ?MODULE:process_pool/2,
format_items = map,
wrap = item,
defaults = wpool_defaults()
defaults = wpool_defaults(Type)
}.

wpool_items() ->
Expand All @@ -556,6 +556,11 @@ wpool_items() ->
validate = positive}
}.

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

wpool_defaults() ->
#{<<"workers">> => 10,
<<"strategy">> => best_worker,
Expand All @@ -565,7 +570,7 @@ wpool_defaults() ->
outgoing_pool_connection(<<"cassandra">>) ->
#section{
items = #{<<"servers">> => #list{items = cassandra_server()},
<<"keyspace">> => #option{type = string,
<<"keyspace">> => #option{type = atom,
validate = non_empty},
<<"auth">> => #section{items = #{<<"plain">> => cassandra_auth_plain()},
required = all,
Expand All @@ -574,7 +579,10 @@ outgoing_pool_connection(<<"cassandra">>) ->
wrap = {kv, ssl},
process = fun ?MODULE:process_tls_sni/1}
},
format_items = map
format_items = map,
include = always,
defaults = #{<<"servers">> => [{"localhost", 9042}],
<<"keyspace">> => mongooseim}
};
outgoing_pool_connection(<<"elastic">>) ->
#section{
Expand Down
9 changes: 2 additions & 7 deletions src/wpool/mongoose_wpool_cassandra.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ init() ->
application:set_env(cqerl, maps, true).

start(HostType, Tag, WpoolOptsIn, CqerlOpts) ->
PoolSize = proplists:get_value(workers, WpoolOptsIn, 20),
PoolSize = proplists:get_value(workers, WpoolOptsIn),
application:set_env(cqerl, num_clients, PoolSize),
ExtConfig = extend_config(CqerlOpts),
Servers = proplists:get_value(servers, ExtConfig),
Expand All @@ -35,12 +35,7 @@ stop(_, _) ->
%% --------------------------------------------------------------
%% Internal functions
extend_config(PoolConfig) ->
Defaults = #{
servers => [{"localhost", 9042}],
tcp_opts => [{keepalive, true}],
keyspace => mongooseim
},
ConfigMap = maps:merge(Defaults, PoolConfig),
ConfigMap = maps:merge(#{tcp_opts => [{keepalive, true}]}, PoolConfig),
maps:to_list(ConfigMap).

%% make the config survive the restart of 'cqerl_cluster' in case of a network failure
Expand Down
2 changes: 1 addition & 1 deletion test/config_parser_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ pool_cassandra_servers(_Config) ->

pool_cassandra_keyspace(_Config) ->
?cfg(pool_config(#{type => cassandra, scope => global, tag => default, opts => #{},
conn_opts => #{keyspace => "big_mongooseim"}}),
conn_opts => #{keyspace => big_mongooseim}}),
pool_conn_raw(<<"cassandra">>, #{<<"keyspace">> => <<"big_mongooseim">>})),
?err(pool_conn_raw(<<"cassandra">>, #{<<"keyspace">> => <<"">>})).

Expand Down
25 changes: 16 additions & 9 deletions test/config_parser_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ options("outgoing_pools") ->
{outgoing_pools,
lists:map(fun merge_with_default_pool_config/1,
[#{type => cassandra, scope => global, tag => default, opts => #{},
conn_opts => #{keyspace => "big_mongooseim",
conn_opts => #{keyspace => big_mongooseim,
servers => [{"cassandra_server1.example.com", 9042},
{"cassandra_server2.example.com", 9042}]}},
#{type => elastic, scope => global, tag => default, opts => #{},
Expand Down Expand Up @@ -762,23 +762,33 @@ merge_with_default_pool_config(PoolIn = #{type := Type}) ->

default_pool_config(Type) ->
#{scope => global,
opts => default_pool_wpool_opts(),
opts => default_pool_wpool_opts(Type),
conn_opts => default_pool_conn_opts(Type)}.

default_pool_wpool_opts() ->
default_pool_wpool_opts(cassandra) ->
#{workers => 20,
strategy => best_worker,
call_timeout => 5000};
default_pool_wpool_opts(_) ->
#{workers => 10,
strategy => best_worker,
call_timeout => 5000}.

default_pool_conn_opts(cassandra) ->
#{servers => [{"localhost", 9042}],
keyspace => mongooseim};
default_pool_conn_opts(elastic) ->
#{host => "localhost",
port => 9200};
default_pool_conn_opts(http) ->
#{path_prefix => "/",
request_timeout => 2000};
default_pool_conn_opts(ldap) ->
#{rootdn => "",
password => "",
encrypt => none,
servers => ["localhost"],
connect_interval => 10000};
default_pool_conn_opts(http) ->
#{path_prefix => "/",
request_timeout => 2000};
default_pool_conn_opts(rabbit) ->
#{amqp_port => 5672,
confirms_enabled => false,
Expand All @@ -788,9 +798,6 @@ default_pool_conn_opts(redis) ->
port => 6379,
database => 0,
password => ""};
default_pool_conn_opts(elastic) ->
#{host => "localhost",
port => 9200};
default_pool_conn_opts(_Type) ->
#{}.

Expand Down

0 comments on commit f33acaa

Please sign in to comment.