Browse Source

Rename socket_passive callback to handle_socket_passive and move its call to the epgsql module

Yury Yantsevich 2 years ago
parent
commit
af31b47059
4 changed files with 21 additions and 10 deletions
  1. 1 1
      doc/streaming.md
  2. 17 6
      src/epgsql.erl
  3. 1 1
      src/epgsql_sock.erl
  4. 2 2
      test/epgsql_replication_SUITE.erl

+ 1 - 1
doc/streaming.md

@@ -171,6 +171,6 @@ The `active` parameter of the socket will be set to the same value as it was con
 the connection's options.
 
 When the connection is in the synchronous mode, a provided callback module must implement
-`socket_passive/1` function, which receives a current callback state and should
+`handle_socket_passive/1` function, which receives a current callback state and should
 return `{ok, NewCallbackState}`. The callback should not call `epgsql:activate/1` directly
 because it results in a deadlock.

+ 17 - 6
src/epgsql.erl

@@ -39,7 +39,10 @@
          start_replication/7,
          to_map/1,
          activate/1]).
--export([handle_x_log_data/5]).                 % private
+%% private
+-export([ handle_x_log_data/5
+        , handle_socket_passive/2
+        ]).
 
 -export_type([connection/0, connect_option/0, connect_opts/0,
               connect_error/0, query_error/0, sql_query/0, column/0,
@@ -158,6 +161,10 @@
 %% Handles a XLogData Message (StartLSN, EndLSN, WALRecord, CbState).
 %% Return: {ok, LastFlushedLSN, LastAppliedLSN, NewCbState}
 -callback handle_x_log_data(lsn(), lsn(), binary(), cb_state()) -> {ok, lsn(), lsn(), cb_state()}.
+
+%% Handles socket_passive message.
+%% Return: {ok, NewCbState}.
+-callback handle_socket_passive(cb_state()) -> {ok, cb_state()}.
 %% -------------
 
 %% -- client interface --
@@ -534,6 +541,10 @@ standby_status_update(Connection, FlushedLSN, AppliedLSN) ->
 handle_x_log_data(Mod, StartLSN, EndLSN, WALRecord, Repl) ->
     Mod:handle_x_log_data(StartLSN, EndLSN, WALRecord, Repl).
 
+-spec handle_socket_passive(atom(), cb_state()) -> {ok, cb_state()}.
+handle_socket_passive(Mod, CbState) ->
+    Mod:handle_socket_passive(CbState).
+
 -type replication_option() ::
     {align_lsn, boolean()}. %% Align last applied and flushed LSN with last received LSN
                             %%  after Primary keepalive message with ReplyRequired flag
@@ -575,11 +586,11 @@ to_map(List) when is_list(List) ->
 
 %% @doc Activates TCP or SSL socket of the connection.
 %%
-%% If {active, X} is set in:
-%% - `tcp_opts` and the current mode is TCP or
-%% - `ssl_opts` and the current mode is SSL
-%% the function sets {active, X} on the connection socket.
-%% It sets {active, true} otherwise.
+%% If `{active, X}' is set in:
+%% - `tcp_opts' and the current mode is TCP or
+%% - `ssl_opts' and the current mode is SSL
+%% the function sets `{active, X}' on the connection socket.
+%% It sets `{active, true}' otherwise.
 %%
 %% @param Connection connection
 %% @returns `ok' or `{error, Reason}'

+ 1 - 1
src/epgsql_sock.erl

@@ -334,7 +334,7 @@ send_socket_pasive(#state{subproto_state = #repl{receiver = Rec}} = State) when
 send_socket_pasive(#state{subproto_state = #repl{ cbmodule = CbMod
                                                 , cbstate = CbState} = Repl
                          } = State) ->
-    {ok, NewCbState} = CbMod:socket_passive(CbState),
+    {ok, NewCbState} = epgsql:handle_socket_passive(CbMod, CbState),
     NewRepl = Repl#repl{cbstate = NewCbState},
     State#state{subproto_state = NewRepl}.
 

+ 2 - 2
test/epgsql_replication_SUITE.erl

@@ -17,7 +17,7 @@
 
           %% Callbacks
         , handle_x_log_data/4
-        , socket_passive/1
+        , handle_socket_passive/1
         ]).
 
 init_per_suite(Config) ->
@@ -169,6 +169,6 @@ handle_x_log_data(StartLSN, EndLSN, Data, CbState) ->
   Pid ! {epgsql, C, {x_log_data, StartLSN, EndLSN, Data}},
   {ok, EndLSN, EndLSN, CbState}.
 
-socket_passive({C, _Pid} = CbState) ->
+handle_socket_passive({C, _Pid} = CbState) ->
   spawn(fun() -> epgsql:activate(C) end),
   {ok, CbState}.