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

Commit d0459e3

Browse files
committed
Purge defunct connections in more cases
Fixes #92 This PR will purge defunct connections in a few more cases than before.
1 parent 3f0f7d8 commit d0459e3

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/rabbit_auth_backend_ldap.erl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,7 @@ with_ldap({ok, Creds}, Fun, Servers) ->
468468
fun () ->
469469
case with_login(Creds, Servers, Opts, Fun) of
470470
{error, {gen_tcp_error, _}} ->
471-
%% purge and retry with a new connection
472-
rabbit_log:warning("TCP connection to a LDAP server was closed or otherwise defunct~n"),
473-
purge_conn(Creds == anon, Servers, Opts),
474-
rabbit_log:warning("LDAP will retry with a new connection~n"),
471+
purge_connection(Creds, Servers, Opts),
475472
with_login(Creds, Servers, Opts, Fun);
476473
Result -> Result
477474
end
@@ -480,7 +477,7 @@ with_ldap({ok, Creds}, Fun, Servers) ->
480477
with_login(Creds, Servers, Opts, Fun) ->
481478
with_login(Creds, Servers, Opts, Fun, ?LDAP_OPERATION_RETRIES).
482479
with_login(_Creds, _Servers, _Opts, _Fun, 0 = _RetriesLeft) ->
483-
rabbit_log:warning("LDAP failed to perform an operation. TCP connection to a LDAP server was closed or otherwise defunct. Exhausted all retries.~n"),
480+
rabbit_log:warning("LDAP failed to perform an operation. TCP connection to a LDAP server was closed or otherwise defunct. Exhausted all retries."),
484481
{error, ldap_connect_error};
485482
with_login(Creds, Servers, Opts, Fun, RetriesLeft) ->
486483
case get_or_create_conn(Creds == anon, Servers, Opts) of
@@ -489,9 +486,10 @@ with_login(Creds, Servers, Opts, Fun, RetriesLeft) ->
489486
anon ->
490487
?L1("anonymous bind", []),
491488
case call_ldap_fun(Fun, LDAP) of
492-
{error, ldap_closed} ->
493-
with_login(Creds, Servers, Opts, Fun, RetriesLeft - 1);
494-
Other -> Other
489+
{error, ldap_closed} ->
490+
purge_connection(Creds, Servers, Opts),
491+
with_login(Creds, Servers, Opts, Fun, RetriesLeft - 1);
492+
Other -> Other
495493
end;
496494
{UserDN, Password} ->
497495
case eldap:simple_bind(LDAP, UserDN, Password) of
@@ -510,11 +508,13 @@ with_login(Creds, Servers, Opts, Fun, RetriesLeft) ->
510508
[scrub_dn(UserDN, env(log))]),
511509
{refused, UserDN, []};
512510
{error, ldap_closed} ->
511+
purge_connection(Creds, Servers, Opts),
513512
with_login(Creds, Servers, Opts, Fun, RetriesLeft - 1);
514513
{error, {gen_tcp_error, _}} ->
514+
purge_connection(Creds, Servers, Opts),
515515
with_login(Creds, Servers, Opts, Fun, RetriesLeft - 1);
516516
{error, E} ->
517-
?L1("bind error: ~s ~p",
517+
?L1("bind error: ~p ~p",
518518
[scrub_dn(UserDN, env(log)), E]),
519519
%% Do not report internal bind error to a client
520520
{error, ldap_bind_error}
@@ -525,10 +525,16 @@ with_login(Creds, Servers, Opts, Fun, RetriesLeft) ->
525525
case Error of
526526
{error, {gen_tcp_error, _}} -> Error;
527527
%% Do not report internal connection error to a client
528-
_Other -> {error, ldap_connect_error}
528+
_Other -> {error, ldap_connect_error}
529529
end
530530
end.
531531

532+
purge_connection(Creds, Servers, Opts) ->
533+
%% purge and retry with a new connection
534+
rabbit_log:warning("TCP connection to a LDAP server was closed or otherwise defunct."),
535+
purge_conn(Creds == anon, Servers, Opts),
536+
rabbit_log:warning("LDAP will retry with a new connection.").
537+
532538
call_ldap_fun(Fun, LDAP) ->
533539
call_ldap_fun(Fun, LDAP, "").
534540

@@ -617,7 +623,7 @@ purge_conn(IsAnon, Servers, Opts) ->
617623
Conns = get(ldap_conns),
618624
Key = {IsAnon, Servers, Opts},
619625
{ok, Conn} = maps:find(Key, Conns),
620-
rabbit_log:warning("LDAP Purging an already closed LDAP server connection~n"),
626+
rabbit_log:warning("LDAP Purging an already closed LDAP server connection"),
621627
% We cannot close the connection with eldap:close/1 because as of OTP-13327
622628
% eldap will try to do_unbind first and will fail with a `{gen_tcp_error, closed}`.
623629
% Since we know that the connection is already closed, we just

0 commit comments

Comments
 (0)