From 5184c96e85cbad75870f3913cd4b2562b135db12 Mon Sep 17 00:00:00 2001 From: Stavros Date: Tue, 2 Sep 2025 00:08:19 +0300 Subject: [PATCH] feat: add mutex to ldap service --- internal/service/ldap_service.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/service/ldap_service.go b/internal/service/ldap_service.go index 8576c4d..b3a1d86 100644 --- a/internal/service/ldap_service.go +++ b/internal/service/ldap_service.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "fmt" + "sync" "time" "github.com/cenkalti/backoff/v5" @@ -23,6 +24,7 @@ type LdapServiceConfig struct { type LdapService struct { Config LdapServiceConfig Conn *ldapgo.Conn + Mutex sync.RWMutex } func NewLdapService(config LdapServiceConfig) *LdapService { @@ -55,6 +57,8 @@ func (ldap *LdapService) Init() error { } func (ldap *LdapService) connect() (*ldapgo.Conn, error) { + ldap.Mutex.Lock() + conn, err := ldapgo.DialURL(ldap.Config.Address, ldapgo.DialWithTLSConfig(&tls.Config{ InsecureSkipVerify: ldap.Config.Insecure, MinVersion: tls.VersionTLS12, @@ -68,6 +72,8 @@ func (ldap *LdapService) connect() (*ldapgo.Conn, error) { return nil, err } + ldap.Mutex.Unlock() + // Set and return the connection ldap.Conn = conn return conn, nil @@ -86,10 +92,12 @@ func (ldap *LdapService) Search(username string) (string, error) { nil, ) + ldap.Mutex.Lock() searchResult, err := ldap.Conn.Search(searchRequest) if err != nil { return "", err } + ldap.Mutex.Unlock() if len(searchResult.Entries) != 1 { return "", fmt.Errorf("multiple or no entries found for user %s", username) @@ -100,10 +108,12 @@ func (ldap *LdapService) Search(username string) (string, error) { } func (ldap *LdapService) Bind(userDN string, password string) error { + ldap.Mutex.Lock() err := ldap.Conn.Bind(userDN, password) if err != nil { return err } + ldap.Mutex.Unlock() return nil } @@ -118,10 +128,12 @@ func (ldap *LdapService) heartbeat() error { nil, ) + ldap.Mutex.Lock() _, err := ldap.Conn.Search(searchRequest) if err != nil { return err } + ldap.Mutex.Unlock() // No error means the connection is alive return nil