1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2018 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
-module(mongoose_wpool_type_sup). |
17 |
|
|
18 |
|
-behaviour(supervisor). |
19 |
|
|
20 |
|
%% API |
21 |
|
-export([start_link/1]). |
22 |
|
-export([name/1]). |
23 |
|
|
24 |
|
%% Supervisor callbacks |
25 |
|
-export([init/1]). |
26 |
|
|
27 |
|
-ignore_xref([start_link/1]). |
28 |
|
|
29 |
|
%%%=================================================================== |
30 |
|
%%% API functions |
31 |
|
%%%=================================================================== |
32 |
|
|
33 |
|
%%-------------------------------------------------------------------- |
34 |
|
%% @doc |
35 |
|
%% Starts the supervisor |
36 |
|
%% |
37 |
|
%% @end |
38 |
|
%%-------------------------------------------------------------------- |
39 |
|
-spec start_link(mongoose_wpool:pool_type()) -> |
40 |
|
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}. |
41 |
|
start_link(PoolType) -> |
42 |
187 |
supervisor:start_link({local, name(PoolType)}, ?MODULE, [PoolType]). |
43 |
|
|
44 |
|
-spec name(mongoose_wpool:pool_type()) -> mongoose_wpool:proc_name(). |
45 |
|
name(PoolType) -> |
46 |
750 |
list_to_atom("mongoose_wpool_" ++ atom_to_list(PoolType) ++ "_sup"). |
47 |
|
|
48 |
|
%%%=================================================================== |
49 |
|
%%% Supervisor callbacks |
50 |
|
%%%=================================================================== |
51 |
|
|
52 |
|
%%-------------------------------------------------------------------- |
53 |
|
%% @private |
54 |
|
%% @doc |
55 |
|
%% Whenever a supervisor is started using supervisor:start_link/[2, 3], |
56 |
|
%% this function is called by the new process to find out about |
57 |
|
%% restart strategy, maximum restart frequency and child |
58 |
|
%% specifications. |
59 |
|
%% |
60 |
|
%% @end |
61 |
|
%%-------------------------------------------------------------------- |
62 |
|
-spec init(Args :: term()) -> {ok, {#{strategy => one_for_one, intensity => 100, period => 5}, |
63 |
|
[#{id := mongoose_wpool:proc_name(), |
64 |
|
start := {mongoose_wpool_mgr, start_link, [mongoose_wpool:pool_type()]}, |
65 |
|
restart => transient, |
66 |
|
shutdown => brutal_kill, |
67 |
|
type => worker, |
68 |
|
modules => [module()]}]}}. |
69 |
|
init([PoolType]) -> |
70 |
187 |
SupFlags = #{strategy => one_for_one, |
71 |
|
intensity => 100, |
72 |
|
period => 5}, |
73 |
|
|
74 |
187 |
ChildSpec = #{id => mongoose_wpool_mgr:name(PoolType), |
75 |
|
start => {mongoose_wpool_mgr, start_link, [PoolType]}, |
76 |
|
restart => transient, |
77 |
|
shutdown => brutal_kill, |
78 |
|
type => worker, |
79 |
|
modules => [?MODULE]}, |
80 |
|
|
81 |
187 |
{ok, {SupFlags, [ChildSpec]}}. |
82 |
|
|
83 |
|
%%%=================================================================== |
84 |
|
%%% Internal functions |
85 |
|
%%%=================================================================== |