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 |
1 |
?LOG_DEBUG("Starting acceptor supervisor"), |
32 |
1 |
supervisor:start_link({local, ?SERVER}, ?MODULE, []). |
33 |
|
|
34 |
|
-spec init([]) -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}. |
35 |
|
init([]) -> |
36 |
1 |
?LOG_DEBUG("Initializing acceptor supervisor"), |
37 |
1 |
{ok, Port} = application:get_env(port), |
38 |
1 |
{ok, PoolSize} = application:get_env(pool_size), |
39 |
1 |
Listener = start_listener(Port), |
40 |
1 |
SupFlags = #{}, |
41 |
1 |
ChildSpecs = |
42 |
|
lists:map( |
43 |
|
fun(N) -> |
44 |
5 |
#{id => {textgroup_acceptor, N}, |
45 |
|
start => {textgroup_acceptor, start_link, [Listener]}} |
46 |
|
end, lists:seq(1, PoolSize)), |
47 |
1 |
{ok, {SupFlags, ChildSpecs}}. |
48 |
|
|
49 |
|
%% Internal functions. |
50 |
|
|
51 |
|
-spec start_listener(inet:port_number()) -> inet:socket(). |
52 |
|
start_listener(Port) -> |
53 |
1 |
?LOG_DEBUG("Starting listener on port ~B", [Port]), |
54 |
1 |
{ok, Listener} = gen_tcp:listen(Port, [binary, |
55 |
|
{reuseaddr, true}, |
56 |
|
{nodelay, true}, |
57 |
|
{active, false}, |
58 |
|
{packet, line}, |
59 |
|
{send_timeout, ?SEND_TIMEOUT}, |
60 |
|
{send_timeout_close, true}]), |
61 |
1 |
Listener. |