/home/runner/work/textgroup/textgroup/_build/test/cover/aggregate/textgroup_acceptor_sup.html

1 %%% Textgroup server.
2 %%%
3 %%% Copyright (c) 2022, 2023 Holger Weiss <holger@zedat.fu-berlin.de>.
4 %%% All rights reserved.
5 %%%
6 %%% Licensed under the Apache License, Version 2.0 (the "License");
7 %%% you may not use this file except in compliance with the License.
8 %%% You may obtain a copy of the License at
9 %%%
10 %%% http://www.apache.org/licenses/LICENSE-2.0
11 %%%
12 %%% Unless required by applicable law or agreed to in writing, software
13 %%% distributed under the License is distributed on an "AS IS" BASIS,
14 %%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 %%% See the License for the specific language governing permissions and
16 %%% limitations under the License.
17
18 -module(textgroup_acceptor_sup).
19 -behaviour(supervisor).
20 -export([start_link/0,
21 init/1]).
22
23 -include_lib("kernel/include/logger.hrl").
24 -define(SERVER, ?MODULE).
25 -define(SEND_TIMEOUT, timer:seconds(15)).
26
27 %% API.
28
29 -spec start_link() -> {ok, pid()} | {error, term()}.
30 start_link() ->
31 2 ?LOG_DEBUG("Starting acceptor supervisor"),
32 2 supervisor:start_link({local, ?SERVER}, ?MODULE, []).
33
34 -spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
35 init([]) ->
36 2 ?LOG_DEBUG("Initializing acceptor supervisor"),
37 2 {ok, Port} = application:get_env(port),
38 2 {ok, PoolSize} = application:get_env(pool_size),
39 2 Listener = start_listener(Port),
40 2 SupFlags = #{},
41 2 ChildSpecs =
42 lists:map(
43 fun(N) ->
44 15 #{id => {textgroup_acceptor, N},
45 start => {textgroup_acceptor, start_link, [Listener]},
46 shutdown => brutal_kill}
47 end, lists:seq(1, PoolSize)),
48 2 {ok, {SupFlags, ChildSpecs}}.
49
50 %% Internal functions.
51
52 -spec start_listener(inet:port_number()) -> inet:socket().
53 start_listener(Port) ->
54 2 ?LOG_DEBUG("Starting listener on port ~B", [Port]),
55 2 {ok, Listener} = gen_tcp:listen(Port, [binary,
56 {reuseaddr, true},
57 {nodelay, true},
58 {active, false},
59 {packet, line},
60 {send_timeout, ?SEND_TIMEOUT},
61 {send_timeout_close, true}]),
62 2 Listener.
Line Hits Source