src/worker_pool.erl
author Simon MacMullen <simon@rabbitmq.com>
Tue, 21 Oct 2014 14:58:56 +0100
changeset 14071 d3b15f1e7e6e
parent 13882 3675697d8d57
permissions -rw-r--r--
Added tag rabbitmq_v3_4_0 for changeset e88d71c0fcdf
     1 %% The contents of this file are subject to the Mozilla Public License
     2 %% Version 1.1 (the "License"); you may not use this file except in
     3 %% compliance with the License. You may obtain a copy of the License
     4 %% at http://www.mozilla.org/MPL/
     5 %%
     6 %% Software distributed under the License is distributed on an "AS IS"
     7 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
     8 %% the License for the specific language governing rights and
     9 %% limitations under the License.
    10 %%
    11 %% The Original Code is RabbitMQ.
    12 %%
    13 %% The Initial Developer of the Original Code is GoPivotal, Inc.
    14 %% Copyright (c) 2007-2014 GoPivotal, Inc.  All rights reserved.
    15 %%
    16 
    17 -module(worker_pool).
    18 
    19 %% Generic worker pool manager.
    20 %%
    21 %% Supports nested submission of jobs (nested jobs always run
    22 %% immediately in current worker process).
    23 %%
    24 %% Possible future enhancements:
    25 %%
    26 %% 1. Allow priorities (basically, change the pending queue to a
    27 %% priority_queue).
    28 
    29 -behaviour(gen_server2).
    30 
    31 -export([start_link/0, submit/1, submit/2, submit_async/1, ready/1,
    32          idle/1]).
    33 
    34 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
    35          terminate/2, code_change/3]).
    36 
    37 %%----------------------------------------------------------------------------
    38 
    39 -ifdef(use_specs).
    40 
    41 -type(mfargs() :: {atom(), atom(), [any()]}).
    42 
    43 -spec(start_link/0 :: () -> {'ok', pid()} | {'error', any()}).
    44 -spec(submit/1 :: (fun (() -> A) | mfargs()) -> A).
    45 -spec(submit/2 :: (fun (() -> A) | mfargs(), 'reuse' | 'single') -> A).
    46 -spec(submit_async/1 :: (fun (() -> any()) | mfargs()) -> 'ok').
    47 -spec(ready/1 :: (pid()) -> 'ok').
    48 -spec(idle/1 :: (pid()) -> 'ok').
    49 
    50 -endif.
    51 
    52 %%----------------------------------------------------------------------------
    53 
    54 -define(SERVER, ?MODULE).
    55 -define(HIBERNATE_AFTER_MIN, 1000).
    56 -define(DESIRED_HIBERNATE, 10000).
    57 
    58 -record(state, { available, pending }).
    59 
    60 %%----------------------------------------------------------------------------
    61 
    62 start_link() -> gen_server2:start_link({local, ?SERVER}, ?MODULE, [],
    63                                        [{timeout, infinity}]).
    64 
    65 submit(Fun) ->
    66     submit(Fun, reuse).
    67 
    68 %% ProcessModel =:= single is for working around the mnesia_locker bug.
    69 submit(Fun, ProcessModel) ->
    70     case get(worker_pool_worker) of
    71         true -> worker_pool_worker:run(Fun);
    72         _    -> Pid = gen_server2:call(?SERVER, {next_free, self()}, infinity),
    73                 worker_pool_worker:submit(Pid, Fun, ProcessModel)
    74     end.
    75 
    76 submit_async(Fun) -> gen_server2:cast(?SERVER, {run_async, Fun}).
    77 
    78 ready(WPid) -> gen_server2:cast(?SERVER, {ready, WPid}).
    79 
    80 idle(WPid) -> gen_server2:cast(?SERVER, {idle, WPid}).
    81 
    82 %%----------------------------------------------------------------------------
    83 
    84 init([]) ->
    85     {ok, #state { pending = queue:new(), available = ordsets:new() }, hibernate,
    86      {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}.
    87 
    88 handle_call({next_free, CPid}, From, State = #state { available = [],
    89                                                       pending   = Pending }) ->
    90     {noreply, State#state{pending = queue:in({next_free, From, CPid}, Pending)},
    91      hibernate};
    92 handle_call({next_free, CPid}, _From, State = #state { available =
    93                                                            [WPid | Avail1] }) ->
    94     worker_pool_worker:next_job_from(WPid, CPid),
    95     {reply, WPid, State #state { available = Avail1 }, hibernate};
    96 
    97 handle_call(Msg, _From, State) ->
    98     {stop, {unexpected_call, Msg}, State}.
    99 
   100 handle_cast({ready, WPid}, State) ->
   101     erlang:monitor(process, WPid),
   102     handle_cast({idle, WPid}, State);
   103 
   104 handle_cast({idle, WPid}, State = #state { available = Avail,
   105                                            pending   = Pending }) ->
   106     {noreply,
   107      case queue:out(Pending) of
   108          {empty, _Pending} ->
   109              State #state { available = ordsets:add_element(WPid, Avail) };
   110          {{value, {next_free, From, CPid}}, Pending1} ->
   111              worker_pool_worker:next_job_from(WPid, CPid),
   112              gen_server2:reply(From, WPid),
   113              State #state { pending = Pending1 };
   114          {{value, {run_async, Fun}}, Pending1} ->
   115              worker_pool_worker:submit_async(WPid, Fun),
   116              State #state { pending = Pending1 }
   117      end, hibernate};
   118 
   119 handle_cast({run_async, Fun}, State = #state { available = [],
   120                                                pending   = Pending }) ->
   121     {noreply, State #state { pending = queue:in({run_async, Fun}, Pending)},
   122      hibernate};
   123 handle_cast({run_async, Fun}, State = #state { available = [WPid | Avail1] }) ->
   124     worker_pool_worker:submit_async(WPid, Fun),
   125     {noreply, State #state { available = Avail1 }, hibernate};
   126 
   127 handle_cast(Msg, State) ->
   128     {stop, {unexpected_cast, Msg}, State}.
   129 
   130 handle_info({'DOWN', _MRef, process, WPid, _Reason},
   131             State = #state { available = Avail }) ->
   132     {noreply, State #state { available = ordsets:del_element(WPid, Avail) },
   133      hibernate};
   134 
   135 handle_info(Msg, State) ->
   136     {stop, {unexpected_info, Msg}, State}.
   137 
   138 code_change(_OldVsn, State, _Extra) ->
   139     {ok, State}.
   140 
   141 terminate(_Reason, State) ->
   142     State.