diff options
-rw-r--r-- | src/Makefile | 3 | ||||
-rw-r--r-- | src/mysqlerl.app | 6 | ||||
-rw-r--r-- | src/mysqlerl.erl | 3 | ||||
-rw-r--r-- | src/mysqlerl_connection_sup.erl | 31 | ||||
-rw-r--r-- | src/mysqlerl_sup.erl | 23 |
5 files changed, 22 insertions, 44 deletions
diff --git a/src/Makefile b/src/Makefile index 4782487..ab690a5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,8 +19,7 @@ BEAMDIR = ../ebin BINS = $(PRIVDIR)/mysqlerl $(BEAMDIR)/mysqlerl.app MYSQLERLOBJS = io.o log.o msg.o mysqlerl.o BEAMS = mysqlerl.beam mysqlerl_app.beam mysqlerl_connection.beam \ - mysqlerl_connection_sup.beam mysqlerl_port.beam \ - mysqlerl_port_sup.beam mysqlerl_sup.beam + mysqlerl_port.beam mysqlerl_port_sup.beam mysqlerl_sup.beam all: $(PRIVDIR) $(BEAMDIR) $(BINS) diff --git a/src/mysqlerl.app b/src/mysqlerl.app index 5687463..704f1dd 100644 --- a/src/mysqlerl.app +++ b/src/mysqlerl.app @@ -4,9 +4,9 @@ {application, mysqlerl, [{description, "mysqlerl"}, {vsn, "0"}, - {modules, [mysqlerl, mysqlerl_app, mysqlerl_connection_sup, - mysqlerl_connection, mysql_port_sup, mysql_port]}, - {registered, [mysqlerl, mysqlerl_app, mysqlerl_connection_sup]}, + {modules, [mysqlerl, mysqlerl_app, mysqlerl_sup, mysqlerl_connection, + mysql_port_sup, mysql_port]}, + {registered, [mysqlerl, mysqlerl_app, mysqlerl_sup]}, {applications, [kernel, stdlib]}, {env, []}, {mod, {mysqlerl_app, []}}]}. diff --git a/src/mysqlerl.erl b/src/mysqlerl.erl index d6a3cf6..0b7d383 100644 --- a/src/mysqlerl.erl +++ b/src/mysqlerl.erl @@ -65,8 +65,7 @@ commit(Ref, rollback, Timeout) -> %% {ok, Ref} | {error, Reason} %% Ref = connection_reference() connect(Host, Port, Database, User, Password, Options) -> - mysqlerl_connection_sup:connect(Host, Port, Database, - User, Password, Options). + mysqlerl_sup:connect(Host, Port, Database, User, Password, Options). %% Arguments: %% Ref = connection_reference() diff --git a/src/mysqlerl_connection_sup.erl b/src/mysqlerl_connection_sup.erl deleted file mode 100644 index 6e1632a..0000000 --- a/src/mysqlerl_connection_sup.erl +++ /dev/null @@ -1,31 +0,0 @@ --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)]. 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)]. |