Skip to content

Commit

Permalink
replaced jerr by epgsql_pool_log
Browse files Browse the repository at this point in the history
  • Loading branch information
khabinov committed Sep 12, 2012
1 parent ba6c76d commit 92fd653
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
33 changes: 33 additions & 0 deletions src/epgsql_pool_log.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%% Same logic as in jerr.erl is used here
%%
-module(epgsql_pool_log).

-export([
log_info/2,
log_info/3,
log_error/2,
log_error/3,
log_warning/2,
log_warning/3,
log_debug/2,
log_debug/3
]).

new_format(Area, Format) ->
"[" ++ atom_to_list(Area) ++ "] " ++ Format ++ "~n".

log_info(Area, Format) -> log_info(Area, Format, []).
log_info(Area, Format, Args) ->
error_logger:info_msg(new_format(Area, Format), Args).

log_error(Area, Format) -> log_error(Area, Format, []).
log_error(Area, Format, Args) ->
error_logger:error_msg(new_format(Area, Format), Args).

log_warning(Area, Format) -> log_warning(Area, Format, []).
log_warning(Area, Format, Args) ->
error_logger:warning_msg(new_format(Area, Format), Args).

log_debug(Area, Format) -> log_debug(Area, Format, []).
log_debug(Area, Format, Args) ->
error_logger:ingo_msg("DEBUG: " ++ new_format(Area, Format), Args);
54 changes: 27 additions & 27 deletions src/pgsql_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ start_link(Size, Opts) ->
start_link(undefined, Size, Opts) ->
start_link(Size, Opts);
start_link(Name, Size, Opts) ->
jerr:log_info(?MODULE, "[start_link] Starting pgsql pool: ~p of size: ~p", [Name, Size]),
epgsql_pool_log:log_info(?MODULE, "[start_link] Starting pgsql pool: ~p of size: ~p", [Name, Size]),
gen_server:start_link({local, Name}, ?MODULE, {Name, Size, opts(Opts)}, []).

%% @doc Stop the pool, close all db connections
Expand Down Expand Up @@ -78,7 +78,7 @@ init({Name, Size, Opts}) ->
{ok, Connection} ->
[{Connection, now_secs()}];
{error, Error} ->
jerr:log_error(?MODULE, "Failed to connect to PostgreSQL server. Error:~n~p~n", [Error]),
epgsql_pool_log:log_error(?MODULE, "Failed to connect to PostgreSQL server. Error:~n~p~n", [Error]),
[]
end,
{ok, TRef} = timer:send_interval(60000, close_unused),
Expand All @@ -101,31 +101,31 @@ handle_call(get_connection_nowait, From, State) ->
handle_call({get_connection, nowait}, From, State);

