Skip to content

Commit

Permalink
Return proper errors when deleting a pool that doesn't exist or creat…
Browse files Browse the repository at this point in the history
…e one that already exist
  • Loading branch information
Silviu Caragea committed Jun 30, 2018
1 parent a11bb8e commit 8105152
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
48 changes: 24 additions & 24 deletions src/mysql_connection_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,33 @@ code_change(_OldVsn, State, _Extra) ->
% internals

internal_create_pool(PoolName, Size, MaxOverflow, ConnectionOptions, AppPid) ->
try
PoolName = ets:new(PoolName, [
set,
named_table,
public,
{read_concurrency, true},
{heir, AppPid, ok}
]),

PoolArgs = [
{max_overflow, MaxOverflow},
{size, Size},
{name, {local, PoolName}},
{worker_module, mysql_connection_proxy}
],
ChildSpecs = poolboy:child_spec(PoolName, PoolArgs, [PoolName, ConnectionOptions]),
mysql_pool_sup:add_pool(PoolName, ChildSpecs)
catch
_:Error ->
?ERROR_MSG("creating pool: ~p failed with error: ~p", [PoolName, Error]),
Error
case mysql_pool_sup:has_pool(PoolName) of
false ->
try
PoolName = ets:new(PoolName, [set, named_table, public, {read_concurrency, true}, {heir, AppPid, ok}]),

PoolArgs = [
{max_overflow, MaxOverflow},
{size, Size},
{name, {local, PoolName}},
{worker_module, mysql_connection_proxy}
],
ChildSpecs = poolboy:child_spec(PoolName, PoolArgs, [PoolName, ConnectionOptions]),
mysql_pool_sup:add_pool(PoolName, ChildSpecs)
catch
_:Error ->
?ERROR_MSG("creating pool: ~p failed with error: ~p", [PoolName, Error]),
Error
end;
_ ->
{error, already_started}
end.

internal_dispose_pool(PoolName) ->
case catch ets:delete(PoolName) of
case mysql_pool_sup:has_pool(PoolName) of
true ->
true = ets:delete(PoolName),
mysql_pool_sup:remove_pool(PoolName);
Error ->
Error
_ ->
{error, not_found}
end.
18 changes: 11 additions & 7 deletions src/mysql_pool_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
start_link/1,
add_pool/2,
remove_pool/1,
has_pool/1,
init/1
]).

start_link(AppPid) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [AppPid]).

add_pool(PoolName, ChildSpecs) ->
case supervisor:start_child(?MODULE, ChildSpecs) of
{error, already_present} ->
supervisor:restart_child(?MODULE, PoolName);
Other ->
Other
end.
add_pool(_PoolName, ChildSpecs) ->
supervisor:start_child(?MODULE, ChildSpecs).

remove_pool(PoolName) ->
case supervisor:terminate_child(?MODULE, PoolName) of
Expand All @@ -28,6 +24,14 @@ remove_pool(PoolName) ->
Error
end.

has_pool(PoolName) ->
case supervisor:get_childspec(?MODULE, PoolName) of
{ok, _} ->
true;
{error, not_found} ->
false
end.

init(Args) ->
Childrens = [
worker(mysql_connection_manager, infinity, Args)
Expand Down

0 comments on commit 8105152

Please sign in to comment.