This commit is contained in:
ttt161 2024-10-28 12:44:51 +03:00
parent c4e447b434
commit 19c1a4bd2c

View File

@ -20,8 +20,6 @@ query(Pool, Stmt, Params) when is_atom(Pool) ->
query(Conn, Stmt, Params) when is_pid(Conn) -> query(Conn, Stmt, Params) when is_pid(Conn) ->
epgsql:equery(Conn, Stmt, Params). epgsql:equery(Conn, Stmt, Params).
query(empty, _Pool, _Stmt, _Params) ->
{error, overload};
query({error, _} = Err, _Pool, _Stmt, _Params) -> query({error, _} = Err, _Pool, _Stmt, _Params) ->
Err; Err;
query(Conn, Pool, Stmt, Params) when is_pid(Conn) -> query(Conn, Pool, Stmt, Params) when is_pid(Conn) ->
@ -34,8 +32,6 @@ transaction(Pool, Fun) when is_atom(Pool) ->
transaction(Conn, Fun) when is_pid(Conn) -> transaction(Conn, Fun) when is_pid(Conn) ->
epgsql:with_transaction(Conn, Fun). epgsql:with_transaction(Conn, Fun).
transaction(empty, _Pool, _Fun) ->
{error, overload};
transaction({error, _} = Err, _Pool, _Fun) -> transaction({error, _} = Err, _Pool, _Fun) ->
Err; Err;
transaction(Conn, Pool, Fun) when is_pid(Conn) -> transaction(Conn, Pool, Fun) when is_pid(Conn) ->
@ -48,8 +44,8 @@ with(Pool, Fun) when is_atom(Pool) ->
with(Conn, Fun) when is_pid(Conn) -> with(Conn, Fun) when is_pid(Conn) ->
Fun(Conn). Fun(Conn).
with(empty, _Pool, _F) -> with({error, _} = Err, _Pool, _F) ->
{error, overload}; Err;
with(Conn, Pool, Fun) when is_pid(Conn) -> with(Conn, Pool, Fun) when is_pid(Conn) ->
Result = Fun(Conn), Result = Fun(Conn),
ok = epg_pool_mgr:checkin(Pool, self(), Conn), ok = epg_pool_mgr:checkin(Pool, self(), Conn),
@ -63,11 +59,14 @@ get_connection(Pool, Deadline) ->
Now = erlang:system_time(millisecond), Now = erlang:system_time(millisecond),
case epg_pool_mgr:checkout(Pool) of case epg_pool_mgr:checkout(Pool) of
empty when Now < Deadline -> empty when Now < Deadline ->
logger:warning("pg pool ~p empty", [Pool]),
timer:sleep(100), timer:sleep(100),
get_connection(Pool, Deadline); get_connection(Pool, Deadline);
empty -> empty ->
logger:error("pg pool ~p checkout timeout", [Pool]),
{error, overload}; {error, overload};
{error, _} = Err -> {error, Reason} = Err ->
logger:error("pg pool ~p error: ~p", [Reason]),
Err; Err;
Connection when is_pid(Connection) -> Connection when is_pid(Connection) ->
Connection Connection