diff options
| author | Brian Cully <bjc@kublai.com> | 2012-02-06 19:44:47 -0500 |
|---|---|---|
| committer | Brian Cully <github.20.shmit@spamgourmet.com> | 2012-02-06 19:46:56 -0500 |
| commit | 50f3c688a7cd4819680dbd19b9933c6cb5e62ca7 (patch) | |
| tree | 5d9bf2edac40d874ec05bafc1e1e38b443879ce3 /lib/src/mysqlerl_connection_sup.erl | |
| parent | d3b118481833acd44d724c06ce8c9d6b6ff2838a (diff) | |
| download | mysqlerl-50f3c688a7cd4819680dbd19b9933c6cb5e62ca7.tar.gz mysqlerl-50f3c688a7cd4819680dbd19b9933c6cb5e62ca7.zip | |
Move most files into lib, add top-level makefile
Diffstat (limited to 'lib/src/mysqlerl_connection_sup.erl')
| -rw-r--r-- | lib/src/mysqlerl_connection_sup.erl | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/src/mysqlerl_connection_sup.erl b/lib/src/mysqlerl_connection_sup.erl new file mode 100644 index 0000000..6e1632a --- /dev/null +++ b/lib/src/mysqlerl_connection_sup.erl @@ -0,0 +1,31 @@ +-module(mysqlerl_connection_sup). +-author('bjc@kublai.com'). + +-behavior(supervisor). + +-export([random_child/0]). +-export([start_link/0, connect/6]). + +-export([init/1]). + +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([]) -> + 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)]. |
