mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-18 04:47:28 +00:00
Optimise cache cleanup run to avoid the GC which causes CPU usage to go higher than necessary.
This commit is contained in:
@@ -709,43 +709,16 @@ func (t *TraefikOidc) buildAuthURL(redirectURL, state, nonce string) string {
|
|||||||
|
|
||||||
// startTokenCleanup starts the token cleanup goroutine
|
// startTokenCleanup starts the token cleanup goroutine
|
||||||
func (t *TraefikOidc) startTokenCleanup() {
|
func (t *TraefikOidc) startTokenCleanup() {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ticker := time.NewTicker(1 * time.Minute) // Run cleanup every minute
|
||||||
ticker := time.NewTicker(15 * time.Second) // More frequent cleanup
|
go func() {
|
||||||
|
defer ticker.Stop()
|
||||||
go func() {
|
for range ticker.C {
|
||||||
defer ticker.Stop()
|
t.logger.Debug("Starting token cleanup cycle")
|
||||||
defer cancel()
|
t.tokenCache.Cleanup()
|
||||||
|
t.tokenBlacklist.Cleanup()
|
||||||
for range ticker.C {
|
// Removed runtime.GC() call
|
||||||
t.logger.Debug("Starting token cleanup cycle")
|
}
|
||||||
|
}()
|
||||||
// Run cleanup in a separate goroutine with shorter timeout
|
|
||||||
cleanupCtx, cleanupCancel := context.WithTimeout(ctx, 5*time.Second)
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(done)
|
|
||||||
// Clean up in smaller batches to prevent long-running operations
|
|
||||||
t.tokenCache.Cleanup()
|
|
||||||
t.tokenBlacklist.Cleanup()
|
|
||||||
|
|
||||||
// Force garbage collection after cleanup
|
|
||||||
runtime.GC()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Wait for cleanup to complete or timeout
|
|
||||||
select {
|
|
||||||
case <-cleanupCtx.Done():
|
|
||||||
if cleanupCtx.Err() == context.DeadlineExceeded {
|
|
||||||
t.logger.Error("Token cleanup cycle timed out")
|
|
||||||
}
|
|
||||||
case <-done:
|
|
||||||
t.logger.Debug("Token cleanup cycle completed successfully")
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupCancel()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevokeToken adds the token to the blacklist
|
// RevokeToken adds the token to the blacklist
|
||||||
|
|||||||
Reference in New Issue
Block a user