Yuriy Zhloba 9 лет назад
Родитель
Сommit
16cb843d7e

+ 19 - 3
src/epgsql_pool.erl

@@ -3,7 +3,8 @@
 -export([start/4, stop/1,
          validate_connection_params/1,
          query/2, query/3, query/4,
-         transaction/2
+         transaction/2,
+         get_settings/0, set_settings/1
         ]).
 
 -include("epgsql_pool.hrl").
@@ -40,8 +41,6 @@ start(PoolName0, InitCount, MaxCount, #epgsql_connection_params{} = ConnectionPa
     end.
 
 
-
-
 -spec stop(pool_name()) -> ok | {error, term()}.
 stop(PoolName) ->
     pooler:rm_pool(epgsql_pool_utils:pool_name_to_atom(PoolName)).
@@ -113,6 +112,23 @@ transaction(PoolName, Fun) ->
     end.
 
 
+-spec get_settings() -> map().
+get_settings() ->
+    lists:foldl(fun(Key, Map) ->
+                        maps:put(Key, epgsql_pool_settings:get(Key), Map)
+                end, maps:new(), epgsql_pool_settings:all_keys()).
+
+
+-spec set_settings(map()) -> ok.
+set_settings(Map) ->
+    lists:foreach(fun(Key) ->
+                          case maps:find(Key, Map) of
+                              {ok, Value} -> epgsql_pool_settings:set(Key, Value);
+                              error -> do_nothing
+                          end
+                  end, epgsql_pool_settings:all_keys()),
+    ok.
+
 %%% inner functions
 
 get_worker(PoolName0) ->

+ 3 - 0
src/epgsql_pool_app.erl

@@ -20,14 +20,17 @@ stop(_State) ->
 -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, 1, 2, Params),
+
     Res1 = epgsql_pool:query(my_pool, "select * from category"),
     error_logger:info_msg("~p", [Res1]),
+
     Res2 = epgsql_pool:query(my_pool, "select * from category where id = $1", [1], [{timeout, 200}]),
     error_logger:info_msg("~p", [Res2]),
     ok.

+ 7 - 1
src/epgsql_pool_settings.erl

@@ -4,7 +4,7 @@
 -export([start_link/0,
          get_connection_params/1,
          set_connection_params/2,
-         get/1, set/2]).
+         get/1, set/2, all_keys/0]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
 -include("epgsql_pool.hrl").
@@ -46,6 +46,12 @@ set(Key, Value) ->
     gen_server:call(?MODULE, {save, {settings, Key}, Value}).
 
 
+-spec all_keys() -> [atom()].
+all_keys() ->
+    [connection_timeout, query_timeout, pooler_get_worker_timeout,
+     max_reconnect_timeout, min_reconnect_timeout, keep_alive_timeout].
+
+
 %%% gen_server API
 
 -spec init(gs_args()) -> gs_init_reply().

+ 1 - 1
src/epgsql_pool_utils.erl

@@ -40,7 +40,7 @@ reconnect(#epgsql_connection{reconnect_attempt = Attempt} = Connection) ->
     MaxTimeout = epgsql_pool_settings:get(max_reconnect_timeout),
     MinTimeout = epgsql_pool_settings:get(min_reconnect_timeout),
     Timeout = exponential_backoff(Attempt, 10, MinTimeout, MaxTimeout),
-    error_logger:info_msg("epgsql_pool reconnect after ~p attempt ~p", [Timeout, Attempt]),
+    error_logger:warning_msg("epgsql_pool reconnect after ~p attempt ~p", [Timeout, Attempt]),
     erlang:send_after(Timeout, self(), open_connection),
     Connection#epgsql_connection{reconnect_attempt = Attempt + 1}.
 

+ 34 - 0
test/epgsql_pool_tests.erl

@@ -0,0 +1,34 @@
+-module(epgsql_pool_tests).
+
+-include("epgsql_pool.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+
+get_set_settings_test() ->
+    epgsql_pool_settings:start_link(),
+
+    ?assertEqual(#{connection_timeout => 10000,
+                   keep_alive_timeout => 60000,
+                   max_reconnect_timeout => 5000,
+                   min_reconnect_timeout => 100,
+                   pooler_get_worker_timeout => 10000,
+                   query_timeout => 10000},
+                 epgsql_pool:get_settings()),
+
+    ok = epgsql_pool:set_settings(#{aa => bb,
+                                    cc => dd,
+                                    max_reconnect_timeout => 777,
+                                    min_reconnect_timeout => 42,
+                                    dd => 42,
+                                    query_timeout => 555}),
+
+    ?assertEqual(#{connection_timeout => 10000,
+                   keep_alive_timeout => 60000,
+                   max_reconnect_timeout => 777,
+                   min_reconnect_timeout => 42,
+                   pooler_get_worker_timeout => 10000,
+                   query_timeout => 555},
+                 epgsql_pool:get_settings()),
+
+    epgsql_pool_settings ! stop,
+    ok.