BUGFIX : conLdap now global (was previously recreated at each request after a failure, exhausting LDAP server connections)
This commit is contained in:
+10
-6
@@ -1,7 +1,6 @@
|
||||
// mxrouter
|
||||
// Copyright (c) 2020 yo000 <johan@nosd.in>
|
||||
//
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user