src/worker_pool.erl
author Michael Klishin <michael@rabbitmq.com>
Fri, 01 Aug 2014 12:22:22 +0400
changeset 13767 df261d1d7982
parent 13330 989f9da88bb5
permissions -rw-r--r--
merge stable into default
     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_async/1, ready/1, idle/1]).
    32 
    33 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
    34          terminate/2, code_change/3]).
    35 
    36 %%----------------------------------------------------------------------------
    37 
    38 -ifdef(use_specs).
    39 
    40 -type(mfargs() :: {atom(), atom(), [any()]}).
    41 
    42 -spec(start_link/0 :: () -> {'ok', pid()} | {'error', any()}).
    43 -spec(submit/1 :: (fun (() -> A) | mfargs()) -> A).
    44 -spec(submit_async/1 :: (fun (() -> any()) | mfargs()) -> 'ok').
    45 -spec(ready/1 :: (pid()) -> 'ok').
    46 -spec(idle/1 :: (pid()) -> 'ok').
    47 
    48 -endif.
    49 
    50 %%----------------------------------------------------------------------------
    51 
    52 -define(SERVER, ?MODULE).
    53 -define(HIBERNATE_AFTER_MIN, 1000).
    54 -define(DESIRED_HIBERNATE, 10000).
    55 
    56 -record(state, { available, pending }).
    57 
    58 %%----------------------------------------------------------------------------
    59 
    60 start_link() -> gen_server2:start_link({local, ?SERVER}, ?MODULE, [],
    61                                        [{timeout, infinity}]).
    62 
    63 submit(Fun) ->
    64     case get(worker_pool_worker) of
    65         true -> worker_pool_worker:run(Fun);
    66         _    -> Pid = gen_server2:call(?SERVER, {next_free, self()}, infinity),
    67                 worker_pool_worker:submit(Pid, Fun)
    68     end.
    69 
    70 submit_async(Fun) -> gen_server2:cast(?SERVER, {run_async, Fun}).
    71 
    72 ready(WPid) -> gen_server2:cast(?SERVER, {ready, WPid}).
    73 
    74 idle(WPid) -> gen_server2:cast(?SERVER, {idle, WPid}).
    75 
    76 %%----------------------------------------------------------------------------
    77 
    78 init([]) ->
    79     {ok, #state { pending = queue:new(), available = ordsets:new() }, hibernate,
    80      {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}.
    81 
    82 handle_call({next_free, CPid}, From, State = #state { available = [],
    83                                                       pending   = Pending }) ->
    84     {noreply, State#state{pending = queue:in({next_free, From, CPid}, Pending)},
    85      hibernate};
    86 handle_call({next_free, CPid}, _From, State = #state { available =
    87                                                            [WPid | Avail1] }) ->
    88     worker_pool_worker:next_job_from(WPid, CPid),
    89     {reply, WPid, State #state { available = Avail1 }, hibernate};
    90 
    91 handle_call(Msg, _From, State) ->
    92     {stop, {unexpected_call, Msg}, State}.
    93 
    94 handle_cast({ready, WPid}, State) ->
    95     erlang:monitor(process, WPid),
    96     handle_cast({idle, WPid}, State);
    97 
    98 handle_cast({idle, WPid}, State = #state { available = Avail,
    99                                            pending   = Pending }) ->
   100     {noreply,
   101      case queue:out(Pending) of
   102          {empty, _Pending} ->
   103              State #state { available = ordsets:add_element(WPid, Avail) };
   104          {{value, {next_free, From, CPid}}, Pending1} ->
   105              worker_pool_worker:next_job_from(WPid, CPid),
   106              gen_server2:reply(From, WPid),
   107              State #state { pending = Pending1 };
   108          {{value, {run_async, Fun}}, Pending1} ->
   109              worker_pool_worker:submit_async(WPid, Fun),
   110              State #state { pending = Pending1 }
   111      end, hibernate};
   112 
   113 handle_cast({run_async, Fun}, State = #state { available = [],
   114                                                pending   = Pending }) ->
   115     {noreply, State #state { pending = queue:in({run_async, Fun}, Pending)},
   116      hibernate};
   117 handle_cast({run_async, Fun}, State = #state { available = [WPid | Avail1] }) ->
   118     worker_pool_worker:submit_async(WPid, Fun),
   119     {noreply, State #state { available = Avail1 }, hibernate};
   120 
   121 handle_cast(Msg, State) ->
   122     {stop, {unexpected_cast, Msg}, State}.
   123 
   124 handle_info({'DOWN', _MRef, process, WPid, _Reason},
   125             State = #state { available = Avail }) ->
   126     {noreply, State #state { available = ordsets:del_element(WPid, Avail) },
   127      hibernate};
   128 
   129 handle_info(Msg, State) ->
   130     {stop, {unexpected_info, Msg}, State}.
   131 
   132 code_change(_OldVsn, State, _Extra) ->
   133     {ok, State}.
   134 
   135 terminate(_Reason, State) ->
   136     State.