Просмотр исходного кода

WIP on supervisor based pools.

pidq_sup is top-level and should eventually supervise the pidq
gen_server as well as the pidq_pool_sup supervisor.

You dynamically create pools by calling
supervisor:start_child(pidq_pool_sup, [A1]) with A1 containing details
of how to start children of that pool.

pidq_pooled_worker_sup is a simple_one_for_one supervisor for the pooled
workers.
Seth Falcon 14 лет назад
Родитель
Сommit
f26ee7dc3a
6 измененных файлов с 71 добавлено и 22 удалено
  1. 23 0
      README.org
  2. 4 0
      src/pidq.erl
  3. 4 1
      src/pidq_app.erl
  4. 16 0
      src/pidq_pool_sup.erl
  5. 15 0
      src/pidq_pooled_worker_sup.erl
  6. 9 21
      src/pidq_sup.erl

+ 23 - 0
README.org

@@ -187,3 +187,26 @@ by entirely different services.  Could be useful for testing a new
 client implementation.
 *** Rename something other than "pid"
 *** Consider ets for state storage rather than dict
+
+#+BEGIN_SRC erlang
+pman:start().
+A1 = {riakc_pb_socket, start_link, ["127.0.0.1", 8081]}.
+{ok, S1} = pidq_pool_sup:start_link(A1).
+supervisor:start_child(S1, []).
+
+{ok, S2} = pidq_sup:start_link([]).
+supervisor:start_child(pidq_pool_sup, [A1]).
+
+#+END_SRC
+
+*** supervision strategy
+
+**** pidq_sup
+top-level supervisor watches pidq gen_server and the pidq_pool_sup
+supervisor.
+**** pidq_pool_sup
+A simple_one_for_one supervisor that is used to create/watch
+pidq_pooled_worker_sup supervisor.  You use this to create a new pool
+and specify the M,F,A of the pooled worker at start.
+**** pidq_pooled_worker_sup
+Another simple_one_for_one that is used to create actual workers.

+ 4 - 0
src/pidq.erl

@@ -28,6 +28,7 @@
 %% ------------------------------------------------------------------
 
 -export([start/1,
+         start_link/1,
          stop/0,
          take_pid/0,
          return_pid/2,
@@ -46,6 +47,9 @@
 %% API Function Definitions
 %% ------------------------------------------------------------------
 
+start_link(Config) ->
+    gen_server:start_link({local, ?SERVER}, ?MODULE, Config, []).
+
 start(Config) ->
     gen_server:start_link({local, ?SERVER}, ?MODULE, Config, []).
 

+ 4 - 1
src/pidq_app.erl

@@ -10,7 +10,10 @@
 %% ===================================================================
 
 start(_StartType, _StartArgs) ->
-    pidq_sup:start_link().
+    case pidq_sup:start_link() of
+        {ok, Pid} -> {ok, Pid};
+        Other -> {error, Other}
+    end.
 
 stop(_State) ->
     ok.

+ 16 - 0
src/pidq_pool_sup.erl

@@ -0,0 +1,16 @@
+-module(pidq_pool_sup).
+
+-behaviour(supervisor).
+
+-export([start_link/0, init/1]).
+
+start_link() ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+    error_logger:info_report(in_pidq_pool_sup),
+    Worker = {pidq_pooled_worker_sup,
+              {pidq_pooled_worker_sup, start_link, []},
+              temporary, 5000, supervisor, [pidq_pooled_worker_sup]},
+    Restart = {simple_one_for_one, 1, 1},
+    {ok, {Restart, [Worker]}}.

+ 15 - 0
src/pidq_pooled_worker_sup.erl

@@ -0,0 +1,15 @@
+-module(pidq_pooled_worker_sup).
+
+-behaviour(supervisor).
+
+-export([start_link/1, init/1]).
+
+start_link(Config) ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, Config).
+
+init({Mod, Fun, Args}) ->
+    error_logger:info_report(in_pidq_pooled_worker_sup),
+    Worker = {Mod, {Mod, Fun, Args}, temporary, brutal_kill, worker, [Mod]},
+    Specs = [Worker],
+    Restart = {simple_one_for_one, 1, 1},
+    {ok, {Restart, Specs}}.

+ 9 - 21
src/pidq_sup.erl

@@ -1,28 +1,16 @@
-
 -module(pidq_sup).
 
 -behaviour(supervisor).
 
-%% API
--export([start_link/0]).
-
-%% Supervisor callbacks
--export([init/1]).
-
-%% Helper macro for declaring children of supervisor
--define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-
-%% ===================================================================
-%% API functions
-%% ===================================================================
-
-start_link() ->
-    supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+-export([start_link/1, init/1]).
 
-%% ===================================================================
-%% Supervisor callbacks
-%% ===================================================================
+start_link(Config) ->
+    supervisor:start_link({local, ?MODULE}, ?MODULE, [Config]).
 
-init([]) ->
-    {ok, { {one_for_one, 5, 10}, []} }.
+init(Config) ->
+    % Pidq = {pidq, {pidq, start_link, [Config]},
+    %         permanent, 5000, worker, [pidq]},
+    PidqPool = {pidq_pool_sup, {pidq_pool_sup, start_link, []},
+                permanent, 5000, supervisor, [pidq_pool_sup]},
+    {ok, {{one_for_one, 5, 10}, [PidqPool]}}.