Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 8833755

Browse files
Merge pull request #278 from rabbitmq/lrb-update-heartbeat-sock-stats-after-sending-frame
Update socket stats after running handler
2 parents 0884700 + b76227e commit 8833755

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

src/rabbit_heartbeat.erl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ heartbeater(Params, Identity) ->
125125
end)}.
126126

127127
heartbeater({Sock, TimeoutMillisec, StatName, Threshold, Handler} = Params,
128-
Deb, {StatVal, SameCount} = State) ->
128+
Deb, {StatVal0, SameCount} = State) ->
129129
Recurse = fun (State1) -> heartbeater(Params, Deb, State1) end,
130130
System = fun (From, Req) ->
131131
sys:handle_system_msg(
@@ -143,23 +143,42 @@ heartbeater({Sock, TimeoutMillisec, StatName, Threshold, Handler} = Params,
143143
Other ->
144144
exit({unexpected_message, Other})
145145
after TimeoutMillisec ->
146-
case rabbit_net:getstat(Sock, [StatName]) of
147-
{ok, [{StatName, NewStatVal}]} ->
148-
if NewStatVal =/= StatVal ->
149-
Recurse({NewStatVal, 0});
150-
SameCount < Threshold ->
151-
Recurse({NewStatVal, SameCount + 1});
152-
true ->
153-
case Handler() of
154-
stop -> ok;
155-
continue -> Recurse({NewStatVal, 0})
156-
end
157-
end;
158-
{error, einval} ->
159-
%% the socket is dead, most likely because the
160-
%% connection is being shut down -> terminate
161-
ok;
162-
{error, Reason} ->
163-
exit({cannot_get_socket_stats, Reason})
164-
end
146+
OkFun = fun(StatVal1) ->
147+
if StatVal1 =/= StatVal0 ->
148+
{recurse, {StatVal1, 0}};
149+
SameCount < Threshold ->
150+
{recurse, {StatVal1, SameCount +1}};
151+
true ->
152+
{run_handler, {StatVal1, 0}}
153+
end
154+
end,
155+
SSResult = get_sock_stats(Sock, StatName, OkFun),
156+
handle_get_sock_stats(SSResult, Sock, StatName, Recurse, Handler)
157+
end.
158+
159+
handle_get_sock_stats(stop, _Sock, _StatName, _Recurse, _Handler) ->
160+
ok;
161+
handle_get_sock_stats({recurse, RecurseArg}, _Sock, _StatName, Recurse, _Handler) ->
162+
Recurse(RecurseArg);
163+
handle_get_sock_stats({run_handler, {_, SameCount}}, Sock, StatName, Recurse, Handler) ->
164+
case Handler() of
165+
stop -> ok;
166+
continue ->
167+
OkFun = fun(StatVal) ->
168+
{recurse, {StatVal, SameCount}}
169+
end,
170+
SSResult = get_sock_stats(Sock, StatName, OkFun),
171+
handle_get_sock_stats(SSResult, Sock, StatName, Recurse, Handler)
172+
end.
173+
174+
get_sock_stats(Sock, StatName, OkFun) ->
175+
case rabbit_net:getstat(Sock, [StatName]) of
176+
{ok, [{StatName, StatVal}]} ->
177+
OkFun(StatVal);
178+
{error, einval} ->
179+
%% the socket is dead, most likely because the
180+
%% connection is being shut down -> terminate
181+
stop;
182+
{error, Reason} ->
183+
exit({cannot_get_socket_stats, Reason})
165184
end.

0 commit comments

Comments
 (0)