Skip to content
Snippets Groups Projects
Commit 3e38f41a authored by ale's avatar ale
Browse files

Reconnect on ldap NetworkError

Before, we weren't releasing the connection properly.
parent 31066b08
Branches
No related tags found
No related merge requests found
...@@ -10,16 +10,24 @@ import ( ...@@ -10,16 +10,24 @@ import (
"git.autistici.org/ai3/go-common/clientutil" "git.autistici.org/ai3/go-common/clientutil"
) )
// Treat all errors as potential network-level issues, except for a // Interface matched by net.Error.
// whitelist of LDAP protocol level errors that we know are benign. type hasTemporary interface {
Temporary() bool
}
// Treat network errors as temporary. Other errors are permanent by
// default.
func isTemporaryLDAPError(err error) bool { func isTemporaryLDAPError(err error) bool {
ldapErr, ok := err.(*ldap.Error) switch v := err.(type) {
if !ok { case *ldap.Error:
return true switch v.ResultCode {
} case ldap.ErrorNetwork:
switch ldapErr.ResultCode { return true
case ldap.ErrorNetwork: default:
return true return false
}
case hasTemporary:
return v.Temporary()
default: default:
return false return false
} }
...@@ -32,6 +40,7 @@ func (p *ConnectionPool) Search(ctx context.Context, searchRequest *ldap.SearchR ...@@ -32,6 +40,7 @@ func (p *ConnectionPool) Search(ctx context.Context, searchRequest *ldap.SearchR
err := clientutil.Retry(func() error { err := clientutil.Retry(func() error {
conn, err := p.Get(ctx) conn, err := p.Get(ctx)
if err != nil { if err != nil {
// Here conn is nil, so we don't need to Release it.
if isTemporaryLDAPError(err) { if isTemporaryLDAPError(err) {
return clientutil.TempError(err) return clientutil.TempError(err)
} }
...@@ -44,7 +53,7 @@ func (p *ConnectionPool) Search(ctx context.Context, searchRequest *ldap.SearchR ...@@ -44,7 +53,7 @@ func (p *ConnectionPool) Search(ctx context.Context, searchRequest *ldap.SearchR
result, err = conn.Search(searchRequest) result, err = conn.Search(searchRequest)
if err != nil && isTemporaryLDAPError(err) { if err != nil && isTemporaryLDAPError(err) {
p.Release(conn, nil) p.Release(conn, err)
return clientutil.TempError(err) return clientutil.TempError(err)
} }
p.Release(conn, err) p.Release(conn, err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment