From 9f73f79a37666383f4866e69b30d9e8b9d1947d1 Mon Sep 17 00:00:00 2001 From: Stavros Date: Mon, 8 Jun 2026 15:43:40 +0300 Subject: [PATCH] fix: check for nil ldap connection in reconnect --- internal/bootstrap/service_bootstrap.go | 2 +- internal/service/ldap_service.go | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/bootstrap/service_bootstrap.go b/internal/bootstrap/service_bootstrap.go index bf94c5c4..9490b00b 100644 --- a/internal/bootstrap/service_bootstrap.go +++ b/internal/bootstrap/service_bootstrap.go @@ -8,7 +8,7 @@ import ( ) func (app *BootstrapApp) setupServices() error { - ldapService, err := service.NewLdapService(app.log, app.config, app.ding) + ldapService, err := service.NewLdapService(app.log, app.config, app.ctx, app.ding) if err != nil { app.log.App.Warn().Err(err).Msg("Failed to initialize LDAP connection, will continue without it") diff --git a/internal/service/ldap_service.go b/internal/service/ldap_service.go index 35b91a6a..9254e5f5 100644 --- a/internal/service/ldap_service.go +++ b/internal/service/ldap_service.go @@ -17,6 +17,7 @@ import ( type LdapService struct { log *logger.Logger config model.Config + ctx context.Context conn *ldapgo.Conn mutex sync.RWMutex @@ -26,6 +27,7 @@ type LdapService struct { func NewLdapService( log *logger.Logger, config model.Config, + ctx context.Context, dg *ding.Ding, ) (*LdapService, error) { if config.LDAP.Address == "" { @@ -35,6 +37,7 @@ func NewLdapService( ldap := &LdapService{ log: log, config: config, + ctx: ctx, } // Check whether authentication with client certificate is possible @@ -259,7 +262,9 @@ func (ldap *LdapService) reconnect(interval time.Duration) error { exp.Reset() operation := func() (*ldapgo.Conn, error) { - ldap.conn.Close() + if ldap.conn != nil { + ldap.conn.Close() + } conn, err := ldap.connect() if err != nil { return nil, err @@ -267,7 +272,7 @@ func (ldap *LdapService) reconnect(interval time.Duration) error { return conn, nil } - _, err := backoff.Retry(context.TODO(), operation, backoff.WithBackOff(exp), backoff.WithMaxTries(3)) + _, err := backoff.Retry(ldap.ctx, operation, backoff.WithBackOff(exp), backoff.WithMaxTries(3)) if err != nil { return err