Skip to content

Commit

Permalink
Properly list Web-MQTT connections in CLI list_mqtt_connections
Browse files Browse the repository at this point in the history
Before this change only `:not_found` was printed for each connection.
  • Loading branch information
gomoripeti committed Oct 13, 2023
1 parent 203bdf4 commit f2ccdcc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion deps/rabbitmq_mqtt/src/rabbit_mqtt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ emit_connection_info(Items, Ref, AggregatorPid, Pids) ->
rabbit_control_misc:emitting_map_with_exit_handler(
AggregatorPid, Ref,
fun(Pid) ->
rabbit_mqtt_reader:info(Pid, Items)
case rabbit_mqtt_reader:info(Pid, Items) of
{error, not_found} ->
%% a Web-MQTT connection
rabbit_web_mqtt_handler:info(Pid, Items);
Result ->
Result
end
end, Pids).

-spec close_local_client_connections(atom()) -> {'ok', non_neg_integer()}.
Expand Down
21 changes: 20 additions & 1 deletion deps/rabbitmq_web_mqtt/src/rabbit_web_mqtt_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
terminate/3
]).

-export([conserve_resources/3]).
-export([conserve_resources/3,
info/2]).

%% cowboy_sub_protocol
-export([upgrade/4,
Expand Down Expand Up @@ -126,6 +127,20 @@ conserve_resources(Pid, _, {_, Conserve, _}) ->
Pid ! {conserve_resources, Conserve},
ok.

%% We cannot use a gen_server call, because the handler process is a
%% special cowboy_websocket process (not a gen_server) which assumes
%% all gen_server calls are supervisor calls, and does not pass on the
%% request to this callback module. (see cowboy_websocket:loop/3 and
%% cowboy_children:handle_supervisor_call/4) However using a generic
%% gen:call with a special label ?MODULE works fine.
-spec info(pid(), rabbit_types:info_keys()) ->
rabbit_types:infos().
info(Pid, all) ->
info(Pid, ?INFO_ITEMS);
info(Pid, Items) ->
{ok, Res} = gen:call(Pid, ?MODULE, {info, Items}),
Res.

-spec websocket_handle(ping | pong | {text | binary | ping | pong, binary()}, State) ->
{cowboy_websocket:commands(), State} |
{cowboy_websocket:commands(), State, hibernate}.
Expand Down Expand Up @@ -244,6 +259,10 @@ websocket_info(connection_created, State) ->
rabbit_core_metrics:connection_created(self(), Infos),
rabbit_event:notify(connection_created, Infos),
{[], State, hibernate};
websocket_info({?MODULE, From, {info, Items}}, State) ->
Infos = infos(Items, State),
gen:reply(From, Infos),
{[], State, hibernate};
websocket_info(Msg, State) ->
?LOG_WARNING("Web MQTT: unexpected message ~tp", [Msg]),
{[], State, hibernate}.
Expand Down

0 comments on commit f2ccdcc

Please sign in to comment.