chore(sqlite): allow periodic database maintenance to be disabled (#10441)

This change allows the periodic database maintenance to be disabled, while providing a way to programmatically start maintenance at a convenient moment.

Signed-off-by: Tommy van der Vorst <tommy@pixelspark.nl>
This commit is contained in:
Tommy van der Vorst
2026-01-14 21:10:54 +00:00
committed by GitHub
parent ed0baec2ca
commit 5bf27a432c
4 changed files with 40 additions and 9 deletions
+21 -6
View File
@@ -19,7 +19,6 @@ import (
"github.com/syncthing/syncthing/internal/db"
"github.com/syncthing/syncthing/internal/slogutil"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/thejerf/suture/v4"
)
const (
@@ -32,7 +31,7 @@ const (
gcMaxRuntime = 5 * time.Minute // max time to spend on gc, per table, per run
)
func (s *DB) Service(maintenanceInterval time.Duration) suture.Service {
func (s *DB) Service(maintenanceInterval time.Duration) db.DBService {
return newService(s, maintenanceInterval)
}
@@ -40,6 +39,7 @@ type Service struct {
sdb *DB
maintenanceInterval time.Duration
internalMeta *db.Typed
start chan struct{}
}
func (s *Service) String() string {
@@ -51,12 +51,19 @@ func newService(sdb *DB, maintenanceInterval time.Duration) *Service {
sdb: sdb,
maintenanceInterval: maintenanceInterval,
internalMeta: db.NewTyped(sdb, internalMetaPrefix),
start: make(chan struct{}),
}
}
func (s *Service) StartMaintenance() {
select {
case s.start <- struct{}{}:
default:
}
}
func (s *Service) Serve(ctx context.Context) error {
// Run periodic maintenance
// Figure out when we last ran maintenance and schedule accordingly. If
// it was never, do it now.
lastMaint, _, _ := s.internalMeta.Time(lastMaintKey)
@@ -66,21 +73,29 @@ func (s *Service) Serve(ctx context.Context) error {
wait = time.Minute
}
slog.DebugContext(ctx, "Next periodic run due", "after", wait)
timer := time.NewTimer(wait)
if s.maintenanceInterval == 0 {
timer.Stop()
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
case <-s.start:
}
if err := s.periodic(ctx); err != nil {
return wrap(err)
}
timer.Reset(s.maintenanceInterval)
slog.DebugContext(ctx, "Next periodic run due", "after", s.maintenanceInterval)
if s.maintenanceInterval != 0 {
timer.Reset(s.maintenanceInterval)
slog.DebugContext(ctx, "Next periodic run due", "after", s.maintenanceInterval)
}
_ = s.internalMeta.PutTime(lastMaintKey, time.Now())
}
}