1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2007-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(tcp_listener_sup).
9
10%% Supervises TCP listeners. There is a separate supervisor for every
11%% protocol. In case of AMQP 0-9-1, it resides under rabbit_sup. Plugins
12%% that provide protocol support (e.g. STOMP) have an instance of this supervisor in their
13%% app supervision tree.
14%%
15%% See also rabbit_networking and tcp_listener.
16
17-behaviour(supervisor).
18
19-export([start_link/11]).
20-export([init/1]).
21
22-type mfargs() :: {atom(), atom(), [any()]}.
23
24-spec start_link
25        (inet:ip_address(), inet:port_number(), module(), [gen_tcp:listen_option()],
26         module(), any(), mfargs(), mfargs(), integer(), integer(), string()) ->
27                           rabbit_types:ok_pid_or_error().
28
29start_link(IPAddress, Port, Transport, SocketOpts, ProtoSup, ProtoOpts, OnStartup, OnShutdown,
30           ConcurrentAcceptorCount, ConcurrentConnsSups, Label) ->
31    supervisor:start_link(
32      ?MODULE, {IPAddress, Port, Transport, SocketOpts, ProtoSup, ProtoOpts, OnStartup, OnShutdown,
33                ConcurrentAcceptorCount, ConcurrentConnsSups, Label}).
34
35init({IPAddress, Port, Transport, SocketOpts, ProtoSup, ProtoOpts, OnStartup, OnShutdown,
36      ConcurrentAcceptorCount, ConcurrentConnsSups, Label}) ->
37    {ok, AckTimeout} = application:get_env(rabbit, ssl_handshake_timeout),
38    MaxConnections = max_conn(rabbit_misc:get_env(rabbit, connection_max, infinity),
39                              ConcurrentConnsSups),
40    RanchListenerOpts = #{
41      num_acceptors => ConcurrentAcceptorCount,
42      max_connections => MaxConnections,
43      handshake_timeout => AckTimeout,
44      connection_type => supervisor,
45      socket_opts => [{ip, IPAddress},
46                      {port, Port} |
47                      SocketOpts],
48      num_conns_sups => ConcurrentConnsSups
49     },
50    Flags = {one_for_all, 10, 10},
51    OurChildSpecStart = {tcp_listener, start_link, [IPAddress, Port, OnStartup, OnShutdown, Label]},
52    OurChildSpec = {tcp_listener, OurChildSpecStart, transient, 16#ffffffff, worker, [tcp_listener]},
53    RanchChildSpec = ranch:child_spec(rabbit_networking:ranch_ref(IPAddress, Port),
54        Transport, RanchListenerOpts,
55        ProtoSup, ProtoOpts),
56    {ok, {Flags, [RanchChildSpec, OurChildSpec]}}.
57
58max_conn(infinity, _) ->
59    infinity;
60max_conn(Max, Sups) ->
61  %% connection_max in Ranch is per connection supervisor
62  Max div Sups.
63