Commits

Anonymous committed 88ea9b4

Create pool workers specifications statically.

Comments (0)

Files changed (3)

 				size=Size
 			},
 
-			{ok, _SupPid} = emongo_sup:start_pool(PoolId, Host, Port),
-			Pool1 = do_open_connections(Pool),
-			{reply, ok, State#state{pools=[{PoolId, Pool1}|Pools]}}
+			{ok, _SupPid} = emongo_sup:start_pool(PoolId, Host, Port, Size),
+			{reply, ok, State#state{pools=[{PoolId, Pool}|Pools]}}
 	end;
 
 handle_call({pid, PoolId}, _From, #state{pools=Pools}=State) ->
 %%% Internal functions
 %%--------------------------------------------------------------------
 
-do_open_connections(#pool{size=Size}=Pool) ->
-	% each connection is an emongo_server supervised by simple_one_for_one
-	% emongo_server_sup supervisor
-	F = fun(_) -> {ok, _} = emongo_server_sup:start_child(Pool#pool.id) end,
-	lists:foreach(F, lists:seq(1, Size)),
-	Pool.
-
 dec2hex(Dec) ->
 	dec2hex(<<>>, Dec).
 

src/emongo_server_sup.erl

-%% simple_one_for_one supervisor for emongo_server instances
+%% one_for_one supervisor for emongo_server instances
 %% there should be one emongo_server_sup instance for each pool, that then
 %% supervises emongo_server instances based on the size of pool
 -module(emongo_server_sup).
 
 -behaviour(supervisor).
 
--export([start_link/3, child_count/1, start_child/1, nth_child_pid/2, init/1]).
+-export([start_link/4, child_count/1, start_child/1, nth_child_pid/2, init/1]).
 
 %%%%%%%%%%%%%%%%
 %% public api %%
 %%%%%%%%%%%%%%%%
 
-start_link(PoolId, Host, Port) ->
-	supervisor:start_link(?MODULE, [PoolId, Host, Port]).
+start_link(PoolId, Host, Port, Size) ->
+    supervisor:start_link(?MODULE, [PoolId, Host, Port, Size]).
 
 child_count(PoolId) ->
     length(supervisor:which_children(pool_pid(PoolId))).
 		N > length(Children) ->
 			throw(badarg);
 		true ->
-			{undefined, Pid, worker, [emongo_server]} = lists:nth(N, Children),
+			{_, Pid, worker, [emongo_server]} = lists:nth(N, Children),
 			Pid
 	end.
 
 %% supervisor callbacks %%
 %%%%%%%%%%%%%%%%%%%%%%%%%%
 
-init([PoolId, Host, Port]) ->
-	{ok, {{simple_one_for_one, 10, 10}, [
-		{emongo_server, {emongo_server, start_link, [PoolId, Host, Port]},
-		 permanent, brutal_kill, worker, [emongo_server]}
-	]}}.
+init([PoolId, Host, Port, Size]) ->
+    {ok, {{one_for_one, Size * 10, 10},
+          [
+           {Id,
+            {emongo_server, start_link, [PoolId, Host, Port]},
+            permanent, brutal_kill, worker, [emongo_server]}
+           || Id <- lists:seq(1, Size)
+          ]}}.
 
 -behaviour(supervisor).
 
--export([start_link/0, start_pool/3, init/1]).
+-export([start_link/0, start_pool/4, init/1]).
 
 %%%%%%%%%%%%%%%%
 %% public api %%
 
 start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
 
-start_pool(PoolId, Host, Port) ->
+start_pool(PoolId, Host, Port, Size) ->
 	% emongo_server_sup instances are added dynamically, one for each pool
 	supervisor:start_child(?MODULE, {PoolId,
-		{emongo_server_sup, start_link, [PoolId, Host, Port]},
+		{emongo_server_sup, start_link, [PoolId, Host, Port, Size]},
 		permanent, infinity, supervisor, [emongo_server_sup]
 	}).
 
 	{ok, {{one_for_one, 10, 10}, [
 		{emongo, {emongo, start_link, []},
 		 permanent, 5000, worker, [emongo]}
-	]}}.
+	]}}.