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
|
// mxrouter
|
||||||
// Copyright (c) 2020 yo000 <johan@nosd.in>
|
// 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
|
// checkmx : postfix tcp_table server which returns second-level domain name of the MX of a domain
|
||||||
// ex :
|
// ex :
|
||||||
@@ -27,11 +26,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.4"
|
version = "0.9.5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logstream *syslog.Writer
|
logstream *syslog.Writer
|
||||||
|
conLdap *ldap.Conn
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
debug *bool
|
debug *bool
|
||||||
listen *string
|
listen *string
|
||||||
@@ -43,20 +43,22 @@ var (
|
|||||||
defDomain *string
|
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()
|
mutex.Lock()
|
||||||
result, err := conLdap.Search(searchReq)
|
result, err := conLdap.Search(searchReq)
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
// Let's just manage connection errors here
|
// Let's just manage connection errors here
|
||||||
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
if err != nil && strings.HasSuffix(err.Error(), "ldap: connection closed") {
|
||||||
logstream.Err("LDAP connection closed, retrying")
|
logstream.Err("LDAP connection closed, retrying")
|
||||||
|
mutex.Lock()
|
||||||
conLdap.Close()
|
conLdap.Close()
|
||||||
conLdap, err = connectLdap()
|
conLdap, err = connectLdap()
|
||||||
|
mutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, err
|
return result, err
|
||||||
} else {
|
} else {
|
||||||
attempt = attempt + 1
|
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,
|
searchReq := ldap.NewSearchRequest(*ldapBaseDN, ldap.ScopeWholeSubtree, 0, 0, 0,
|
||||||
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
false, filter, []string{*ldapAttr}, []ldap.Control{})
|
||||||
|
|
||||||
result, err := ldapSearch(conLdap, searchReq, 0)
|
result, err := ldapSearch(searchReq, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
logstream.Err(fmt.Sprintln("Error searching into LDAP: ", err))
|
||||||
s := strings.Replace(err.Error(), " ", "%20", -1)
|
s := strings.Replace(err.Error(), " ", "%20", -1)
|
||||||
@@ -159,7 +161,9 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func connectLdap() (*ldap.Conn, error) {
|
func connectLdap() (*ldap.Conn, error) {
|
||||||
conLdap, err := ldap.DialURL(*ldapURL)
|
var err error
|
||||||
|
|
||||||
|
conLdap, err = ldap.DialURL(*ldapURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logstream.Err(fmt.Sprintln("Error dialing LDAP: ", err))
|
logstream.Err(fmt.Sprintln("Error dialing LDAP: ", err))
|
||||||
return conLdap, err
|
return conLdap, err
|
||||||
|
|||||||
Reference in New Issue
Block a user