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/
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.
11 %% The Original Code is RabbitMQ.
13 %% The Initial Developer of the Original Code is VMware, Inc.
14 %% Copyright (c) 2007-2013 VMware, Inc. All rights reserved.
17 -module(worker_pool_worker).
19 -behaviour(gen_server2).
21 -export([start_link/1, submit/2, submit_async/2, run/1]).
23 -export([set_maximum_since_use/2]).
25 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
26 terminate/2, code_change/3, prioritise_cast/3]).
28 %%----------------------------------------------------------------------------
32 -type(mfargs() :: {atom(), atom(), [any()]}).
34 -spec(start_link/1 :: (any()) -> {'ok', pid()} | {'error', any()}).
35 -spec(submit/2 :: (pid(), fun (() -> A) | mfargs()) -> A).
36 -spec(submit_async/2 :: (pid(), fun (() -> any()) | mfargs()) -> 'ok').
37 -spec(run/1 :: (fun (() -> A)) -> A; (mfargs()) -> any()).
38 -spec(set_maximum_since_use/2 :: (pid(), non_neg_integer()) -> 'ok').
42 %%----------------------------------------------------------------------------
44 -define(HIBERNATE_AFTER_MIN, 1000).
45 -define(DESIRED_HIBERNATE, 10000).
47 %%----------------------------------------------------------------------------
50 gen_server2:start_link(?MODULE, [WId], [{timeout, infinity}]).
53 gen_server2:call(Pid, {submit, Fun}, infinity).
55 submit_async(Pid, Fun) ->
56 gen_server2:cast(Pid, {submit_async, Fun}).
58 set_maximum_since_use(Pid, Age) ->
59 gen_server2:cast(Pid, {set_maximum_since_use, Age}).
66 %%----------------------------------------------------------------------------
69 ok = file_handle_cache:register_callback(?MODULE, set_maximum_since_use,
71 ok = worker_pool:idle(WId),
72 put(worker_pool_worker, true),
74 {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}.
76 prioritise_cast({set_maximum_since_use, _Age}, _Len, _State) -> 8;
77 prioritise_cast(_Msg, _Len, _State) -> 0.
79 handle_call({submit, Fun}, From, WId) ->
80 gen_server2:reply(From, run(Fun)),
81 ok = worker_pool:idle(WId),
82 {noreply, WId, hibernate};
84 handle_call(Msg, _From, State) ->
85 {stop, {unexpected_call, Msg}, State}.
87 handle_cast({submit_async, Fun}, WId) ->
89 ok = worker_pool:idle(WId),
90 {noreply, WId, hibernate};
92 handle_cast({set_maximum_since_use, Age}, WId) ->
93 ok = file_handle_cache:set_maximum_since_use(Age),
94 {noreply, WId, hibernate};
96 handle_cast(Msg, State) ->
97 {stop, {unexpected_cast, Msg}, State}.
99 handle_info(Msg, State) ->
100 {stop, {unexpected_info, Msg}, State}.
102 code_change(_OldVsn, State, _Extra) ->
105 terminate(_Reason, State) ->