Files
OpenFlare/internal/platform/lifecycle/lifecycle.go
T
ryan ae3b792e16 feat(core): sync framework security hardening and accessibility improvements
- add util.Go with panic recovery for background goroutines
- add util.EscapeLike and explicit ESCAPE clause for SQL LIKE queries
- add DummyCheckPassword and subtle.ConstantTimeCompare against timing attacks
- enforce session ID rotation upon login/oauth callback to prevent session fixation
- add sliding window login failure rate limiting and oauth state rate limiting
- fix redis client capture race in pubsub listeners and wait on stop channel
- adjust global --primary to oklch(51.1% 0.262 276.966) for WCAG AA contrast
- fix semantic heading levels and missing aria-labels across UI components
- document security, concurrency, and a11y standards in AGENTS.md
2026-08-27 23:01:28 +08:00

70 lines
1.5 KiB
Go

// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
// Package lifecycle manages global application and business component shutdown hooks.
package lifecycle
import (
"context"
"log"
"sync"
"github.com/Rain-kl/Wavelet/pkg/util"
)
// ShutdownFunc defines the signature for a graceful shutdown callback.
type ShutdownFunc func(ctx context.Context) error
type hook struct {
name string
fn ShutdownFunc
}
var (
hooks []hook
mu sync.Mutex
)
// OnShutdown registers a callback to be run during graceful shutdown.
func OnShutdown(name string, fn ShutdownFunc) {
mu.Lock()
defer mu.Unlock()
hooks = append(hooks, hook{name: name, fn: fn})
}
// Stop executes all registered shutdown hooks concurrently and waits for completion or context timeout.
func Stop(ctx context.Context) {
mu.Lock()
localHooks := make([]hook, len(hooks))
copy(localHooks, hooks)
mu.Unlock()
var wg sync.WaitGroup
for _, h := range localHooks {
wg.Add(1)
name, fn := h.name, h.fn
util.Go(func() {
defer wg.Done()
log.Printf("[Lifecycle] stopping %s...\n", name)
if err := fn(ctx); err != nil {
log.Printf("[Lifecycle] stop %s failed: %v\n", name, err)
} else {
log.Printf("[Lifecycle] %s stopped successfully\n", name)
}
})
}
done := make(chan struct{})
util.Go(func() {
wg.Wait()
close(done)
})
select {
case <-done:
log.Println("[Lifecycle] all services stopped gracefully")
case <-ctx.Done():
log.Printf("[Lifecycle] shutdown timed out: %v\n", ctx.Err())
}
}