1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
-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)].
|