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,51 @@
|
||||
package localdb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// serverPullCursor is the server-side NOW(6) taken before the last successful
|
||||
// incremental pull of a table, bound to the server it was taken from.
|
||||
type serverPullCursor struct {
|
||||
Scope string `json:"scope"`
|
||||
ServerTime string `json:"server_time"`
|
||||
}
|
||||
|
||||
func serverPullCursorKey(table string) string {
|
||||
return "server_pull_cursor:" + table
|
||||
}
|
||||
|
||||
// ServerPullScope identifies the MariaDB server+database a pull cursor belongs to,
|
||||
// so a cursor never survives a switch to another server.
|
||||
func (l *LocalDB) ServerPullScope() (string, error) {
|
||||
settings, err := l.GetSettings()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s:%d/%s", settings.Host, settings.Port, settings.Database), nil
|
||||
}
|
||||
|
||||
// GetServerPullCursor returns the stored server time for table if it was taken
|
||||
// on the server identified by scope.
|
||||
func (l *LocalDB) GetServerPullCursor(table, scope string) (string, bool) {
|
||||
raw, ok := l.getAppSettingValue(serverPullCursorKey(table))
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
var cursor serverPullCursor
|
||||
if err := json.Unmarshal([]byte(raw), &cursor); err != nil || cursor.Scope != scope || cursor.ServerTime == "" {
|
||||
return "", false
|
||||
}
|
||||
return cursor.ServerTime, true
|
||||
}
|
||||
|
||||
// SetServerPullCursor stores the server time a pull of table started at.
|
||||
func (l *LocalDB) SetServerPullCursor(table, scope, serverTime string) error {
|
||||
raw, err := json.Marshal(serverPullCursor{Scope: scope, ServerTime: serverTime})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return l.upsertAppSetting(l.db, serverPullCursorKey(table), string(raw), time.Now())
|
||||
}
|
||||
@@ -72,6 +72,11 @@ func (l *LocalDB) SyncQtSettings(mariaDB *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var current []LocalQtSetting
|
||||
if err := l.db.Select("name, value").Find(¤t).Error; err == nil && sameQtSettings(current, rows) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return l.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec("DELETE FROM local_qt_settings").Error; err != nil {
|
||||
return fmt.Errorf("clearing local_qt_settings: %w", err)
|
||||
@@ -142,3 +147,19 @@ func (l *LocalDB) GetConfiguratorSettings() (*ConfiguratorSettings, error) {
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func sameQtSettings(a, b []LocalQtSetting) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
values := make(map[string]string, len(a))
|
||||
for _, row := range a {
|
||||
values[row.Name] = row.Value
|
||||
}
|
||||
for _, row := range b {
|
||||
if v, ok := values[row.Name]; !ok || v != row.Value {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in new issue
Block a user