Add debug logging and optimize SQLite performance
- Add debug logging for goroutines and memory usage (enabled via DEBUG=routewatch) - Increase SQLite connection pool from 1 to 10 connections for better concurrency - Optimize SQLite pragmas for balanced performance and safety - Add proper shutdown handling for peering handler - Define constants to avoid magic numbers in code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -139,6 +139,10 @@ func (rw *RouteWatch) Shutdown() {
|
||||
rw.logger.Info("Flushing prefix handler")
|
||||
rw.prefixHandler.Stop()
|
||||
}
|
||||
if rw.peeringHandler != nil {
|
||||
rw.logger.Info("Flushing peering handler")
|
||||
rw.peeringHandler.Stop()
|
||||
}
|
||||
|
||||
// Stop services
|
||||
rw.streamer.Stop()
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -14,8 +16,43 @@ import (
|
||||
const (
|
||||
// shutdownTimeout is the maximum time allowed for graceful shutdown
|
||||
shutdownTimeout = 60 * time.Second
|
||||
// debugInterval is how often to log debug stats
|
||||
debugInterval = 60 * time.Second
|
||||
// bytesPerMB is bytes per megabyte
|
||||
bytesPerMB = 1024 * 1024
|
||||
)
|
||||
|
||||
// logDebugStats logs goroutine count and memory usage
|
||||
func logDebugStats(logger *logger.Logger) {
|
||||
// Only run if DEBUG env var contains "routewatch"
|
||||
debugEnv := os.Getenv("DEBUG")
|
||||
if !strings.Contains(debugEnv, "routewatch") {
|
||||
return
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(debugInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
|
||||
logger.Debug("System stats",
|
||||
"goroutines", runtime.NumGoroutine(),
|
||||
"alloc_mb", m.Alloc/bytesPerMB,
|
||||
"total_alloc_mb", m.TotalAlloc/bytesPerMB,
|
||||
"sys_mb", m.Sys/bytesPerMB,
|
||||
"num_gc", m.NumGC,
|
||||
"heap_alloc_mb", m.HeapAlloc/bytesPerMB,
|
||||
"heap_sys_mb", m.HeapSys/bytesPerMB,
|
||||
"heap_idle_mb", m.HeapIdle/bytesPerMB,
|
||||
"heap_inuse_mb", m.HeapInuse/bytesPerMB,
|
||||
"heap_released_mb", m.HeapReleased/bytesPerMB,
|
||||
"stack_inuse_mb", m.StackInuse/bytesPerMB,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// CLIEntry is the main entry point for the CLI
|
||||
func CLIEntry() {
|
||||
app := fx.New(
|
||||
@@ -24,6 +61,9 @@ func CLIEntry() {
|
||||
fx.Invoke(func(lc fx.Lifecycle, rw *RouteWatch, logger *logger.Logger) {
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(_ context.Context) error {
|
||||
// Start debug stats logging
|
||||
go logDebugStats(logger)
|
||||
|
||||
go func() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user