aboutsummaryrefslogtreecommitdiffstats
path: root/src/mysqlerl_sup.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/mysqlerl_sup.erl')
-rw-r--r--src/mysqlerl_sup.erl23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/mysqlerl_sup.erl b/src/mysqlerl_sup.erl
index 1ebfbf2..361fe2b 100644
--- a/src/mysqlerl_sup.erl
+++ b/src/mysqlerl_sup.erl
@@ -3,14 +3,25 @@
-behavior(supervisor).
--export([start_link/0, init/1]).
+-export([start_link/0, init/1, connect/6, random_child/0]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+connect(Host, Port, Database, User, Password, Options) ->
+ supervisor:start_child(?MODULE, [self(), Host, Port, Database,
+ User, Password, Options]).
+
+random_child() ->
+ case get_pids() of
+ [] -> {error, no_connections};
+ Pids -> lists:nth(erlang:phash(now(), length(Pids)), Pids)
+ end.
+
init([]) ->
- ConnectionSup = {mysqlerl_connection_sup,
- {mysqlerl_connection_sup, start_link, []},
- permanent, infinity, supervisor,
- [mysqlerl_connection_sup]},
- {ok, {{one_for_one, 10, 5}, [ConnectionSup]}}.
+ Connection = {undefined, {mysqlerl_connection, start_link, []},
+ transient, 5, worker, [mysqlerl_connection]},
+ {ok, {{simple_one_for_one, 10, 5}, [Connection]}}.
+
+get_pids() ->
+ [Pid || {_Id, Pid, _Type, _Modules} <- supervisor:which_children(?MODULE)].