1%% This Source Code Form is subject to the terms of the Mozilla Public
2%% License, v. 2.0. If a copy of the MPL was not distributed with this
3%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4%%
5%% Copyright (c) 2020-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module(rabbit_channel_tracking_handler).
9
10%% This module keeps track of channel creation and termination events
11%% on its local node. Similar to the rabbit_connection_tracking_handler,
12%% the primary goal here is to decouple channel tracking from rabbit_reader
13%% and isolate channel tracking to its own process to avoid blocking connection
14%% creation events. Additionaly, creation events are also non-blocking in that
15%% they spawn a short-live process for updating the tracking tables in realtime.
16%%
17%% Events from other nodes are ignored.
18
19-behaviour(gen_event).
20
21-export([init/1, handle_call/2, handle_event/2, handle_info/2,
22         terminate/2, code_change/3]).
23
24-include_lib("rabbit_common/include/rabbit.hrl").
25
26-rabbit_boot_step({?MODULE,
27                   [{description, "channel tracking event handler"},
28                    {mfa,         {gen_event, add_handler,
29                                   [rabbit_event, ?MODULE, []]}},
30                    {cleanup,     {gen_event, delete_handler,
31                                   [rabbit_event, ?MODULE, []]}},
32                    {requires,    [channel_tracking]},
33                    {enables,     recovery}]}).
34
35%%
36%% API
37%%
38
39init([]) ->
40    {ok, []}.
41
42handle_event(#event{type = channel_created, props = Details}, State) ->
43    ok = rabbit_channel_tracking:update_tracked({channel_created, Details}),
44    {ok, State};
45handle_event(#event{type = channel_closed, props = Details}, State) ->
46    ok = rabbit_channel_tracking:update_tracked({channel_closed, Details}),
47    {ok, State};
48handle_event(#event{type = connection_closed, props = Details}, State) ->
49    ok = rabbit_channel_tracking:update_tracked({connection_closed, Details}),
50    {ok, State};
51handle_event(#event{type = user_deleted, props = Details}, State) ->
52    ok = rabbit_channel_tracking:update_tracked({user_deleted, Details}),
53    {ok, State};
54%% A node had been deleted from the cluster.
55handle_event(#event{type = node_deleted, props = Details}, State) ->
56    ok = rabbit_channel_tracking:update_tracked({node_deleted, Details}),
57    {ok, State};
58handle_event(_Event, State) ->
59    {ok, State}.
60
61handle_call(_Request, State) ->
62    {ok, not_understood, State}.
63
64handle_info(_Info, State) ->
65    {ok, State}.
66
67terminate(_Arg, _State) ->
68    ok.
69
70code_change(_OldVsn, State, _Extra) ->
71    {ok, State}.
72