handle_call({get_connection, WaitMode}, From, #state{connections = Connections, waiting = Waiting} = State) ->
jerr:log_debug(?MODULE, "[handle_call get_connection] WaitMode: ~p", [WaitMode]),
epgsql_pool_log:log_debug(?MODULE, "[handle_call get_connection] WaitMode: ~p", [WaitMode]),
case Connections of
[{C, _} | T] ->
jerr:log_debug(?MODULE, "[handle_call get_connection] Return existing unused connection"),
epgsql_pool_log:log_debug(?MODULE, "[handle_call get_connection] Return existing unused connection"),
{noreply, deliver(From, C, State#state{connections = T})};
[] ->
jerr:log_debug(?MODULE, "[handle_call get_connection] No free connections."),
epgsql_pool_log:log_debug(?MODULE, "[handle_call get_connection] No free connections."),
case length(State#state.monitors) < State#state.size of
true ->
jerr:log_debug(?MODULE, "[handle_call get_connection] Allocate a new connection and return it."),
epgsql_pool_log:log_debug(?MODULE, "[handle_call get_connection] Allocate a new connection and return it."),
case connect(State#state.opts) of
{ok, C} ->
jerr:log_debug(?MODULE, "[handle_call get_connection] Allocated new connection: ~p", [C]),
epgsql_pool_log:log_debug(?MODULE, "[handle_call get_connection] Allocated new connection: ~p", [C]),
{noreply, deliver(From, C, State)};
{error, Error} ->
jerr:log_error(?MODULE, "[handle_call get_connection] Got error: ~p; return {error, busy}", [Error]),
epgsql_pool_log:log_error(?MODULE, "[handle_call get_connection] Got error: ~p; return {error, busy}", [Error]),
{reply, {error, busy}, State}
end;
false ->
case WaitMode of
wait ->
jerr:log_error(?MODULE, "[handle_call get_connection] Reached max connections, let the requestor wait."),
epgsql_pool_log:log_error(?MODULE, "[handle_call get_connection] Reached max connections, let the requestor wait."),
{noreply, State#state{waiting = queue:in(From, Waiting)}};
nowait ->
jerr:log_error(?MODULE, "[handle_call get_connection] Reached max connections, return {error, busy}"),
epgsql_pool_log:log_error(?MODULE, "[handle_call get_connection] Reached max connections, return {error, busy}"),
{reply, {error, busy}, State}
end
end
Expand All @@ -147,11 +147,11 @@ handle_call(Request, _From, State) ->
handle_cast({return_connection, C}, #state{monitors = Monitors} = State) ->
case lists:keytake(C, 1, Monitors) of
{value, {C, M}, Monitors2} ->
jerr:log_debug(?MODULE, "[handle_cast return_connection] Found connection ~p in the monitors list", [C]),
epgsql_pool_log:log_debug(?MODULE, "[handle_cast return_connection] Found connection ~p in the monitors list", [C]),
erlang:demonitor(M),
{noreply, return(C, State#state{monitors = Monitors2})};
false ->
jerr:log_debug(?MODULE, "[handle_cast return_connection] Have not found connection ~p in the monitors list", [C]),
epgsql_pool_log:log_debug(?MODULE, "[handle_cast return_connection] Have not found connection ~p in the monitors list", [C]),
{noreply, State}
end;

Expand Down Expand Up @@ -181,30 +181,30 @@ handle_info(never_close_unused, State) ->

%% Requestor we are monitoring went down. Kill the associated connection, as it might be in an unknown state.
handle_info({'DOWN', M, process, _Pid, _Info}, #state{monitors = Monitors} = State) ->
jerr:log_debug(?MODULE, "[handle_info DOWN] Requestor we are monitoring went down. monitor: ~p", [M]),
epgsql_pool_log:log_debug(?MODULE, "[handle_info DOWN] Requestor we are monitoring went down. monitor: ~p", [M]),
case lists:keytake(M, 2, Monitors) of
{value, {C, M}, Monitors2} ->
jerr:log_debug(?MODULE, "[handle_info DOWN] closed connection, deleted the monitor from the monitors list."),
epgsql_pool_log:log_debug(?MODULE, "[handle_info DOWN] closed connection, deleted the monitor from the monitors list."),
catch pgsql:close(C),
{noreply, State#state{monitors = Monitors2}};
false ->
jerr:log_debug(?MODULE, "[handle_info DOWN] Have not found the monitor in the monitors list"),
epgsql_pool_log:log_debug(?MODULE, "[handle_info DOWN] Have not found the monitor in the monitors list"),
{noreply, State}
end;

%% One of our database connections went down. Clean up our administration.
handle_info({'EXIT', ConnectionPid, _Reason}, State) ->
jerr:log_debug(?MODULE, "[handle_info EXIT] One of our database connections went down. Clean up our administration. Pid: ~p", [ConnectionPid]),
epgsql_pool_log:log_debug(?MODULE, "[handle_info EXIT] One of our database connections went down. Clean up our administration. Pid: ~p", [ConnectionPid]),
#state{connections = Connections, monitors = Monitors} = State,
Connections2 = proplists:delete(ConnectionPid, Connections),
F = fun({C, M}) when C == ConnectionPid ->
jerr:log_debug(?MODULE, "[handle_info EXIT] demonitor the requestor connected via the closed connection. M: ~p", [M]),
epgsql_pool_log:log_debug(?MODULE, "[handle_info EXIT] demonitor the requestor connected via the closed connection. M: ~p", [M]),
erlang:demonitor(M),
false;
({_, _}) -> true
end,
Monitors2 = lists:filter(F, Monitors),
jerr:log_debug(?MODULE, "[handle_info EXIT] Connections: ~p; Monitors: ~p", [Connections2, Monitors2]),
epgsql_pool_log:log_debug(?MODULE, "[handle_info EXIT] Connections: ~p; Monitors: ~p", [Connections2, Monitors2]),
{noreply, State#state{connections = Connections2, monitors = Monitors2}};

%% Trap unsupported info calls.
Expand All @@ -213,7 +213,7 @@ handle_info(Info, State) ->

terminate(_Reason, State) ->
timer:cancel(State#state.timer),
jerr:log_info(?MODULE, "[terminate] ~p terminating...", [State#state.id]),
epgsql_pool_log:log_info(?MODULE, "[terminate] ~p terminating...", [State#state.id]),
ok.

code_change(_OldVsn, State, _Extra) ->
Expand All @@ -228,34 +228,34 @@ connect(Opts) ->
try
connect_ll(Host, Username, Password, Opts)
catch C:R ->
jerr:log_error(?MODULE, "[connect] Failed to connect to PostgreSQL server. Error:~n~p:~p", [C, R]),
epgsql_pool_log:log_error(?MODULE, "[connect] Failed to connect to PostgreSQL server. Error:~n~p:~p", [C, R]),
{error, {C, R}}
end.

connect_ll(Host, Username, Password, Opts) ->
jerr:log_debug(?MODULE, "[connect_ll] Connect to: ~p", [Host]),
epgsql_pool_log:log_debug(?MODULE, "[connect_ll] Connect to: ~p", [Host]),
{ok, Conn} = pgsql:connect(Host, Username, Password, Opts),
jerr:log_debug(?MODULE, "[connect_ll] Got connection: ~p", [Conn]),
epgsql_pool_log:log_debug(?MODULE, "[connect_ll] Got connection: ~p", [Conn]),
{ok, [], []} = pgsql:squery(Conn, "SET search_path TO " ++ proplists:get_value(schema, Opts)),
jerr:log_debug(?MODULE, "[connect_ll] Return connection"),
epgsql_pool_log:log_debug(?MODULE, "[connect_ll] Return connection"),
{ok, Conn}.

deliver({Pid,_Tag} = From, C, #state{monitors=Monitors} = State) ->
M = erlang:monitor(process, Pid),
gen_server:reply(From, {ok, C}),
jerr:log_debug(?MODULE, "[deliver] Connection: ~p; New monitor: ~p", [C, M]),
epgsql_pool_log:log_debug(?MODULE, "[deliver] Connection: ~p; New monitor: ~p", [C, M]),
State#state{ monitors=[{C, M} | Monitors] }.

return(C, #state{connections = Connections, waiting = Waiting} = State) ->
jerr:log_debug(?MODULE, "[return] return connection: ~p", [C]),
epgsql_pool_log:log_debug(?MODULE, "[return] return connection: ~p", [C]),
case queue:out(Waiting) of
{{value, From}, Waiting2} ->
jerr:log_debug(?MODULE, "[return] got a pending request from the wait list: ~p, deliver the connection to it", [From]),
epgsql_pool_log:log_debug(?MODULE, "[return] got a pending request from the wait list: ~p, deliver the connection to it", [From]),
State2 = deliver(From, C, State),
State2#state{waiting = Waiting2};
{empty, _Waiting} ->
Connections2 = Connections ++ [{C, now_secs()}],
jerr:log_debug(?MODULE, "[return] returned the connection to the connections pool, bumped last usage time"),
epgsql_pool_log:log_debug(?MODULE, "[return] returned the connection to the connections pool, bumped last usage time"),
State#state{connections = Connections2}
end.

Expand Down

0 comments on commit 92fd653

Please sign in to comment.