Fix shutdown handling and optimize SQLite settings
- Fix Ctrl-C shutdown by using fx.Shutdowner instead of just canceling context - Pass context from fx lifecycle to rw.Run() for proper cancellation - Adjust WAL settings: checkpoint at 50MB, max size 100MB - Reduce busy timeout from 30s to 2s to fail fast on lock contention This should fix the issue where Ctrl-C doesn't cause shutdown and improve database responsiveness under heavy load.
This commit is contained in:
parent
21921a170c
commit
b9b0792df9
@ -96,13 +96,13 @@ func (d *Database) Initialize() error {
|
|||||||
"PRAGMA cache_size=-2097152", // 2GB cache (negative = KB)
|
"PRAGMA cache_size=-2097152", // 2GB cache (negative = KB)
|
||||||
"PRAGMA temp_store=MEMORY", // Use memory for temp tables
|
"PRAGMA temp_store=MEMORY", // Use memory for temp tables
|
||||||
"PRAGMA mmap_size=536870912", // 512MB memory-mapped I/O
|
"PRAGMA mmap_size=536870912", // 512MB memory-mapped I/O
|
||||||
"PRAGMA wal_autocheckpoint=1000", // Checkpoint every 1000 pages (4MB with 4KB pages)
|
"PRAGMA wal_autocheckpoint=12500", // Checkpoint every 12.5k pages (50MB with 4KB pages)
|
||||||
"PRAGMA wal_checkpoint(PASSIVE)", // Checkpoint now
|
"PRAGMA wal_checkpoint(PASSIVE)", // Checkpoint now
|
||||||
"PRAGMA page_size=4096", // Standard page size
|
"PRAGMA page_size=4096", // Standard page size
|
||||||
"PRAGMA busy_timeout=30000", // 30 second busy timeout
|
"PRAGMA busy_timeout=2000", // 2 second busy timeout
|
||||||
"PRAGMA locking_mode=NORMAL", // Allow multiple connections
|
"PRAGMA locking_mode=NORMAL", // Allow multiple connections
|
||||||
"PRAGMA read_uncommitted=true", // Allow dirty reads for better concurrency
|
"PRAGMA read_uncommitted=true", // Allow dirty reads for better concurrency
|
||||||
"PRAGMA journal_size_limit=67108864", // Limit WAL to 64MB
|
"PRAGMA journal_size_limit=104857600", // Limit WAL to 100MB
|
||||||
"PRAGMA analysis_limit=0", // Disable automatic ANALYZE
|
"PRAGMA analysis_limit=0", // Disable automatic ANALYZE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,16 +58,12 @@ func CLIEntry() {
|
|||||||
app := fx.New(
|
app := fx.New(
|
||||||
getModule(),
|
getModule(),
|
||||||
fx.StopTimeout(shutdownTimeout), // Allow 60 seconds for graceful shutdown
|
fx.StopTimeout(shutdownTimeout), // Allow 60 seconds for graceful shutdown
|
||||||
fx.Invoke(func(lc fx.Lifecycle, rw *RouteWatch, logger *logger.Logger) {
|
fx.Invoke(func(lc fx.Lifecycle, rw *RouteWatch, logger *logger.Logger, shutdowner fx.Shutdowner) {
|
||||||
lc.Append(fx.Hook{
|
lc.Append(fx.Hook{
|
||||||
OnStart: func(_ context.Context) error {
|
OnStart: func(ctx context.Context) error {
|
||||||
// Start debug stats logging
|
// Start debug stats logging
|
||||||
go logDebugStats(logger)
|
go logDebugStats(logger)
|
||||||
|
|
||||||
go func() {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// Handle shutdown signals
|
// Handle shutdown signals
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
||||||
@ -75,9 +71,12 @@ func CLIEntry() {
|
|||||||
go func() {
|
go func() {
|
||||||
<-sigCh
|
<-sigCh
|
||||||
logger.Info("Received shutdown signal")
|
logger.Info("Received shutdown signal")
|
||||||
cancel()
|
if err := shutdowner.Shutdown(); err != nil {
|
||||||
|
logger.Error("Failed to shutdown gracefully", "error", err)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
if err := rw.Run(ctx); err != nil {
|
if err := rw.Run(ctx); err != nil {
|
||||||
logger.Error("RouteWatch error", "error", err)
|
logger.Error("RouteWatch error", "error", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user