|
@@ -4,7 +4,8 @@
|
|
validate_connection_params/1,
|
|
validate_connection_params/1,
|
|
query/2, query/3, query/4,
|
|
query/2, query/3, query/4,
|
|
transaction/2,
|
|
transaction/2,
|
|
- get_settings/0, set_settings/1
|
|
|
|
|
|
+ get_settings/0, set_settings/1,
|
|
|
|
+ test_run/0
|
|
]).
|
|
]).
|
|
|
|
|
|
-include("epgsql_pool.hrl").
|
|
-include("epgsql_pool.hrl").
|
|
@@ -28,17 +29,20 @@ start(PoolName, InitCount, MaxCount, ConnectionParams) when is_map(ConnectionPar
|
|
|
|
|
|
start(PoolName0, InitCount, MaxCount, #epgsql_connection_params{} = ConnectionParams) ->
|
|
start(PoolName0, InitCount, MaxCount, #epgsql_connection_params{} = ConnectionParams) ->
|
|
PoolName = epgsql_pool_utils:pool_name_to_atom(PoolName0),
|
|
PoolName = epgsql_pool_utils:pool_name_to_atom(PoolName0),
|
|
- %% TODO check PoolName not in all_keys()
|
|
|
|
- application:set_env(epgsql_pool, PoolName, ConnectionParams),
|
|
|
|
- {ok, MaxQueue} = application:get_env(epgsql_pool, pooler_max_queue),
|
|
|
|
- PoolConfig = [{name, PoolName},
|
|
|
|
- {init_count, InitCount},
|
|
|
|
- {max_count, MaxCount},
|
|
|
|
- {queue_max, MaxQueue},
|
|
|
|
- {start_mfa, {epgsql_pool_worker, start_link, [PoolName]}},
|
|
|
|
- {stop_mfa, {epgsql_pool_worker, stop, []}}
|
|
|
|
- ],
|
|
|
|
- pooler:new_pool(PoolConfig).
|
|
|
|
|
|
+ case lists:member(PoolName, all_keys()) of
|
|
|
|
+ true -> {error, invalid_pool_name};
|
|
|
|
+ false ->
|
|
|
|
+ application:set_env(epgsql_pool, PoolName, ConnectionParams),
|
|
|
|
+ {ok, MaxQueue} = application:get_env(epgsql_pool, pooler_max_queue),
|
|
|
|
+ PoolConfig = [{name, PoolName},
|
|
|
|
+ {init_count, InitCount},
|
|
|
|
+ {max_count, MaxCount},
|
|
|
|
+ {queue_max, MaxQueue},
|
|
|
|
+ {start_mfa, {epgsql_pool_worker, start_link, [PoolName]}},
|
|
|
|
+ {stop_mfa, {epgsql_pool_worker, stop, []}}
|
|
|
|
+ ],
|
|
|
|
+ pooler:new_pool(PoolConfig)
|
|
|
|
+ end.
|
|
|
|
|
|
|
|
|
|
-spec stop(pool_name()) -> ok | {error, term()}.
|
|
-spec stop(pool_name()) -> ok | {error, term()}.
|
|
@@ -151,6 +155,24 @@ set_settings(Map) ->
|
|
ok.
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
+-spec test_run() -> ok.
|
|
|
|
+test_run() ->
|
|
|
|
+ application:ensure_all_started(epgsql_pool),
|
|
|
|
+
|
|
|
|
+ Params = #{host => "localhost",
|
|
|
|
+ port => 5432,
|
|
|
|
+ username => "test",
|
|
|
|
+ password => "test",
|
|
|
|
+ database => "testdb"},
|
|
|
|
+ {ok, _} = epgsql_pool:start(my_pool, 2, 2, Params),
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Res1 = epgsql_pool:query(my_pool, "select * from category"),
|
|
|
|
+ error_logger:info_msg("Res1: ~p", [Res1]),
|
|
|
|
+
|
|
|
|
+ ok.
|
|
|
|
+
|
|
|
|
+
|
|
%%% inner functions
|
|
%%% inner functions
|
|
|
|
|
|
-spec get_worker(pool_name()) -> {ok, pid()} | {error, term()}.
|
|
-spec get_worker(pool_name()) -> {ok, pid()} | {error, term()}.
|
|
@@ -167,6 +189,8 @@ get_worker(PoolName) ->
|
|
|
|
|
|
-spec all_keys() -> [atom()].
|
|
-spec all_keys() -> [atom()].
|
|
all_keys() ->
|
|
all_keys() ->
|
|
- [connection_timeout, query_timeout,
|
|
|
|
- pooler_get_worker_timeout, pooler_max_queue,
|
|
|
|
- max_reconnect_timeout, min_reconnect_timeout, keep_alive_timeout].
|
|
|
|
|
|
+ lists:filtermap(fun({_Key, #epgsql_connection_params{}}) -> false;
|
|
|
|
+ ({included_applications, _}) -> false;
|
|
|
|
+ ({Key, _Value}) -> {true, Key}
|
|
|
|
+ end,
|
|
|
|
+ application:get_all_env(epgsql_pool)).
|