Skip to content

Commit f09b4ca

Browse files
committed
Expose pgapp connected workers and fix dialyzer errors
1 parent 4e89f39 commit f09b4ca

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ebin/
22
*~
33
deps/
44
*.config
5+
*.plt

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{deps,
22
[
3-
{epgsql, ".*", {git, "git://github.com/epgsql/epgsql.git", {tag, "3.1.0"}}},
3+
{epgsql, ".*", {git, "git://github.com/epgsql/epgsql.git", "792a93c"}},
44
{poolboy, ".*", {git, "git://github.com/devinus/poolboy.git", {tag, "1.4.2"}}}
55
]}.

src/pgapp.erl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
-module(pgapp).
1010

1111
%% API
12-
-export([connect/1, connect/2,
13-
equery/2, equery/3, equery/4,
14-
squery/1, squery/2, squery/3,
15-
with_transaction/1, with_transaction/2, with_transaction/3]).
12+
-export([connect/1, connect/2]).
13+
-export([equery/2, equery/3, equery/4]).
14+
-export([squery/1, squery/2, squery/3]).
15+
-export([with_transaction/1, with_transaction/2, with_transaction/3]).
16+
-export([connected_workers/1]).
1617

1718
%%%===================================================================
1819
%%% API
1920
%%%===================================================================
2021

22+
-spec connect(Settings :: list()) -> {ok, WorkerPid} when
23+
WorkerPid :: pid().
2124
connect(Settings) ->
2225
connect(epgsql_pool, Settings).
2326

27+
-spec connect(PoolName :: atom(),
28+
Settings :: list()) -> {ok, WorkerPid} when
29+
WorkerPid :: pid().
2430
connect(PoolName, Settings) ->
2531
PoolSize = proplists:get_value(size, Settings, 5),
2632
MaxOverflow = proplists:get_value(max_overflow, Settings, 5),
@@ -89,6 +95,13 @@ with_transaction(PoolName, Fun) when is_function(Fun, 0) ->
8995
with_transaction(PoolName, Fun, Timeout) when is_function(Fun, 0) ->
9096
pgapp_worker:with_transaction(PoolName, Fun, Timeout).
9197

98+
-spec connected_workers(PoolName :: atom()) -> {ok, ConnectedWorkerPids} when
99+
ConnectedWorkerPids :: list(pid()).
100+
connected_workers(PoolName) ->
101+
WorkerPids = gen_server:call(PoolName, get_avail_workers),
102+
{ok, [WorkerPid || WorkerPid <- WorkerPids,
103+
pgapp_worker:is_connected(WorkerPid)]}.
104+
92105
%%--------------------------------------------------------------------
93106
%% @doc
94107
%% @spec

src/pgapp_worker.erl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
-behaviour(gen_server).
99
-behaviour(poolboy_worker).
1010

11-
-export([squery/1, squery/2, squery/3,
12-
equery/2, equery/3, equery/4,
13-
with_transaction/2, with_transaction/3]).
11+
-export([squery/1, squery/2, squery/3]).
12+
-export([equery/2, equery/3, equery/4]).
13+
-export([with_transaction/2, with_transaction/3]).
14+
-export([is_connected/1]).
1415

1516
-export([start_link/1]).
1617

@@ -29,34 +30,37 @@
2930
-define(STATE_VAR, '$pgapp_state').
3031

3132
squery(Sql) ->
33+
squery(Sql, ?TIMEOUT).
34+
35+
squery(PoolName, Sql) when is_atom(PoolName) ->
36+
squery(PoolName, Sql, ?TIMEOUT);
37+
squery(Sql, Timeout) ->
3238
case get(?STATE_VAR) of
3339
undefined ->
34-
squery(epgsql_pool, Sql);
40+
squery(epgsql_pool, Sql, Timeout);
3541
Conn ->
3642
epgsql:squery(Conn, Sql)
3743
end.
3844

39-
squery(PoolName, Sql) ->
40-
squery(PoolName, Sql, ?TIMEOUT).
41-
4245
squery(PoolName, Sql, Timeout) ->
4346
poolboy:transaction(PoolName,
4447
fun (Worker) ->
4548
gen_server:call(Worker, {squery, Sql}, Timeout)
4649
end, Timeout).
4750

48-
4951
equery(Sql, Params) ->
52+
equery(Sql, Params, ?TIMEOUT).
53+
54+
equery(PoolName, Sql, Params) when is_atom(PoolName) ->
55+
equery(PoolName, Sql, Params, ?TIMEOUT);
56+
equery(Sql, Params, Timeout) ->
5057
case get(?STATE_VAR) of
5158
undefined ->
52-
equery(epgsql_pool, Sql, Params);
59+
equery(epgsql_pool, Sql, Params, Timeout);
5360
Conn ->
5461
epgsql:equery(Conn, Sql, Params)
5562
end.
5663

57-
equery(PoolName, Sql, Params) ->
58-
equery(PoolName, Sql, Params, ?TIMEOUT).
59-
6064
equery(PoolName, Sql, Params, Timeout) ->
6165
poolboy:transaction(PoolName,
6266
fun (Worker) ->
@@ -74,6 +78,9 @@ with_transaction(PoolName, Fun, Timeout) ->
7478
{transaction, Fun}, Timeout)
7579
end, Timeout).
7680

81+
is_connected(WorkerPid) ->
82+
gen_server:call(WorkerPid, {is_connected}, ?TIMEOUT).
83+
7784
start_link(Args) ->
7885
gen_server:start_link(?MODULE, Args, []).
7986

@@ -93,7 +100,12 @@ handle_call({transaction, Fun}, _From,
93100
put(?STATE_VAR, Conn),
94101
Result = epgsql:with_transaction(Conn, fun(_) -> Fun() end),
95102
erase(?STATE_VAR),
96-
{reply, Result, State}.
103+
{reply, Result, State};
104+
105+
handle_call({is_connected}, _From, #state{conn = undefined} = State) ->
106+
{reply, false, State};
107+
handle_call({is_connected}, _From, #state{conn = _Conn} = State) ->
108+
{reply, true, State}.
97109

98110
handle_cast(reconnect, State) ->
99111
{noreply, connect(State)}.

0 commit comments

Comments
 (0)