fix(sync): stop MariaDB connection leak and cut per-cycle sync waste
- ConnectionManager reuses a stale-but-healthy pool instead of dialing a new one every 30s, and closes every pool it replaces (one client held ~30 connections). Pool capped at 2 connections, idle released after 1 min. - Background sync every 1 min with ±30% jitter, scheduled from cycle start; users-status online threshold = interval × 1.3. One heartbeat per cycle. - Partnumber books: read headers only, fetch partnumbers_json per book when new/incomplete; empty book counts as complete; an incomplete book is re-pulled once per process (server book lists BOT_AGIBOT_G2_PERF missing from qt_partnumber_book_items). - NeedSync in one UNION ALL query; SyncQtSettings skips unchanged rewrite. - Migration 035: server-maintained server_updated_at on qt_configurations and qt_projects. Clients pull only changed rows after one full pull per process; servers without 035 and old clients keep full pulls. - Migrator: fresh GORM statement per verify query and history insert; files with two verify checks failed to record. ADL: bible-local/decisions/2026-10-26-mariadb-single-pool.md, bible-local/decisions/2026-10-26-incremental-config-project-pull.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a08270b79
commit
44a09d4420
20 files changed
+901
-75
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func newTestConnectionManager(t *testing.T) (*ConnectionManager, *int) {
|
||||
t.Helper()
|
||||
local, err := localdb.New(filepath.Join(t.TempDir(), "qfs.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("open local db: %v", err)
|
||||
}
|
||||
cm := NewConnectionManager(local)
|
||||
dials := 0
|
||||
cm.dialFn = func() (*gorm.DB, error) {
|
||||
dials++
|
||||
return gorm.Open(sqlite.Open(filepath.Join(t.TempDir(), "remote.db")), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
}
|
||||
return cm, &dials
|
||||
}
|
||||
|
||||
func TestGetDBReusesStalePoolInsteadOfRedialing(t *testing.T) {
|
||||
cm, dials := newTestConnectionManager(t)
|
||||
|
||||
first, err := cm.GetDB()
|
||||
if err != nil {
|
||||
t.Fatalf("first GetDB: %v", err)
|
||||
}
|
||||
|
||||
// Simulate pingInterval elapsing between sync steps.
|
||||
cm.mu.Lock()
|
||||
cm.lastCheck = time.Now().Add(-2 * cm.pingInterval)
|
||||
cm.mu.Unlock()
|
||||
|
||||
second, err := cm.GetDB()
|
||||
if err != nil {
|
||||
t.Fatalf("second GetDB: %v", err)
|
||||
}
|
||||
if second != first {
|
||||
t.Fatalf("expected stale but healthy pool to be reused")
|
||||
}
|
||||
if *dials != 1 {
|
||||
t.Fatalf("expected 1 dial, got %d", *dials)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTryConnectClosesReplacedPool(t *testing.T) {
|
||||
cm, _ := newTestConnectionManager(t)
|
||||
|
||||
first, err := cm.GetDB()
|
||||
if err != nil {
|
||||
t.Fatalf("GetDB: %v", err)
|
||||
}
|
||||
if err := cm.TryConnect(); err != nil {
|
||||
t.Fatalf("TryConnect: %v", err)
|
||||
}
|
||||
|
||||
sqlDB, err := first.DB()
|
||||
if err != nil {
|
||||
t.Fatalf("sql.DB: %v", err)
|
||||
}
|
||||
if err := sqlDB.Ping(); err == nil {
|
||||
t.Fatalf("expected replaced pool to be closed")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user