forked from rabbitmq/rabbitmq-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbit_channel_sup.erl
More file actions
135 lines (119 loc) · 4.84 KB
/
Copy pathrabbit_channel_sup.erl
File metadata and controls
135 lines (119 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
-module(rabbit_channel_sup).
%% Supervises processes that implement AMQP 0-9-1 channels:
%%
%% * Channel process itself
%% * Network writer (for network connections)
%% * Limiter (handles channel QoS and flow control)
%%
%% Every rabbit_channel_sup is supervised by rabbit_channel_sup_sup.
%%
%% See also rabbit_channel, rabbit_writer, rabbit_limiter.
-behaviour(supervisor).
-export([start_link/1]).
-export([init/1]).
-include_lib("rabbit_common/include/rabbit.hrl").
%%----------------------------------------------------------------------------
-export_type([start_link_args/0]).
-type start_link_args() ::
{'tcp', rabbit_net:socket(), rabbit_channel:channel_number(),
non_neg_integer(), pid(), string(),
rabbit_types:user(), rabbit_types:vhost(), rabbit_framing:amqp_table(),
pid()} |
{'direct', rabbit_channel:channel_number(), pid(), string(),
rabbit_types:user(), rabbit_types:vhost(),
rabbit_framing:amqp_table(), pid()}.
-define(FAIR_WAIT, 70000).
%%----------------------------------------------------------------------------
-spec start_link(start_link_args()) ->
{'ok', pid(), {pid(), any()}} | {'error', any()}.
start_link({tcp, Sock, Channel, FrameMax, ReaderPid, ConnName, User,
VHost, Capabilities, Collector}) ->
{ok, SupPid} = supervisor:start_link(
?MODULE, {tcp, Sock, Channel, FrameMax,
ReaderPid, {ConnName, Channel}}),
[LimiterPid] = rabbit_misc:find_child(SupPid, limiter),
[WriterPid] = rabbit_misc:find_child(SupPid, writer),
StartMFA = {rabbit_channel, start_link,
[Channel, ReaderPid, WriterPid, ReaderPid, ConnName,
User, VHost, Capabilities, Collector,
LimiterPid]},
ChildSpec = #{id => channel,
start => StartMFA,
restart => transient,
significant => true,
shutdown => ?FAIR_WAIT,
type => worker,
modules => [rabbit_channel]},
case supervisor:start_child(SupPid, ChildSpec) of
{ok, ChannelPid} ->
{ok, AState} = rabbit_command_assembler:init(),
{ok, SupPid, {ChannelPid, AState}};
{error, Reason} ->
rabbit_misc:shutdown_supervisor(SupPid),
{error, Reason}
end;
start_link({direct, Channel, ClientChannelPid, ConnPid, ConnName,
User, VHost, Capabilities, Collector, AmqpParams}) ->
{ok, SupPid} = supervisor:start_link(
?MODULE, {direct, {ConnName, Channel}}),
[LimiterPid] = rabbit_misc:find_child(SupPid, limiter),
StartMFA = {rabbit_channel, start_link,
[Channel, ClientChannelPid, ClientChannelPid, ConnPid,
ConnName, User, VHost, Capabilities, Collector,
LimiterPid, AmqpParams]},
ChildSpec = #{id => channel,
start => StartMFA,
restart => transient,
significant => true,
shutdown => ?FAIR_WAIT,
type => worker,
modules => [rabbit_channel]},
case supervisor:start_child(SupPid, ChildSpec) of
{ok, ChannelPid} ->
{ok, SupPid, {ChannelPid, none}};
{error, Reason} ->
rabbit_misc:shutdown_supervisor(SupPid),
{error, Reason}
end.
%%----------------------------------------------------------------------------
init(Type) ->
?LG_PROCESS_TYPE(channel_sup),
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1,
auto_shutdown => any_significant},
{ok, {SupFlags, child_specs(Type)}}.
child_specs({tcp, Sock, Channel, FrameMax, ReaderPid, Identity}) ->
StartMFA = {rabbit_writer, start_link,
[Sock, Channel, FrameMax, ReaderPid, Identity, true]},
[
#{
id => writer,
start => StartMFA,
restart => transient,
significant => true,
shutdown => ?FAIR_WAIT,
type => worker,
modules => [rabbit_writer]
}
| child_specs({direct, Identity})
];
child_specs({direct, Identity}) ->
StartMFA = {rabbit_limiter, start_link, [Identity]},
[
#{
id => limiter,
start => StartMFA,
restart => transient,
significant => true,
shutdown => ?FAIR_WAIT,
type => worker,
modules => [rabbit_limiter]
}
].