Force reconnect when nested checkout detected

This commit is contained in:
ttt161 2024-10-09 08:34:11 +03:00
parent a507052d66
commit c4e447b434
3 changed files with 30 additions and 7 deletions

View File

@ -13,6 +13,18 @@
default_pool => #{
database => default_db,
size => 10
},
front_pool => #{
database => default_db,
size => 10
},
back_pool => #{
database => default_db,
size => 10
},
vip_pool => #{
database => default_db,
size => 10
}
}},
%% Must specified for umbrella application

View File

@ -51,10 +51,18 @@ init([PoolName, DbParams, Size]) ->
%%
handle_call(
checkout, {Pid, _Ref},
State = #epg_pool_mgr_state{owners = Owners}
) when erlang:is_map_key(Pid, Owners)->
{reply, {error, nested_checkout}, State};
checkout, {Pid, _},
State = #epg_pool_mgr_state{connections = Conns, monitors = Monitors, owners = Owners}
) when erlang:is_map_key(Pid, Owners) ->
{{Ref, Conn}, NewOwners} = maps:take(Pid, Owners),
_ = catch erlang:demonitor(Ref),
NewConns = queue:delete(Conn, Conns),
NewMonitors = demonitor_and_close(Conn, Monitors),
{
reply,
{error, nested_checkout},
State#epg_pool_mgr_state{connections = NewConns, monitors = NewMonitors, owners = NewOwners}
};
handle_call(
checkout, {Pid, _Ref},
State = #epg_pool_mgr_state{connections = Conns, owners = Owners}
@ -104,7 +112,7 @@ handle_cast(
undefined ->
skip;
{Ref, _Conn} ->
_ = erlang:demonitor(Ref, [flush])
_ = erlang:demonitor(Ref)
end,
{
noreply,

View File

@ -33,7 +33,7 @@ handle_info(
) ->
%epg_pool_mgr:remove(Pool, self(), Pid),
logger:error("db connection lost. pool: ~p. database: ~p", [Pool, DB]),
reconnect_timer(),
reconnect_timer(50),
{noreply, State#epg_pool_wrk_state{connection = undefined, monitor = undefined}};
handle_info({timeout, _Ref, reconnect}, State = #epg_pool_wrk_state{}) ->
@ -49,7 +49,10 @@ code_change(_OldVsn, State = #epg_pool_wrk_state{}, _Extra) ->
%%
reconnect_timer() ->
erlang:start_timer(5000, self(), reconnect).
reconnect_timer(5000).
reconnect_timer(Timeout) ->
erlang:start_timer(Timeout, self(), reconnect).
connect(#epg_pool_wrk_state{pool = Pool, params = #{database := DB} = Params} = State) ->
try epgsql:connect(Params) of