Skip to content

Commit 7858e44

Browse files
committed
Move application environment read to the channel intialisation.
This can save us some reductions by avoiding ets read. [#161983593]
1 parent 03fc22d commit 7858e44

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

src/rabbit_channel.erl

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@
158158
delivery_flow,
159159
interceptor_state,
160160
queue_states,
161-
queue_cleanup_timer
161+
queue_cleanup_timer,
162+
%% Message content size limit
163+
max_message_size
162164
}).
163165

164166
-define(QUEUE, lqueue).
@@ -441,6 +443,12 @@ init([Channel, ReaderPid, WriterPid, ConnPid, ConnName, Protocol, User, VHost,
441443
_ ->
442444
Limiter0
443445
end,
446+
MaxMessageSize = case application:get_env(rabbit, max_message_size) of
447+
{ok, MS} when is_integer(MS) ->
448+
erlang:min(MS, ?MAX_MSG_SIZE);
449+
_ ->
450+
?MAX_MSG_SIZE
451+
end,
444452
State = #ch{state = starting,
445453
protocol = Protocol,
446454
channel = Channel,
@@ -473,7 +481,8 @@ init([Channel, ReaderPid, WriterPid, ConnPid, ConnName, Protocol, User, VHost,
473481
reply_consumer = none,
474482
delivery_flow = Flow,
475483
interceptor_state = undefined,
476-
queue_states = #{}},
484+
queue_states = #{},
485+
max_message_size = MaxMessageSize},
477486
State1 = State#ch{
478487
interceptor_state = rabbit_channel_interceptor:init(State)},
479488
State2 = rabbit_event:init_stats_timer(State1, #ch.stats_timer),
@@ -985,20 +994,19 @@ extract_topic_variable_map_from_amqp_params([{amqp_params, {amqp_params_direct,
985994
extract_topic_variable_map_from_amqp_params(_) ->
986995
#{}.
987996

988-
check_msg_size(Content) ->
997+
check_msg_size(Content, MaxMessageSize) ->
989998
Size = rabbit_basic:maybe_gc_large_msg(Content),
990-
case Size > ?MAX_MSG_SIZE of
991-
true -> precondition_failed("message size ~B larger than max size ~B",
992-
[Size, ?MAX_MSG_SIZE]);
993-
false ->
994-
case application:get_env(rabbit, max_message_size) of
995-
{ok, MaxSize} when is_integer(MaxSize) andalso Size > MaxSize ->
996-
precondition_failed("message size ~B larger than"
997-
" configured max size ~B",
998-
[Size, MaxSize]);
999-
1000-
_ -> ok
1001-
end
999+
case Size of
1000+
S when S > MaxMessageSize ->
1001+
ErrorMessage = case MaxMessageSize of
1002+
?MAX_MSG_SIZE ->
1003+
"message size ~B larger than max size ~B";
1004+
_ ->
1005+
"message size ~B larger than configured max size ~B"
1006+
end,
1007+
precondition_failed(ErrorMessage,
1008+
[Size, MaxMessageSize]);
1009+
_ -> ok
10021010
end.
10031011

10041012
check_vhost_queue_limit(#resource{name = QueueName}, VHost) ->
@@ -1172,16 +1180,17 @@ handle_method(#'basic.publish'{immediate = true}, _Content, _State) ->
11721180
handle_method(#'basic.publish'{exchange = ExchangeNameBin,
11731181
routing_key = RoutingKey,
11741182
mandatory = Mandatory},
1175-
Content, State = #ch{virtual_host = VHostPath,
1176-
tx = Tx,
1177-
channel = ChannelNum,
1178-
confirm_enabled = ConfirmEnabled,
1179-
trace_state = TraceState,
1180-
user = #user{username = Username} = User,
1181-
conn_name = ConnName,
1182-
delivery_flow = Flow,
1183-
conn_pid = ConnPid}) ->
1184-
check_msg_size(Content),
1183+
Content, State = #ch{virtual_host = VHostPath,
1184+
tx = Tx,
1185+
channel = ChannelNum,
1186+
confirm_enabled = ConfirmEnabled,
1187+
trace_state = TraceState,
1188+
user = #user{username = Username} = User,
1189+
conn_name = ConnName,
1190+
delivery_flow = Flow,
1191+
conn_pid = ConnPid,
1192+
max_message_size = MaxMessageSize}) ->
1193+
check_msg_size(Content, MaxMessageSize),
11851194
ExchangeName = rabbit_misc:r(VHostPath, exchange, ExchangeNameBin),
11861195
check_write_permitted(ExchangeName, User),
11871196
Exchange = rabbit_exchange:lookup_or_die(ExchangeName),

test/unit_inbroker_parallel_SUITE.erl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,13 +1343,12 @@ max_message_size(Config) ->
13431343
(integer_to_binary(Size128Mb))/binary>>,
13441344
assert_channel_fail_max_size(Ch, Monitor, ExpectedException),
13451345

1346-
1347-
{_, Ch1} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
1348-
13491346
%% Set a bigger message size
13501347
rabbit_ct_broker_helpers:rpc(Config, 0,
13511348
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 256]),
13521349

1350+
{_, Ch1} = rabbit_ct_client_helpers:open_connection_and_channel(Config, 0),
1351+
13531352
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary128M}),
13541353
assert_channel_alive(Ch1),
13551354

@@ -1361,22 +1360,26 @@ max_message_size(Config) ->
13611360
rabbit_ct_broker_helpers:rpc(Config, 0,
13621361
application, set_env, [rabbit, max_message_size, 1024 * 1024 * 515]),
13631362

1363+
%% Need a new channel for changes to take effect
1364+
rabbit_ct_client_helpers:close_channel(Ch1),
1365+
Ch2 = rabbit_ct_client_helpers:open_channel(Config),
1366+
13641367
Binary512M = << Binary128M/binary, Binary128M/binary,
13651368
Binary128M/binary, Binary128M/binary>>,
13661369

13671370
BinaryBiggerThan512M = <<"_", Binary512M/binary>>,
13681371

1369-
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary512M}),
1370-
assert_channel_alive(Ch1),
1372+
amqp_channel:call(Ch2, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = Binary512M}),
1373+
assert_channel_alive(Ch2),
13711374

1372-
Monitor1 = monitor(process, Ch1),
1373-
amqp_channel:call(Ch1, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = BinaryBiggerThan512M}),
1375+
Monitor2 = monitor(process, Ch2),
1376+
amqp_channel:call(Ch2, #'basic.publish'{routing_key = <<"nope">>}, #amqp_msg{payload = BinaryBiggerThan512M}),
13741377
ct:pal("Assert channel error 512"),
13751378
ExpectedException1 = <<"PRECONDITION_FAILED - message size ",
13761379
(integer_to_binary(byte_size(BinaryBiggerThan512M)))/binary,
13771380
" larger than max size ",
13781381
(integer_to_binary(byte_size(Binary512M)))/binary>>,
1379-
assert_channel_fail_max_size(Ch1, Monitor1, ExpectedException1).
1382+
assert_channel_fail_max_size(Ch2, Monitor2, ExpectedException1).
13801383

13811384
%% ---------------------------------------------------------------------------
13821385
%% rabbitmqctl helpers.

0 commit comments

Comments
 (0)