From ebc50a6600fcf4ebc53df4846f790bb05757b3df Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 7 Feb 2026 22:07:27 +0200 Subject: feat(sync): add throttled sync mode Introduce an opt-in throttle that skips inactive repos based on local activity and per-repo cooldowns, and bump the version to 0.12.0. Co-authored-by: Cursor --- internal/state/state.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'internal/state') diff --git a/internal/state/state.go b/internal/state/state.go index c2a62ed..3288db1 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -11,6 +11,9 @@ import ( // State represents the persistent state of gitsyncer type State struct { LastBatchRun time.Time `json:"lastBatchRun"` + // Per-repo sync tracking for throttling + LastRepoSync map[string]time.Time `json:"lastRepoSync,omitempty"` + NextRepoSyncAllowed map[string]time.Time `json:"nextRepoSyncAllowed,omitempty"` } // Manager handles state persistence @@ -77,3 +80,48 @@ func (s *State) HasRunWithinWeek() bool { func (s *State) UpdateBatchRunTime() { s.LastBatchRun = time.Now() } + +// EnsureRepoMaps initializes per-repo maps if needed +func (s *State) EnsureRepoMaps() { + if s.LastRepoSync == nil { + s.LastRepoSync = make(map[string]time.Time) + } + if s.NextRepoSyncAllowed == nil { + s.NextRepoSyncAllowed = make(map[string]time.Time) + } +} + +// GetLastRepoSync returns the last sync time for a repo +func (s *State) GetLastRepoSync(repoName string) time.Time { + if s == nil || s.LastRepoSync == nil { + return time.Time{} + } + return s.LastRepoSync[repoName] +} + +// GetNextRepoSyncAllowed returns the next allowed sync time for a repo +func (s *State) GetNextRepoSyncAllowed(repoName string) time.Time { + if s == nil || s.NextRepoSyncAllowed == nil { + return time.Time{} + } + return s.NextRepoSyncAllowed[repoName] +} + +// SetRepoSync updates the last sync time and next allowed sync time for a repo +func (s *State) SetRepoSync(repoName string, lastSync time.Time, nextAllowed time.Time) { + if s == nil { + return + } + s.EnsureRepoMaps() + s.LastRepoSync[repoName] = lastSync + s.NextRepoSyncAllowed[repoName] = nextAllowed +} + +// SetNextRepoSyncAllowed updates only the next allowed sync time for a repo +func (s *State) SetNextRepoSyncAllowed(repoName string, nextAllowed time.Time) { + if s == nil { + return + } + s.EnsureRepoMaps() + s.NextRepoSyncAllowed[repoName] = nextAllowed +} -- cgit v1.2.3