|
matthew@6072
|
1 |
%% The contents of this file are subject to the Mozilla Public License
|
|
matthew@6072
|
2 |
%% Version 1.1 (the "License"); you may not use this file except in
|
|
matthew@6072
|
3 |
%% compliance with the License. You may obtain a copy of the License
|
|
matthew@6072
|
4 |
%% at http://www.mozilla.org/MPL/
|
|
matthew@2926
|
5 |
%%
|
|
matthew@6072
|
6 |
%% Software distributed under the License is distributed on an "AS IS"
|
|
matthew@6072
|
7 |
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
matthew@6072
|
8 |
%% the License for the specific language governing rights and
|
|
matthew@6072
|
9 |
%% limitations under the License.
|
|
matthew@2926
|
10 |
%%
|
|
matthew@6072
|
11 |
%% The Original Code is RabbitMQ.
|
|
matthew@2926
|
12 |
%%
|
|
matthew@6072
|
13 |
%% The Initial Developer of the Original Code is VMware, Inc.
|
|
emile@8906
|
14 |
%% Copyright (c) 2007-2012 VMware, Inc. All rights reserved.
|
|
matthew@2926
|
15 |
%%
|
|
matthew@2926
|
16 |
|
|
matthew@2926
|
17 |
-module(worker_pool_worker).
|
|
matthew@2926
|
18 |
|
|
matthew@2926
|
19 |
-behaviour(gen_server2).
|
|
matthew@2926
|
20 |
|
|
matthias@3008
|
21 |
-export([start_link/1, submit/2, submit_async/2, run/1]).
|
|
matthew@2926
|
22 |
|
|
matthias@3268
|
23 |
-export([set_maximum_since_use/2]).
|
|
matthias@3268
|
24 |
|
|
matthew@2926
|
25 |
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
alexandru@4598
|
26 |
terminate/2, code_change/3, prioritise_cast/2]).
|
|
matthew@2926
|
27 |
|
|
matthew@2926
|
28 |
%%----------------------------------------------------------------------------
|
|
matthew@2926
|
29 |
|
|
matthew@2926
|
30 |
-ifdef(use_specs).
|
|
matthew@2926
|
31 |
|
|
matthias@8788
|
32 |
-type(mfargs() :: {atom(), atom(), [any()]}).
|
|
matthias@8788
|
33 |
|
|
matthias@4392
|
34 |
-spec(start_link/1 :: (any()) -> {'ok', pid()} | {'error', any()}).
|
|
matthias@8788
|
35 |
-spec(submit/2 :: (pid(), fun (() -> A) | mfargs()) -> A).
|
|
matthias@8788
|
36 |
-spec(submit_async/2 :: (pid(), fun (() -> any()) | mfargs()) -> 'ok').
|
|
matthias@8788
|
37 |
-spec(run/1 :: (fun (() -> A)) -> A; (mfargs()) -> any()).
|
|
matthias@3268
|
38 |
-spec(set_maximum_since_use/2 :: (pid(), non_neg_integer()) -> 'ok').
|
|
matthew@2926
|
39 |
|
|
matthew@2926
|
40 |
-endif.
|
|
matthew@2926
|
41 |
|
|
matthew@2926
|
42 |
%%----------------------------------------------------------------------------
|
|
matthew@2926
|
43 |
|
|
matthew@2926
|
44 |
-define(HIBERNATE_AFTER_MIN, 1000).
|
|
matthew@2926
|
45 |
-define(DESIRED_HIBERNATE, 10000).
|
|
matthew@2926
|
46 |
|
|
matthew@2926
|
47 |
%%----------------------------------------------------------------------------
|
|
matthew@2926
|
48 |
|
|
matthew@2926
|
49 |
start_link(WId) ->
|
|
matthew@2926
|
50 |
gen_server2:start_link(?MODULE, [WId], [{timeout, infinity}]).
|
|
matthew@2926
|
51 |
|
|
matthew@2926
|
52 |
submit(Pid, Fun) ->
|
|
matthew@2926
|
53 |
gen_server2:call(Pid, {submit, Fun}, infinity).
|
|
matthew@2926
|
54 |
|
|
matthias@3008
|
55 |
submit_async(Pid, Fun) ->
|
|
matthias@3008
|
56 |
gen_server2:cast(Pid, {submit_async, Fun}).
|
|
matthias@3008
|
57 |
|
|
matthias@3268
|
58 |
set_maximum_since_use(Pid, Age) ->
|
|
alexandru@4598
|
59 |
gen_server2:cast(Pid, {set_maximum_since_use, Age}).
|
|
matthias@3268
|
60 |
|
|
matthew@3266
|
61 |
run({M, F, A}) ->
|
|
matthew@3266
|
62 |
apply(M, F, A);
|
|
matthew@3266
|
63 |
run(Fun) ->
|
|
matthew@3266
|
64 |
Fun().
|
|
matthew@3266
|
65 |
|
|
matthew@3266
|
66 |
%%----------------------------------------------------------------------------
|
|
matthew@3266
|
67 |
|
|
matthew@2926
|
68 |
init([WId]) ->
|
|
matthias@3268
|
69 |
ok = file_handle_cache:register_callback(?MODULE, set_maximum_since_use,
|
|
matthias@3268
|
70 |
[self()]),
|
|
matthew@2926
|
71 |
ok = worker_pool:idle(WId),
|
|
matthew@2926
|
72 |
put(worker_pool_worker, true),
|
|
matthew@2926
|
73 |
{ok, WId, hibernate,
|
|
matthew@2926
|
74 |
{backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}.
|
|
matthew@2926
|
75 |
|
|
matthew@4819
|
76 |
prioritise_cast({set_maximum_since_use, _Age}, _State) -> 8;
|
|
matthew@4819
|
77 |
prioritise_cast(_Msg, _State) -> 0.
|
|
alexandru@4598
|
78 |
|
|
matthew@2926
|
79 |
handle_call({submit, Fun}, From, WId) ->
|
|
matthew@2926
|
80 |
gen_server2:reply(From, run(Fun)),
|
|
matthew@2926
|
81 |
ok = worker_pool:idle(WId),
|
|
matthew@2961
|
82 |
{noreply, WId, hibernate};
|
|
matthew@2926
|
83 |
|
|
matthew@2926
|
84 |
handle_call(Msg, _From, State) ->
|
|
matthew@2926
|
85 |
{stop, {unexpected_call, Msg}, State}.
|
|
matthew@2926
|
86 |
|
|
matthias@3008
|
87 |
handle_cast({submit_async, Fun}, WId) ->
|
|
matthias@3008
|
88 |
run(Fun),
|
|
matthias@3008
|
89 |
ok = worker_pool:idle(WId),
|
|
matthias@3008
|
90 |
{noreply, WId, hibernate};
|
|
matthias@3008
|
91 |
|
|
matthias@3268
|
92 |
handle_cast({set_maximum_since_use, Age}, WId) ->
|
|
matthias@3268
|
93 |
ok = file_handle_cache:set_maximum_since_use(Age),
|
|
matthias@3268
|
94 |
{noreply, WId, hibernate};
|
|
matthias@3268
|
95 |
|
|
matthew@2926
|
96 |
handle_cast(Msg, State) ->
|
|
matthew@2926
|
97 |
{stop, {unexpected_cast, Msg}, State}.
|
|
matthew@2926
|
98 |
|
|
matthew@2926
|
99 |
handle_info(Msg, State) ->
|
|
matthew@2926
|
100 |
{stop, {unexpected_info, Msg}, State}.
|
|
matthew@2926
|
101 |
|
|
matthew@2926
|
102 |
code_change(_OldVsn, State, _Extra) ->
|
|
matthew@2926
|
103 |
{ok, State}.
|
|
matthew@2926
|
104 |
|
|
matthew@2926
|
105 |
terminate(_Reason, State) ->
|
|
matthew@2926
|
106 |
State.
|