Skip to content

Commit 8f6cf2d

Browse files
committed
poc to reuse ldap connection
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
1 parent 8e73bd2 commit 8f6cf2d

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

identifier/backends/ldap/ldap.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net/url"
2727
"strconv"
2828
"strings"
29+
"sync"
2930
"time"
3031

3132
"github.com/go-ldap/ldap/v3"
@@ -71,6 +72,9 @@ type LDAPIdentifierBackend struct {
7172

7273
timeout int
7374
limiter *rate.Limiter
75+
76+
connLock sync.Mutex
77+
conn *ldap.Conn
7478
}
7579

7680
type ldapAttributeMapping map[string]string
@@ -372,7 +376,7 @@ func (b *LDAPIdentifierBackend) Logon(ctx context.Context, audience, username, p
372376
if err != nil {
373377
return false, nil, nil, nil, fmt.Errorf("ldap identifier backend logon connect error: %v", err)
374378
}
375-
defer l.Close()
379+
//defer l.Close()
376380

377381
// Search for the given username.
378382
entry, err := b.searchUsername(l, username, b.attributeMapping.attributes())
@@ -431,7 +435,7 @@ func (b *LDAPIdentifierBackend) ResolveUserByUsername(ctx context.Context, usern
431435
if err != nil {
432436
return nil, fmt.Errorf("ldap identifier backend resolve connect error: %v", err)
433437
}
434-
defer l.Close()
438+
//defer l.Close()
435439

436440
// Search for the given username.
437441
entry, err := b.searchUsername(l, username, b.attributeMapping.attributes())
@@ -464,7 +468,7 @@ func (b *LDAPIdentifierBackend) GetUser(ctx context.Context, entryID string, ses
464468
if err != nil {
465469
return nil, fmt.Errorf("ldap identifier backend get user connect error: %v", err)
466470
}
467-
defer l.Close()
471+
//defer l.Close()
468472

469473
entry, err := b.getUser(l, entryID, b.attributeMapping.attributes())
470474
if err != nil {
@@ -518,6 +522,12 @@ func (b *LDAPIdentifierBackend) Name() string {
518522
}
519523

520524
func (b *LDAPIdentifierBackend) connect(parentCtx context.Context) (*ldap.Conn, error) {
525+
b.connLock.Lock()
526+
defer b.connLock.Unlock()
527+
528+
if b.conn != nil && !b.conn.IsClosing() {
529+
return b.conn, nil
530+
}
521531
// A timeout for waiting for a limiter slot. The timeout also includes the
522532
// time to connect to the LDAP server which as a consequence means that both
523533
// getting a free slot and establishing the connection are one timeout.
@@ -556,9 +566,10 @@ func (b *LDAPIdentifierBackend) connect(parentCtx context.Context) (*ldap.Conn,
556566
if err != nil {
557567
return nil, err
558568
}
569+
b.conn = l
559570
}
560571

561-
return l, nil
572+
return b.conn, nil
562573
}
563574

564575
func (b *LDAPIdentifierBackend) searchUsername(l *ldap.Conn, username string, attributes []string) (*ldap.Entry, error) {

0 commit comments

Comments
 (0)