feat: use sync groups for better cancellation

This commit is contained in:
Stavros
2026-05-08 18:08:27 +03:00
parent b73a9db061
commit 71ddfbbdba
7 changed files with 61 additions and 34 deletions
+4 -1
View File
@@ -77,6 +77,7 @@ type AuthService struct {
config model.Config
runtime model.RuntimeConfig
context context.Context
wg *sync.WaitGroup
ldap *LdapService
queries *repository.Queries
@@ -98,6 +99,7 @@ func NewAuthService(
config model.Config,
runtime model.RuntimeConfig,
context context.Context,
wg *sync.WaitGroup,
ldap *LdapService,
queries *repository.Queries,
oauthBroker *OAuthBrokerService,
@@ -106,6 +108,7 @@ func NewAuthService(
log: log,
runtime: runtime,
context: context,
wg: wg,
config: config,
loginAttempts: make(map[string]*LoginAttempt),
ldapGroupsCache: make(map[string]*LdapGroupsCache),
@@ -117,7 +120,7 @@ func NewAuthService(
}
func (auth *AuthService) Init() error {
go auth.CleanupOAuthSessionsRoutine()
auth.wg.Go(auth.CleanupOAuthSessionsRoutine)
return nil
}
+5 -1
View File
@@ -3,6 +3,7 @@ package service
import (
"context"
"strings"
"sync"
"github.com/tinyauthapp/tinyauth/internal/model"
"github.com/tinyauthapp/tinyauth/internal/utils/decoders"
@@ -16,6 +17,7 @@ type DockerService struct {
log *logger.Logger
client *client.Client
context context.Context
wg *sync.WaitGroup
isConnected bool
}
@@ -23,10 +25,12 @@ type DockerService struct {
func NewDockerService(
log *logger.Logger,
context context.Context,
wg *sync.WaitGroup,
) *DockerService {
return &DockerService{
log: log,
context: context,
wg: wg,
}
}
@@ -53,7 +57,7 @@ func (docker *DockerService) Init() error {
docker.isConnected = true
docker.log.App.Debug().Msg("Docker connected successfully")
go docker.watchAndClose()
docker.wg.Go(docker.watchAndClose)
return nil
}
+7 -4
View File
@@ -38,9 +38,9 @@ type ingressApp struct {
type KubernetesService struct {
log *logger.Logger
ctx context.Context
wg *sync.WaitGroup
client dynamic.Interface
cancel context.CancelFunc
started bool
mu sync.RWMutex
ingressApps map[ingressKey][]ingressApp
@@ -51,10 +51,12 @@ type KubernetesService struct {
func NewKubernetesService(
log *logger.Logger,
context context.Context,
wg *sync.WaitGroup,
) *KubernetesService {
return &KubernetesService{
log: log,
ctx: context,
wg: wg,
ingressApps: make(map[ingressKey][]ingressApp),
domainIndex: make(map[string]ingressAppKey),
appNameIndex: make(map[string]ingressAppKey),
@@ -264,8 +266,6 @@ func (k *KubernetesService) Init() error {
}
k.client = client
k.ctx, k.cancel = context.WithCancel(k.ctx)
gvr := schema.GroupVersionResource{
Group: "networking.k8s.io",
Version: "v1",
@@ -274,6 +274,7 @@ func (k *KubernetesService) Init() error {
accessCtx, accessCancel := context.WithTimeout(k.ctx, 5*time.Second)
defer accessCancel()
_, err = k.client.Resource(gvr).List(accessCtx, metav1.ListOptions{Limit: 1})
if err != nil {
k.log.App.Warn().Err(err).Str("api", gvr.GroupVersion().String()).Msg("Failed to access Ingress API, Kubernetes label provider will be disabled")
@@ -282,7 +283,9 @@ func (k *KubernetesService) Init() error {
}
k.log.App.Debug().Str("api", gvr.GroupVersion().String()).Msg("Successfully accessed Ingress API, starting watcher")
go k.watchGVR(gvr)
k.wg.Go(func() {
k.watchGVR(gvr)
})
k.started = true
k.log.App.Debug().Msg("Kubernetes label provider started successfully")
+5 -2
View File
@@ -17,6 +17,7 @@ type LdapService struct {
log *logger.Logger
config model.Config
context context.Context
wg *sync.WaitGroup
conn *ldapgo.Conn
mutex sync.RWMutex
@@ -28,11 +29,13 @@ func NewLdapService(
log *logger.Logger,
config model.Config,
context context.Context,
wg *sync.WaitGroup,
) *LdapService {
return &LdapService{
log: log,
config: config,
context: context,
wg: wg,
}
}
@@ -88,7 +91,7 @@ func (ldap *LdapService) Init() error {
return fmt.Errorf("failed to connect to LDAP server: %w", err)
}
go func() {
ldap.wg.Go(func() {
ldap.log.App.Debug().Msg("Starting LDAP connection heartbeat routine")
ticker := time.NewTicker(5 * time.Minute)
@@ -111,7 +114,7 @@ func (ldap *LdapService) Init() error {
return
}
}
}()
})
return nil
}
+7 -3
View File
@@ -16,6 +16,7 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"
"slices"
@@ -117,6 +118,7 @@ type OIDCService struct {
runtime model.RuntimeConfig
queries *repository.Queries
context context.Context
wg *sync.WaitGroup
clients map[string]model.OIDCClientConfig
privateKey *rsa.PrivateKey
@@ -130,13 +132,15 @@ func NewOIDCService(
config model.Config,
runtime model.RuntimeConfig,
queries *repository.Queries,
context context.Context) *OIDCService {
context context.Context,
wg *sync.WaitGroup) *OIDCService {
return &OIDCService{
log: log,
config: config,
runtime: runtime,
queries: queries,
context: context,
wg: wg,
}
}
@@ -281,7 +285,7 @@ func (service *OIDCService) Init() error {
}
// Start cleanup routine
go service.cleanupRoutine()
service.wg.Go(service.cleanupRoutine)
return nil
}
@@ -811,7 +815,7 @@ func (service *OIDCService) cleanupRoutine() {
service.log.App.Debug().Msg("Finished OIDC cleanup routine")
case <-service.context.Done():
service.log.App.Debug().Msg("OIDC cleanup routine context cancelled, stopping")
service.log.App.Debug().Msg("Stopping OIDC cleanup routine")
return
}
}