Skip to content

Commit f879352

Browse files
committed
inet_tcp_proxy: Fix logging of "BLOCKED"/"allowed" events
We used to use the process dictionary to store the previous blocking state. However, when a distribution connection times out (because of the block), it is closed and the process exits, thus loosing the previous state. Now, we use a second ETS table to store the current state. This makes the logging accurate. [#146911969]
1 parent 1103bce commit f879352

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/inet_tcp_proxy.erl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
-export([start/3, reconnect/1, is_enabled/0, allow/1, block/1]).
2222

23-
-define(TABLE, ?MODULE).
23+
-define(NODES_TO_BLOCK, inet_tcp_proxy__nodes_to_block).
24+
-define(NODES_BLOCKED, inet_tcp_proxy__nodes_blocked).
2425

2526
%% This can't start_link because there's no supervision hierarchy we
2627
%% can easily fit it into (we need to survive all application
@@ -44,16 +45,16 @@ reconnect(Nodes) ->
4445
ok.
4546

4647
is_enabled() ->
47-
lists:member(?TABLE, ets:all()).
48+
lists:member(?NODES_TO_BLOCK, ets:all()).
4849

4950
allow(Node) ->
5051
error_logger:info_msg("(~s) Allowing distribution between ~s and ~s~n",
5152
[?MODULE, node(), Node]),
52-
ets:delete(?TABLE, Node).
53+
true = ets:delete(?NODES_TO_BLOCK, Node).
5354
block(Node) ->
5455
error_logger:info_msg("(~s) BLOCKING distribution between ~s and ~s~n",
5556
[?MODULE, node(), Node]),
56-
ets:insert(?TABLE, {Node, block}).
57+
true = ets:insert(?NODES_TO_BLOCK, {Node, block}).
5758

5859
%%----------------------------------------------------------------------------
5960

@@ -74,7 +75,8 @@ error_handler(Thunk) ->
7475
end.
7576

7677
go(Parent, Port, ProxyPort) ->
77-
ets:new(?TABLE, [public, named_table]),
78+
ets:new(?NODES_TO_BLOCK, [public, named_table]),
79+
ets:new(?NODES_BLOCKED, [public, named_table]),
7880
{ok, Sock} = gen_tcp:listen(ProxyPort, [inet,
7981
{reuseaddr, true}]),
8082
Parent ! ready,
@@ -101,18 +103,20 @@ run_it(SockIn, Port) ->
101103
end.
102104

103105
run_loop(Sockets, RemoteNode, Buf0) ->
104-
Block = [{RemoteNode, block}] =:= ets:lookup(?TABLE, RemoteNode),
106+
Block = [{RemoteNode, block}] =:= ets:lookup(?NODES_TO_BLOCK, RemoteNode),
107+
WasBlocked = [{RemoteNode, blocked}] =:= ets:lookup(?NODES_BLOCKED,
108+
RemoteNode),
105109
receive
106110
{tcp, Sock, Data} ->
107111
Buf = [Data | Buf0],
108-
case {Block, get(dist_was_blocked)} of
109-
{true, false} ->
110-
put(dist_was_blocked, Block),
112+
case {WasBlocked, Block} of
113+
{false, true} ->
114+
true = ets:insert(?NODES_BLOCKED, {RemoteNode, blocked}),
111115
rabbit_log:warning(
112116
"(~s) Distribution BLOCKED between ~s and ~s~n",
113117
[?MODULE, node(), RemoteNode]);
114-
{false, S} when S =:= true orelse S =:= undefined ->
115-
put(dist_was_blocked, Block),
118+
{true, false} ->
119+
true = ets:delete(?NODES_BLOCKED, RemoteNode),
116120
rabbit_log:warning(
117121
"(~s) Distribution allowed between ~s and ~s~n",
118122
[?MODULE, node(), RemoteNode]);

0 commit comments

Comments
 (0)