aboutsummaryrefslogtreecommitdiffstats
path: root/src/mysqlerl_connection_sup.erl
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2008-03-01 13:10:46 -0500
committerBrian Cully <bjc@kublai.com>2008-03-01 13:10:46 -0500
commit8ffa0cdb1f19cd56d7adf63893e5d158daa9aa3c (patch)
treef304bbcc3543be536089fafcad2fa4d3fc2fbd5f /src/mysqlerl_connection_sup.erl
downloadmysqlerl-8ffa0cdb1f19cd56d7adf63893e5d158daa9aa3c.tar.gz
mysqlerl-8ffa0cdb1f19cd56d7adf63893e5d158daa9aa3c.zip
Initial commit
Diffstat (limited to 'src/mysqlerl_connection_sup.erl')
-rw-r--r--src/mysqlerl_connection_sup.erl29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/mysqlerl_connection_sup.erl b/src/mysqlerl_connection_sup.erl
new file mode 100644
index 0000000..4aa3fc6
--- /dev/null
+++ b/src/mysqlerl_connection_sup.erl
@@ -0,0 +1,29 @@
+-module(mysqlerl_connection_sup).
+-author('bjc@kublai.com').
+
+-behavior(supervisor).
+
+-export([start_link/0, connect/6, random_child/0]).
+
+-export([init/1]).
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+connect(Host, Port, Database, User, Password, Options) ->
+ supervisor:start_child(?MODULE, [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([]) ->
+ 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)].