v1.0.3 : updateCache have its own connection, minor msg modeling and error check

This commit is contained in:
yo
2026-07-14 17:16:48 +02:00
parent 0e73dac143
commit f59d548f04
+22 -8
View File
@@ -26,7 +26,7 @@ import (
)
const (
version = "1.0.2"
version = "1.0.3"
)
var (
@@ -176,6 +176,8 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
// Dont notice if client closed connection
if err.Error() != "EOF" && !strings.HasSuffix(err.Error(), "i/o timeout") {
logstream.Errorf("Error reading connection: %v\n", err.Error())
} else {
logstream.Debug("Error reading on connection : %s", err.Error())
}
return
}
@@ -276,12 +278,14 @@ func sendResponse(con net.Conn, respMsg string, respCode int) error {
return nil
}
// attempt is useless now. It was introduced to break after N attempts, but this is not implemented. Should it be?
func searchLdap(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")|| strings.HasSuffix(err.Error(), "ldap: conn is nil, expected net.Conn"))) {
if err != nil {
if strings.HasSuffix(err.Error(), "ldap: connection closed") || err == ldap.ErrNilConnection {
logstream.Error("LDAP connection closed, retrying")
mutex.Lock()
// 16/01/2023: panic: runtime error: invalid memory address or nil pointer dereference
@@ -298,6 +302,7 @@ func searchLdap(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult,
return searchLdap(searchReq, attempt)
}
}
}
return result, err
}
@@ -316,13 +321,22 @@ func connectLdap() (*ldap.Conn, error) {
return conLdap, err
}
// TODO : buildNetCache should have its own LDAP connection
func periodicallyUpdateCache(conLdap *ldap.Conn) {
func periodicallyUpdateCache() {
myLdap, err := connectLdap()
if err != nil {
return
}
// On initialise immediatement le cache
buildNetCacheFromIPNetwork(conLdap)
buildNetCacheFromIPNetwork(myLdap)
myLdap.Close()
for range time.Tick(time.Second * time.Duration(*refreshInterval)) {
buildNetCacheFromIPNetwork(conLdap)
myLdap, err := connectLdap()
if err != nil {
return
}
buildNetCacheFromIPNetwork(myLdap)
myLdap.Close()
}
}
@@ -336,7 +350,7 @@ func run() {
return
}
go periodicallyUpdateCache(conLdap)
go periodicallyUpdateCache()
// Spawn a go routine for incoming connection
for {
@@ -375,7 +389,7 @@ func main() {
return
}
fmt.Printf("%s: MyNetTCPTable v.%s starting\n", time.Now().Format(time.RFC3339), version)
fmt.Printf("%s: MyNetTCPTable v.%s listening on %s\n", time.Now().Format(time.RFC3339), version, *listen)
logstream = logrus.New()
level, err := logrus.ParseLevel(*logLevel)