9
9
-behaviour (cowboy_websocket ).
10
10
-behaviour (cowboy_sub_protocol ).
11
11
12
+ -include_lib (" kernel/include/logger.hrl" ).
13
+ -include_lib (" rabbit_common/include/logging.hrl" ).
12
14
-include_lib (" rabbitmq_stomp/include/rabbit_stomp.hrl" ).
13
15
-include_lib (" rabbitmq_stomp/include/rabbit_stomp_frame.hrl" ).
14
16
-include_lib (" amqp_client/include/amqp_client.hrl" ).
41
43
peername ,
42
44
auth_hd ,
43
45
stats_timer ,
44
- connection
46
+ connection ,
47
+ should_use_fhc :: rabbit_types :option (boolean ())
45
48
}).
46
49
50
+ -define (APP , rabbitmq_web_stomp ).
51
+
47
52
% % cowboy_sub_protcol
48
53
upgrade (Req , Env , Handler , HandlerState ) ->
49
54
upgrade (Req , Env , Handler , HandlerState , #{}).
@@ -80,6 +85,11 @@ init(Req0, Opts) ->
80
85
end ,
81
86
WsOpts0 = proplists :get_value (ws_opts , Opts , #{}),
82
87
WsOpts = maps :merge (#{compress => true }, WsOpts0 ),
88
+ ShouldUseFHC = application :get_env (? APP , enable_file_handle_cache , true ),
89
+ case ShouldUseFHC of
90
+ true -> ? LOG_INFO (" Web STOMP: file handle cache use is enabled" );
91
+ false -> ? LOG_INFO (" Web STOMP: file handle cache use is disabled" )
92
+ end ,
83
93
{? MODULE , Req , # state {
84
94
frame_type = proplists :get_value (type , Opts , text ),
85
95
heartbeat_sup = KeepaliveSup ,
@@ -89,11 +99,18 @@ init(Req0, Opts) ->
89
99
conserve_resources = false ,
90
100
socket = SockInfo ,
91
101
peername = PeerAddr ,
92
- auth_hd = cowboy_req :header (<<" authorization" >>, Req )
102
+ auth_hd = cowboy_req :header (<<" authorization" >>, Req ),
103
+ should_use_fhc = ShouldUseFHC
93
104
}, WsOpts }.
94
105
95
- websocket_init (State ) ->
96
- ok = file_handle_cache :obtain (),
106
+ websocket_init (State = # state {should_use_fhc = ShouldUseFHC }) ->
107
+ case ShouldUseFHC of
108
+ true ->
109
+ ok = file_handle_cache :obtain ();
110
+ false -> ok ;
111
+ undefined ->
112
+ ok = file_handle_cache :obtain ()
113
+ end ,
97
114
process_flag (trap_exit , true ),
98
115
{ok , ProcessorState } = init_processor_state (State ),
99
116
{ok , rabbit_event :init_stats_timer (
@@ -314,9 +331,15 @@ maybe_block(State, _) ->
314
331
stop (State ) ->
315
332
stop (State , 1000 , " STOMP died" ).
316
333
317
- stop (State = # state {proc_state = ProcState }, CloseCode , Error0 ) ->
334
+ stop (State = # state {proc_state = ProcState , should_use_fhc = ShouldUseFHC }, CloseCode , Error0 ) ->
318
335
maybe_emit_stats (State ),
319
- ok = file_handle_cache :release (),
336
+ case ShouldUseFHC of
337
+ true ->
338
+ ok = file_handle_cache :release ();
339
+ false -> ok ;
340
+ undefined ->
341
+ ok = file_handle_cache :release ()
342
+ end ,
320
343
_ = rabbit_stomp_processor :flush_and_die (ProcState ),
321
344
Error1 = rabbit_data_coercion :to_binary (Error0 ),
322
345
{[{close , CloseCode , Error1 }], State }.
0 commit comments