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

Commit 4d821aa

Browse files
authored
Merge pull request #195 from rabbitmq/augment-closed-connection-log-entries
Augment closed connection log entries
2 parents 73f730c + 44eba79 commit 4d821aa

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/rabbit_reader.erl

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,20 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
387387
last_blocked_by = none,
388388
last_blocked_at = never}},
389389
try
390-
run({?MODULE, recvloop,
391-
[Deb, [], 0, switch_callback(rabbit_event:init_stats_timer(
392-
State, #v1.stats_timer),
393-
handshake, 8)]}),
394-
log(info, "closing AMQP connection ~p (~s)~n", [self(), dynamic_connection_name(Name)])
390+
case run({?MODULE, recvloop,
391+
[Deb, [], 0, switch_callback(rabbit_event:init_stats_timer(
392+
State, #v1.stats_timer),
393+
handshake, 8)]}) of
394+
%% connection was closed cleanly by the client
395+
#v1{connection = #connection{user = #user{username = Username},
396+
vhost = VHost}} ->
397+
log(info, "closing AMQP connection ~p (~s, vhost: '~s', user: '~s')~n",
398+
[self(), dynamic_connection_name(Name), VHost, Username]);
399+
%% just to be more defensive
400+
_ ->
401+
log(info, "closing AMQP connection ~p (~s)~n",
402+
[self(), dynamic_connection_name(Name)])
403+
end
395404
catch
396405
Ex ->
397406
log_connection_exception(dynamic_connection_name(Name), Ex)
@@ -413,6 +422,7 @@ start_connection(Parent, HelperSup, Deb, Sock) ->
413422
log_connection_exception(Name, Ex) ->
414423
Severity = case Ex of
415424
connection_closed_with_no_data_received -> debug;
425+
{connection_closed_abruptly, _} -> warning;
416426
connection_closed_abruptly -> warning;
417427
_ -> error
418428
end,
@@ -422,6 +432,17 @@ log_connection_exception(Severity, Name, {heartbeat_timeout, TimeoutSec}) ->
422432
%% Long line to avoid extra spaces and line breaks in log
423433
log(Severity, "closing AMQP connection ~p (~s):~nmissed heartbeats from client, timeout: ~ps~n",
424434
[self(), Name, TimeoutSec]);
435+
log_connection_exception(Severity, Name, {connection_closed_abruptly,
436+
#v1{connection = #connection{user = #user{username = Username},
437+
vhost = VHost}}}) ->
438+
log(Severity, "closing AMQP connection ~p (~s, vhost: '~s', user: '~s'):~nclient unexpectedly closed TCP connection~n",
439+
[self(), Name, VHost, Username]);
440+
%% when client abruptly closes connection before connection.open/authentication/authorization
441+
%% succeeded, don't log username and vhost as 'none'
442+
log_connection_exception(Severity, Name, {connection_closed_abruptly, _}) ->
443+
log(Severity, "closing AMQP connection ~p (~s):~nclient unexpectedly closed TCP connection~n",
444+
[self(), Name]);
445+
%% old exception structure
425446
log_connection_exception(Severity, Name, connection_closed_abruptly) ->
426447
log(Severity, "closing AMQP connection ~p (~s):~nclient unexpectedly closed TCP connection~n",
427448
[self(), Name]);
@@ -490,7 +511,7 @@ mainloop(Deb, Buf, BufLen, State = #v1{sock = Sock,
490511
recvloop(Deb, [Data | Buf], BufLen + size(Data),
491512
State#v1{pending_recv = false});
492513
closed when State#v1.connection_state =:= closed ->
493-
ok;
514+
State;
494515
closed when CS =:= pre_init andalso Buf =:= [] ->
495516
stop(tcp_healthcheck, State);
496517
closed ->
@@ -506,7 +527,7 @@ mainloop(Deb, Buf, BufLen, State = #v1{sock = Sock,
506527
?MODULE, Deb, {Buf, BufLen, State});
507528
{other, Other} ->
508529
case handle_other(Other, State) of
509-
stop -> ok;
530+
stop -> State;
510531
NewState -> recvloop(Deb, Buf, BufLen, NewState)
511532
end
512533
end.
@@ -519,7 +540,7 @@ stop(tcp_healthcheck, State) ->
519540
throw(connection_closed_with_no_data_received);
520541
stop(closed, State) ->
521542
maybe_emit_stats(State),
522-
throw(connection_closed_abruptly);
543+
throw({connection_closed_abruptly, State});
523544
stop(Reason, State) ->
524545
maybe_emit_stats(State),
525546
throw({inet_error, Reason}).
@@ -1129,7 +1150,7 @@ handle_method0(MethodName, FieldsBin,
11291150
State)
11301151
catch throw:{inet_error, E} when E =:= closed; E =:= enotconn ->
11311152
maybe_emit_stats(State),
1132-
throw(connection_closed_abruptly);
1153+
throw({connection_closed_abruptly, State});
11331154
exit:#amqp_error{method = none} = Reason ->
11341155
handle_exception(State, 0, Reason#amqp_error{method = MethodName});
11351156
Type:Reason ->

0 commit comments

Comments
 (0)