Просмотр исходного кода

fix all_keys, check pool_name, remove app.erl

Yuriy Zhloba 9 лет назад
Родитель
Сommit
8ce428eed1
4 измененных файлов с 40 добавлено и 51 удалено
  1. 1 1
      Makefile
  2. 0 1
      src/epgsql_pool.app.src
  3. 39 15
      src/epgsql_pool.erl
  4. 0 34
      src/epgsql_pool_app.erl

+ 1 - 1
Makefile

@@ -8,7 +8,7 @@ compile:
 
 
 run:
-	erl -pa ebin -pa deps/*/ebin -boot start_sasl -s epgsql_pool_app test_run
+	erl -pa ebin -pa deps/*/ebin -boot start_sasl -s epgsql_pool test_run
 
 
 tests:

+ 0 - 1
src/epgsql_pool.app.src

@@ -6,7 +6,6 @@
   {vsn, "1.0.0"},
   {registered, []},
   {applications, [epgsql, pooler]},
-  {mod, {epgsql_pool_app, []}},
   {env, [
          {connection_timeout, 10000},
          {query_timeout, 10000},

+ 39 - 15
src/epgsql_pool.erl

@@ -4,7 +4,8 @@
          validate_connection_params/1,
          query/2, query/3, query/4,
          transaction/2,
-         get_settings/0, set_settings/1
+         get_settings/0, set_settings/1,
+         test_run/0
         ]).
 
 -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) ->
     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()}.
@@ -151,6 +155,24 @@ set_settings(Map) ->
     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
 
 -spec get_worker(pool_name()) -> {ok, pid()} | {error, term()}.
@@ -167,6 +189,8 @@ get_worker(PoolName) ->
 
 -spec all_keys() -> [atom()].
 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)).

+ 0 - 34
src/epgsql_pool_app.erl

@@ -1,34 +0,0 @@
--module(epgsql_pool_app).
-
--behaviour(application).
-
--export([start/2, stop/1, test_run/0]).
-
--include("epgsql_pool.hrl").
-
-
--spec start(term(), term()) -> {ok, pid()}.
-start(_StartType, _StartArgs) ->
-    {ok, self()}.
-
-
--spec stop(term()) -> ok.
-stop(_State) ->
-    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, 1, 1, Params),
-
-    Res1 = epgsql_pool:query(my_pool, "select * from category"),
-    error_logger:info_msg("Res1: ~p", [Res1]),
-
-    ok.