Skip to content

Commit 1402183

Browse files
Introduce a way to disable FHC use in web_mqtt
Via `web_mqtt.enable_file_handle_cache`
1 parent a6c522d commit 1402183

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

deps/rabbit_common/src/rabbit_types.erl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
-include("rabbit.hrl").
1111

12-
-export_type([maybe/1, info/0, infos/0, info_key/0, info_keys/0,
12+
-export_type([
13+
%% deprecated
14+
maybe/1,
15+
option/1,
16+
info/0, infos/0, info_key/0, info_keys/0,
1317
message/0, msg_id/0, basic_message/0,
1418
delivery/0, content/0, decoded_content/0, undecoded_content/0,
1519
unencoded_content/0, encoded_content/0, message_properties/0,
@@ -28,6 +32,8 @@
2832
permission_atom/0, rabbit_amqqueue_name/0, binding_key/0, channel_number/0,
2933
exchange_name/0, exchange_type/0, guid/0, routing_key/0]).
3034

35+
-type(option(T) :: T | 'none' | 'undefined').
36+
%% Deprecated, 'maybe' is a keyword in modern Erlang
3137
-type(maybe(T) :: T | 'none').
3238
-type(timestamp() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}).
3339

@@ -49,11 +55,11 @@
4955
-type(decoded_content() ::
5056
#content{class_id :: rabbit_framing:amqp_class_id(),
5157
properties :: rabbit_framing:amqp_property_record(),
52-
properties_bin :: maybe(binary()),
58+
properties_bin :: option(binary()),
5359
payload_fragments_rev :: [binary()]}).
5460
-type(encoded_content() ::
5561
#content{class_id :: rabbit_framing:amqp_class_id(),
56-
properties :: maybe(rabbit_framing:amqp_property_record()),
62+
properties :: option(rabbit_framing:amqp_property_record()),
5763
properties_bin :: binary(),
5864
payload_fragments_rev :: [binary()]}).
5965
-type(content() :: undecoded_content() | decoded_content()).

deps/rabbitmq_web_mqtt/priv/schema/rabbitmq_web_mqtt.schema

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,12 @@ end}.
181181

182182
{mapping, "web_mqtt.proxy_protocol", "rabbitmq_web_mqtt.proxy_protocol",
183183
[{datatype, {enum, [true, false]}}]}.
184+
185+
%%
186+
%% File Handle Cache
187+
%%
188+
189+
{mapping, "web_mqtt.enable_file_handle_cache", "rabbitmq_web_mqtt.enable_file_handle_cache",
190+
[
191+
{datatype, {enum, [true, false]}}
192+
]}.

deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
upgrade/5,
3030
takeover/7]).
3131

32+
-define(APP, rabbitmq_web_mqtt).
33+
3234
-record(state, {
3335
socket :: {rabbit_proxy_socket, any(), any()} | rabbit_net:socket(),
3436
parse_state = rabbit_mqtt_packet:init_state() :: rabbit_mqtt_packet:state(),
@@ -38,7 +40,8 @@
3840
conserve = false :: boolean(),
3941
stats_timer :: option(rabbit_event:state()),
4042
keepalive = rabbit_mqtt_keepalive:init() :: rabbit_mqtt_keepalive:state(),
41-
conn_name :: option(binary())
43+
conn_name :: option(binary()),
44+
should_use_fhc :: rabbit_types:option(boolean())
4245
}).
4346

4447
-type state() :: #state{}.
@@ -78,19 +81,31 @@ init(Req, Opts) ->
7881
false ->
7982
no_supported_sub_protocol(Protocol, Req);
8083
true ->
84+
ShouldUseFHC = application:get_env(?APP, enable_file_handle_cache, true),
85+
case ShouldUseFHC of
86+
true -> ?LOG_INFO("Web MQTT: file handle cache use is enabled");
87+
false -> ?LOG_INFO("Web MQTT: file handle cache use is disabled")
88+
end,
89+
8190
{?MODULE,
8291
cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, <<"mqtt">>, Req),
83-
#state{socket = maps:get(proxy_header, Req, undefined)},
92+
#state{socket = maps:get(proxy_header, Req, undefined), should_use_fhc = ShouldUseFHC},
8493
WsOpts}
8594
end
8695
end.
8796

8897
-spec websocket_init(state()) ->
8998
{cowboy_websocket:commands(), state()} |
9099
{cowboy_websocket:commands(), state(), hibernate}.
91-
websocket_init(State0 = #state{socket = Sock}) ->
100+
websocket_init(State0 = #state{socket = Sock, should_use_fhc = ShouldUseFHC}) ->
92101
logger:set_process_metadata(#{domain => ?RMQLOG_DOMAIN_CONN ++ [web_mqtt]}),
93-
ok = file_handle_cache:obtain(),
102+
case ShouldUseFHC of
103+
true ->
104+
ok = file_handle_cache:obtain();
105+
false -> ok;
106+
undefined ->
107+
ok = file_handle_cache:obtain()
108+
end,
94109
case rabbit_net:connection_string(Sock, inbound) of
95110
{ok, ConnStr} ->
96111
ConnName = rabbit_data_coercion:to_binary(ConnStr),

deps/rabbitmq_web_mqtt/test/config_schema_SUITE_data/rabbitmq_web_mqtt.snippets

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@
4848
[{max_connections, 5000}]}],
4949
[rabbitmq_web_mqtt]},
5050

51+
{enable_file_handle_cache_is_disabled,
52+
"web_mqtt.enable_file_handle_cache = false",
53+
[{rabbitmq_web_mqtt,
54+
[{enable_file_handle_cache, false}]}],
55+
[rabbitmq_web_mqtt]},
56+
57+
{enable_file_handle_cache_is_enabled,
58+
"web_mqtt.enable_file_handle_cache = true",
59+
[{rabbitmq_web_mqtt,
60+
[{enable_file_handle_cache, true}]}],
61+
[rabbitmq_web_mqtt]},
62+
5163
{ssl_listener,
5264
"web_mqtt.ssl.listener = 127.0.0.4:15672",
5365
[{rabbitmq_web_mqtt,

0 commit comments

Comments
 (0)