@@ -106,8 +106,11 @@ func NewLdapHandler(opts ...Option) Handler {
106106 handler .servers = append (handler .servers , l )
107107
108108 // Initialize connection pool for this server
109- serverKey := fmt .Sprintf ("%s:%d" , l .Hostname , l .Port )
110- handler .connPool .connections [serverKey ] = make (chan * ldap.Conn , handler .connPool .maxPoolSize )
109+ var serverKey strings.Builder
110+ serverKey .WriteString (l .Hostname )
111+ serverKey .WriteString (":" )
112+ serverKey .WriteString (strconv .Itoa (l .Port ))
113+ handler .connPool .connections [serverKey .String ()] = make (chan * ldap.Conn , handler .connPool .maxPoolSize )
111114 }
112115
113116 // test server connectivity before listening, then keep it updated
@@ -123,7 +126,7 @@ func (h *ldapHandler) Bind(bindDN, bindSimplePw string, conn net.Conn) (result l
123126 start := time .Now ()
124127 defer func () {
125128 h .monitor .SetResponseTimeMetric (
126- map [string ]string {"operation" : "bind" , "status" : fmt . Sprintf ( "%v" , result )},
129+ map [string ]string {"operation" : "bind" , "status" : strconv . FormatInt ( int64 ( result ), 10 )},
127130 time .Since (start ).Seconds (),
128131 )
129132 }()
@@ -198,8 +201,10 @@ func (h *ldapHandler) Search(boundDN string, searchReq ldap.SearchRequest, conn
198201
199202 start := time .Now ()
200203 defer func () {
204+ status := strconv .FormatInt (int64 (result .ResultCode ), 10 )
205+
201206 h .monitor .SetResponseTimeMetric (
202- map [string ]string {"operation" : "search" , "status" : fmt . Sprintf ( "%v" , result . ResultCode ) },
207+ map [string ]string {"operation" : "search" , "status" : status },
203208 time .Since (start ).Seconds (),
204209 )
205210 }()
@@ -387,7 +392,7 @@ func (h *ldapHandler) Add(boundDN string, req ldap.AddRequest, conn net.Conn) (r
387392 start := time .Now ()
388393 defer func () {
389394 h .monitor .SetResponseTimeMetric (
390- map [string ]string {"operation" : "add" , "status" : fmt . Sprintf ( "%v" , result )},
395+ map [string ]string {"operation" : "add" , "status" : strconv . FormatInt ( int64 ( result ), 10 )},
391396 time .Since (start ).Seconds (),
392397 )
393398 }()
@@ -403,7 +408,7 @@ func (h *ldapHandler) Modify(boundDN string, req ldap.ModifyRequest, conn net.Co
403408
404409 defer func () {
405410 h .monitor .SetResponseTimeMetric (
406- map [string ]string {"operation" : "modify" , "status" : fmt . Sprintf ( "%v" , result )},
411+ map [string ]string {"operation" : "modify" , "status" : strconv . FormatInt ( int64 ( result ), 10 )},
407412 time .Since (start ).Seconds (),
408413 )
409414 }()
@@ -420,7 +425,7 @@ func (h *ldapHandler) Delete(boundDN string, deleteDN string, conn net.Conn) (re
420425
421426 defer func () {
422427 h .monitor .SetResponseTimeMetric (
423- map [string ]string {"operation" : "delete" , "status" : fmt . Sprintf ( "%v" , result )},
428+ map [string ]string {"operation" : "delete" , "status" : strconv . FormatInt ( int64 ( result ), 10 )},
424429 time .Since (start ).Seconds (),
425430 )
426431 }()
@@ -599,33 +604,39 @@ func (h *ldapHandler) getBestServer() (ldapBackend, error) {
599604// helper functions
600605func connID (conn net.Conn ) string {
601606 key := conn .LocalAddr ().String () + conn .RemoteAddr ().String ()
602- return fmt . Sprintf ( "%x" , xxhash .Sum64String (key ))
607+ return strconv . FormatUint ( xxhash .Sum64String (key ), 16 )
603608}
604609
605610// createConnection creates a new LDAP connection to the specified server
606611func (h * ldapHandler ) createConnection (server ldapBackend ) (* ldap.Conn , error ) {
607- dest := fmt .Sprintf ("%s:%d" , server .Hostname , server .Port )
612+ var dest strings.Builder
613+ dest .WriteString (server .Hostname )
614+ dest .WriteString (":" )
615+ dest .WriteString (strconv .Itoa (server .Port ))
608616
609617 switch server .Scheme {
610618 case "ldaps" :
611619 tlsCfg := & tls.Config {}
612620 if h .backend .Insecure {
613621 tlsCfg .InsecureSkipVerify = true
614622 }
615- return ldap .DialTLS ("tcp" , dest , tlsCfg )
623+ return ldap .DialTLS ("tcp" , dest . String () , tlsCfg )
616624 case "ldap" :
617- return ldap .Dial ("tcp" , dest )
625+ return ldap .Dial ("tcp" , dest . String () )
618626 default :
619627 return nil , fmt .Errorf ("unsupported LDAP scheme: %s" , server .Scheme )
620628 }
621629}
622630
623631// getConnectionFromPool tries to get a connection from the pool, creates a new one if pool is empty
624632func (h * ldapHandler ) getConnectionFromPool (server ldapBackend ) (* ldap.Conn , error ) {
625- serverKey := fmt .Sprintf ("%s:%d" , server .Hostname , server .Port )
633+ var serverKey strings.Builder
634+ serverKey .WriteString (server .Hostname )
635+ serverKey .WriteString (":" )
636+ serverKey .WriteString (strconv .Itoa (server .Port ))
626637
627638 h .connPool .mu .RLock ()
628- pool , exists := h .connPool .connections [serverKey ]
639+ pool , exists := h .connPool .connections [serverKey . String () ]
629640 h .connPool .mu .RUnlock ()
630641
631642 if ! exists {
@@ -657,10 +668,13 @@ func (h *ldapHandler) returnConnectionToPool(server ldapBackend, conn *ldap.Conn
657668 return
658669 }
659670
660- serverKey := fmt .Sprintf ("%s:%d" , server .Hostname , server .Port )
671+ var serverKey strings.Builder
672+ serverKey .WriteString (server .Hostname )
673+ serverKey .WriteString (":" )
674+ serverKey .WriteString (strconv .Itoa (server .Port ))
661675
662676 h .connPool .mu .RLock ()
663- pool , exists := h .connPool .connections [serverKey ]
677+ pool , exists := h .connPool .connections [serverKey . String () ]
664678 h .connPool .mu .RUnlock ()
665679
666680 if ! exists {
@@ -673,11 +687,11 @@ func (h *ldapHandler) returnConnectionToPool(server ldapBackend, conn *ldap.Conn
673687 select {
674688 case pool <- conn :
675689 // Successfully returned to pool
676- h .log .Debug ().Str ("server" , serverKey ).Msg ("Connection returned to pool" )
690+ h .log .Debug ().Str ("server" , serverKey . String () ).Msg ("Connection returned to pool" )
677691 default :
678692 // Pool is full, close connection
679693 conn .Close ()
680- h .log .Debug ().Str ("server" , serverKey ).Msg ("Pool full, connection closed" )
694+ h .log .Debug ().Str ("server" , serverKey . String () ).Msg ("Pool full, connection closed" )
681695 }
682696}
683697
0 commit comments