Add timeout, work on connection closing (previous commit left connections open)
This commit is contained in:
+16
-3
@@ -23,6 +23,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
"github.com/peterbourgon/ff"
|
"github.com/peterbourgon/ff"
|
||||||
@@ -30,7 +31,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
version = "0.9.7"
|
version = "0.9.7.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -46,6 +47,7 @@ var (
|
|||||||
ldapAttr *string
|
ldapAttr *string
|
||||||
defDomain *string
|
defDomain *string
|
||||||
pidFilePath *string
|
pidFilePath *string
|
||||||
|
timeout *int
|
||||||
)
|
)
|
||||||
|
|
||||||
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
func ldapSearch(searchReq *ldap.SearchRequest, attempt int) (*ldap.SearchResult, error) {
|
||||||
@@ -85,9 +87,19 @@ func handleConnection(connClt net.Conn, conLdap *ldap.Conn) {
|
|||||||
var mxdom string
|
var mxdom string
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
|
|
||||||
// It's client responsability to close connection
|
// Close connection when this function ends
|
||||||
//defer connClt.Close()
|
defer func() {
|
||||||
|
logstream.Info("Closing connection")
|
||||||
|
connClt.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
timeoutDuration := time.Duration(*timeout) * time.Second
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
// Set a deadline for reading. Read operation will fail if no data
|
||||||
|
// is received after deadline.
|
||||||
|
connClt.SetReadDeadline(time.Now().Add(timeoutDuration))
|
||||||
|
|
||||||
readlen, err := connClt.Read(buf)
|
readlen, err := connClt.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Dont notice if client closed connection
|
// Dont notice if client closed connection
|
||||||
@@ -229,6 +241,7 @@ func main() {
|
|||||||
ldapAttr = fs.String("ldapAttr", "relayName", "LDAP attribute containing the relay name to return")
|
ldapAttr = fs.String("ldapAttr", "relayName", "LDAP attribute containing the relay name to return")
|
||||||
defDomain = fs.String("domain", "", "Domain to add to relay name if not a FQDN (also via DOMAIN env var)")
|
defDomain = fs.String("domain", "", "Domain to add to relay name if not a FQDN (also via DOMAIN env var)")
|
||||||
pidFilePath = fs.String("pidfile", "/var/run/mxrouter/mxrouter.pid", "PID File (also via PIDFILE env var)")
|
pidFilePath = fs.String("pidfile", "/var/run/mxrouter/mxrouter.pid", "PID File (also via PIDFILE env var)")
|
||||||
|
timeout = fs.Int("timeout", 5, "timeout in seconds")
|
||||||
_ = fs.String("config", "", "config file (optional)")
|
_ = fs.String("config", "", "config file (optional)")
|
||||||
|
|
||||||
// Surcharge de la fonction Usage()
|
// Surcharge de la fonction Usage()
|
||||||
|
|||||||
Reference in New Issue
Block a user