Browse Source

use listener process instead of callback function for connect/disconnect events

Yuriy Zhloba 7 years ago
parent
commit
f9746139d2
3 changed files with 55 additions and 29 deletions
  1. 2 8
      src/epgsql_pool.app.src
  2. 6 6
      src/epgsql_pool_worker.erl
  3. 47 15
      src/epgsql_test.erl

+ 2 - 8
src/epgsql_pool.app.src

@@ -15,13 +15,7 @@
          {max_reconnect_timeout, 5000},
          {min_reconnect_timeout, 100},
          {keep_alive_timeout, 60000},
-         %%
-         %% callback is function of arity 2 which accepts pool name and connection socket pid
-         %% {on_connect_callback, {Module :: atom(), Function :: atom()}},
-         {on_connect_callback, undefined},
-         %%
-         %% callback is function of arity 1 which accepts pool name
-         %% {on_disconnect_callback, {Module :: atom(), Function :: atom()}}
-         {on_disconnect_callback, undefined}
+         {connect_listener, undefined}, % process pid or name
+         {disconnect_listener, undefined} % process pid or name
         ]}
  ]}.

+ 6 - 6
src/epgsql_pool_worker.erl

@@ -108,20 +108,20 @@ handle_info(open_connection, #state{pool_name = PoolName, connection = Connectio
     end;
 
 handle_info(on_connect, #state{pool_name = PoolName, connection = Connection} = State) ->
-    case application:get_env(epgsql_pool, on_connect_callback) of
+    case application:get_env(epgsql_pool, connect_listener) of
         undefined -> ignore;
         {ok, undefined} -> ignore;
-        {ok, {M, F}} ->
-            #epgsql_connection{sock = Sock} = Connection,
-            M:F(PoolName, Sock)
+        {ok, Listener} ->
+            Listener ! {epgsql_connect, PoolName, self()}
     end,
     {noreply, State};
 
 handle_info(on_disconnect, #state{pool_name = PoolName} = State) ->
-    case application:get_env(epgsql_pool, on_disconnect_callback) of
+    case application:get_env(epgsql_pool, disconnect_listener) of
         undefined -> ignore;
         {ok, undefined} -> ignore;
-        {ok, {M, F}} -> M:F(PoolName)
+        {ok, Listener} ->
+            Listener ! {epgsql_disconnect, PoolName}
     end,
     {noreply, State};
 

+ 47 - 15
src/epgsql_test.erl

@@ -1,20 +1,45 @@
 -module(epgsql_test).
+-behaviour(gen_server).
+
+-export([test_run/0]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
--export([test_run/0, on_connect/2, on_disconnect/1]).
 
--spec test_run() -> ok.
 test_run() ->
     application:ensure_all_started(epgsql_pool),
-    application:set_env(epgsql_pool, on_connect_callback, {?MODULE, on_connect}),
-    application:set_env(epgsql_pool, on_disconnect_callback, {?MODULE, on_disconnect}),
+    gen_server:start_link({local, ?MODULE}, ?MODULE, no_args, []).
+
+
+init(no_args) ->
+    application:set_env(epgsql_pool, connect_listener, self()),
+    application:set_env(epgsql_pool, disconnect_listener, ?MODULE),
+    self() ! start_pool,
+    {ok, no_state}.
+
+
+handle_call(_Request, _From, State) ->
+    {reply, ok, State}.
 
-    Params = #{host => "localhost",
+
+handle_cast(_Request, State) ->
+    {noreply, State}.
+
+
+handle_info(start_pool, State) ->
+    error_logger:info_msg("Start Pool"),
+    Params = #{
+        host => "localhost",
         port => 5432,
         username => "test",
         password => "test",
-        database => "testdb"},
+        database => "testdb"
+    },
     {ok, _} = epgsql_pool:start(my_pool, 2, 2, Params),
+    self() ! do_queries,
+    {noreply, State};
 
+handle_info(do_queries, State) ->
+    error_logger:info_msg("Do Queries"),
     Qs = [
         "CREATE TABLE category (id bigserial, title text, PRIMARY KEY (id))",
         "CREATE TABLE item (id bigserial, category_id bigint, title text, num int, PRIMARY KEY (id), "
@@ -22,25 +47,32 @@ test_run() ->
         "insert into category (title) values ('some'), ('other')",
         "select * from category"
     ],
-
     lists:foreach(
         fun(Q) ->
             Res = epgsql_pool:query(my_pool, Q),
             error_logger:info_msg("Q:~p~nRes:~p~n", [Q, Res])
         end,
         Qs),
-    ok.
-
+    {noreply, State};
 
--spec on_connect(term(), pid()) -> ok.
-on_connect(PoolName, Sock) ->
+handle_info({epgsql_connect, PoolName, Sock}, State) ->
     error_logger:info_msg("On Connect ~p ~p", [PoolName, Sock]),
-    ok.
-
+    Res = epgsql_pool:query(Sock, "select id from category"),
+    error_logger:info_msg("Res:~p", [Res]),
+    {noreply, State};
 
--spec on_disconnect(term()) -> ok.
-on_disconnect(PoolName) ->
+handle_info({epgsql_disconnect, PoolName}, State) ->
     error_logger:info_msg("On Disconnect ~p", [PoolName]),
+    {noreply, State};
+
+handle_info(_Info, State) ->
+    {noreply, State}.
+
+
+terminate(_Reason, _State) ->
     ok.
 
 
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+