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
@@ -3,12 +3,29 @@ package sync
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/db"
|
||||
)
|
||||
|
||||
// Worker performs background synchronization at regular intervals
|
||||
// WorkerJitter spreads background sync cycles by ±30% of the interval so that
|
||||
// clients started at the same time do not hit MariaDB in lockstep.
|
||||
const WorkerJitter = 0.3
|
||||
|
||||
// jitteredInterval returns base scaled by a random factor in [1-WorkerJitter, 1+WorkerJitter].
|
||||
func jitteredInterval(base time.Duration) time.Duration {
|
||||
factor := 1 - WorkerJitter + rand.Float64()*2*WorkerJitter
|
||||
return time.Duration(float64(base) * factor)
|
||||
}
|
||||
|
||||
// nextCycleDelay returns how long to wait so the next cycle starts a jittered
|
||||
// interval after started; zero if the cycle itself took longer than that.
|
||||
func nextCycleDelay(started time.Time, base time.Duration) time.Duration {
|
||||
return max(jitteredInterval(base)-time.Since(started), 0)
|
||||
}
|
||||
|
||||
// Worker performs background synchronization at jittered intervals
|
||||
type Worker struct {
|
||||
service *Service
|
||||
connMgr *db.ConnectionManager
|
||||
@@ -37,12 +54,14 @@ func (w *Worker) isOnline() bool {
|
||||
func (w *Worker) Start(ctx context.Context) {
|
||||
w.logger.Info("starting background sync worker", "interval", w.interval)
|
||||
|
||||
ticker := time.NewTicker(w.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Run once immediately
|
||||
// Run once immediately. The next cycle is scheduled from the start of the
|
||||
// previous one, so heartbeats are at most (1+WorkerJitter)*interval apart.
|
||||
started := time.Now()
|
||||
w.runSync()
|
||||
|
||||
timer := time.NewTimer(nextCycleDelay(started, w.interval))
|
||||
defer timer.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -51,8 +70,10 @@ func (w *Worker) Start(ctx context.Context) {
|
||||
case <-w.stopCh:
|
||||
w.logger.Info("background sync worker stopped")
|
||||
return
|
||||
case <-ticker.C:
|
||||
case <-timer.C:
|
||||
started = time.Now()
|
||||
w.runSync()
|
||||
timer.Reset(nextCycleDelay(started, w.interval))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +100,7 @@ func (w *Worker) runSync() {
|
||||
"reason_code", readiness.ReasonCode,
|
||||
"reason_text", readiness.ReasonText,
|
||||
)
|
||||
} else if pushed, err := w.service.PushPendingChanges(); err != nil {
|
||||
} else if pushed, err := w.service.pushPendingChanges(); err != nil {
|
||||
w.logger.Warn("background sync: failed to push pending changes", "error", err)
|
||||
} else if pushed > 0 {
|
||||
w.logger.Info("background sync: pushed pending changes", "count", pushed)
|
||||
|
||||
Reference in new issue
Block a user