|
| 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) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_presence). |
| 9 | + |
| 10 | +-behaviour(gen_server). |
| 11 | + |
| 12 | +-export([list_present/0]). |
| 13 | +-export([start_link/0]). |
| 14 | + |
| 15 | +-export([init/1, |
| 16 | + handle_call/3, |
| 17 | + handle_cast/2, |
| 18 | + handle_info/2, |
| 19 | + terminate/2, |
| 20 | + code_change/3]). |
| 21 | + |
| 22 | +-define(SERVER, ?MODULE). |
| 23 | +-define(INTERVAL, 1000). |
| 24 | + |
| 25 | +-record(?MODULE, {tbl :: ets:table(), |
| 26 | + nodes = [] :: [node()]}). |
| 27 | + |
| 28 | +%%---------------------------------------------------------------------------- |
| 29 | +%% A presence server that heartbeats all configured servers with the goal of |
| 30 | +%% providing a very quickly accessible idea of node availability without |
| 31 | +%% having to use rabbit_nodes:all_running/1 which can block for a long time. |
| 32 | +%%---------------------------------------------------------------------------- |
| 33 | + |
| 34 | +-spec list_present() -> [node()]. |
| 35 | +list_present() -> |
| 36 | + case whereis(?MODULE) of |
| 37 | + undefined -> |
| 38 | + %% TODO: change return type to ok | error? |
| 39 | + exit(presence_server_not_running); |
| 40 | + _ -> |
| 41 | + Cutoff = erlang:system_time(millisecond) - 5000, |
| 42 | + [N || {N, SeenMs} <- ets:tab2list(?MODULE), |
| 43 | + %% if it hasn't been seen since the cutoff |
| 44 | + SeenMs > Cutoff, |
| 45 | + %% if not in nodes() it is also considered not present |
| 46 | + lists:member(N, nodes())] |
| 47 | + end. |
| 48 | + |
| 49 | +-spec start_link() -> rabbit_types:ok_pid_or_error(). |
| 50 | +start_link() -> |
| 51 | + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). |
| 52 | + |
| 53 | +init([]) -> |
| 54 | + process_flag(trap_exit, true), |
| 55 | + Ref = ets:new(?MODULE, [set, named_table, public]), |
| 56 | + Nodes = rabbit_nodes:list_members(), |
| 57 | + beat_all(Nodes), |
| 58 | + erlang:send_after(?INTERVAL, self(), beat), |
| 59 | + {ok, #?MODULE{tbl = Ref, |
| 60 | + nodes = Nodes}}. |
| 61 | + |
| 62 | +handle_call(_Request, _From, State) -> |
| 63 | + {noreply, State}. |
| 64 | + |
| 65 | +handle_cast(_Request, State) -> |
| 66 | + {noreply, State}. |
| 67 | + |
| 68 | +handle_info(beat, #?MODULE{tbl = _Tbl, |
| 69 | + nodes = Nodes} = State) -> |
| 70 | + _ = erlang:send_after(?INTERVAL, self(), beat), |
| 71 | + _ = beat_all(Nodes), |
| 72 | + {noreply, State}; |
| 73 | +handle_info({hb, Node}, #?MODULE{tbl = Tbl, |
| 74 | + nodes = _Nodes} = State) -> |
| 75 | + ets:insert(Tbl, {Node, erlang:system_time(millisecond)}), |
| 76 | + {noreply, State}; |
| 77 | +handle_info({terminate, Node}, #?MODULE{tbl = Tbl, |
| 78 | + nodes = _Nodes} = State) -> |
| 79 | + ets:delete(Tbl, Node), |
| 80 | + {noreply, State}; |
| 81 | +handle_info(_Msg, State) -> |
| 82 | + {noreply, State}. |
| 83 | + |
| 84 | +terminate(_Reason, #?MODULE{nodes = Nodes}) -> |
| 85 | + %% only send terminate if reason is `shutdown`? |
| 86 | + _ = send_terminate(Nodes), |
| 87 | + ok. |
| 88 | + |
| 89 | +code_change(_OldVsn, State, _Extra) -> |
| 90 | + {ok, State}. |
| 91 | + |
| 92 | + |
| 93 | +%% INTERNAL |
| 94 | + |
| 95 | +beat_all(Nodes) -> |
| 96 | + [send(N, {hb, node()}) || N <- Nodes, N =/= node()]. |
| 97 | + |
| 98 | +send_terminate(Nodes) -> |
| 99 | + [send(N, {terminate, node()}) || N <- Nodes, N =/= node()]. |
| 100 | + |
| 101 | +send(Node, Msg) -> |
| 102 | + erlang:send({?SERVER, Node}, Msg, [noconnect, nosuspend]). |
0 commit comments