From 5fcc47f77aa93bb9ee479cd320cdb9fcc9e0fd29 Mon Sep 17 00:00:00 2001 From: yo Date: Thu, 14 Jan 2021 10:20:11 +0100 Subject: [PATCH] BUGFIX : conLdap now global (was previously recreated at each request after a failure, exhausting LDAP server connections) --- mxrouter.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mxrouter.go b/mxrouter.go index befcb00..571c858 100644 --- a/mxrouter.go +++ b/mxrouter.go @@ -1,7 +1,6 @@ // mxrouter // Copyright (c) 2020 yo000 // -// TODO : REconnexion ldap lorsque necessaire // checkmx : postfix tcp_table server which returns second-level domain name of the MX of a domain // ex : @@ -27,11 +26,12 @@ import ( ) const ( - version = "0.9.4" + version = "0.9.5" ) var ( logstream *syslog.Writer + conLdap *ldap.Conn mutex sync.Mutex debug *bool listen *string @@ -43,20 +43,22 @@ var ( defDomain *string ) -func ldapSearch(conLdap *ldap.Conn, searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) { +func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) { mutex.Lock() result, err := conLdap.Search(searchReq) mutex.Unlock() // Let's just manage connection errors here if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") { logstream.Err("LDAP connection closed, retrying") + mutex.Lock() conLdap.Close() conLdap, err = connectLdap() + mutex.Unlock() if err != nil { return result, err } else { attempt = attempt + 1 - return ldapSearch(conLdap, searchReq, attempt) + return ldapSearch(searchReq, attempt) } } @@ -109,7 +111,7 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) { searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0, false, filter, []string{*ldapAttr}, []ldap.Control{}) - result, err := ldapSearch(conLdap, searchReq, 0) + result, err := ldapSearch(searchReq, 0) if err != nil { logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err)) s := strings.Replace(err.Error(), " ", "%20", -1) @@ -159,7 +161,9 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) { } func connectLdap() (*ldap.Conn, error) { - conLdap, err := ldap.DialURL(*ldapURL) + var err error + + conLdap, err = ldap.DialURL(*ldapURL) if err != nil { logstream.Err(fmt.Sprintln("Error dialing LDAP: ", err)) return conLdap, err