mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-05-11 23:25:14 +00:00
dd2f044f2b
Closes #921 ## Summary Follow-up to #920 (incremental auto-vacuum). Addresses both items from the adversarial review: ### 1. RW connection caching Previously, every call to `openRW(dbPath)` opened a new SQLite RW connection and closed it after use. This happened in: - `runIncrementalVacuum` (~4x/hour) - `PruneOldPackets`, `PruneOldMetrics`, `RemoveStaleObservers` - `buildAndPersistEdges`, `PruneNeighborEdges` - All neighbor persist operations Now a single `*sql.DB` handle (with `MaxOpenConns(1)`) is cached process-wide via `cachedRW(dbPath)`. The underlying connection pool manages serialization. The original `openRW()` function is retained for one-shot test usage. ### 2. DBConfig dedup `DBConfig` was defined identically in both `cmd/server/config.go` and `cmd/ingestor/config.go`. Extracted to `internal/dbconfig/` as a shared package; both binaries now use a type alias (`type DBConfig = dbconfig.DBConfig`). ## Tests added | Test | File | |------|------| | `TestCachedRW_ReturnsSameHandle` | `cmd/server/rw_cache_test.go` | | `TestCachedRW_100Calls_SingleConnection` | `cmd/server/rw_cache_test.go` | | `TestGetIncrementalVacuumPages_Default` | `internal/dbconfig/dbconfig_test.go` | | `TestGetIncrementalVacuumPages_Configured` | `internal/dbconfig/dbconfig_test.go` | ## Verification ``` ok github.com/corescope/server 20.069s ok github.com/corescope/ingestor 47.117s ok github.com/meshcore-analyzer/dbconfig 0.003s ``` Both binaries build cleanly. 100 sequential `cachedRW()` calls return the same handle with exactly 1 entry in the cache map. --------- Co-authored-by: you <you@example.com>
56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestCachedRW_ReturnsSameHandle(t *testing.T) {
|
|
dir := t.TempDir()
|
|
dbPath := filepath.Join(dir, "test.db")
|
|
|
|
// Create the DB file
|
|
f, _ := os.Create(dbPath)
|
|
f.Close()
|
|
|
|
defer closeRWCache()
|
|
|
|
db1, err := cachedRW(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("first cachedRW: %v", err)
|
|
}
|
|
db2, err := cachedRW(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("second cachedRW: %v", err)
|
|
}
|
|
if db1 != db2 {
|
|
t.Fatalf("cachedRW returned different handles: %p vs %p", db1, db2)
|
|
}
|
|
}
|
|
|
|
func TestCachedRW_100Calls_SingleConnection(t *testing.T) {
|
|
dir := t.TempDir()
|
|
dbPath := filepath.Join(dir, "test.db")
|
|
f, _ := os.Create(dbPath)
|
|
f.Close()
|
|
|
|
defer closeRWCache()
|
|
|
|
var first interface{}
|
|
for i := 0; i < 100; i++ {
|
|
db, err := cachedRW(dbPath)
|
|
if err != nil {
|
|
t.Fatalf("call %d: %v", i, err)
|
|
}
|
|
if i == 0 {
|
|
first = db
|
|
} else if db != first {
|
|
t.Fatalf("call %d returned different handle", i)
|
|
}
|
|
}
|
|
if rwCacheLen() != 1 {
|
|
t.Fatalf("expected 1 cached connection, got %d", rwCacheLen())
|
|
}
|
|
}
|