Skip to content

Commit 3e7f5c8

Browse files
committed
fix(reannounce): use consistent debounce window for cooldown checks
1 parent 8baf753 commit 3e7f5c8

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

internal/services/reannounce/service.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func (s *Service) GetMonitoredTorrents(ctx context.Context, instanceID int) []Mo
263263

264264
now := s.currentTime()
265265
instJobs := s.j[instanceID]
266+
debounceWindow := s.effectiveDebounceWindow(settings)
266267

267268
var result []MonitoredTorrent
268269
for _, torrent := range torrents {
@@ -286,7 +287,7 @@ func (s *Service) GetMonitoredTorrents(ctx context.Context, instanceID int) []Mo
286287
if job, ok := instJobs[hashUpper]; ok {
287288
if job.isRunning {
288289
state = MonitoredTorrentStateReannouncing
289-
} else if !job.lastCompleted.IsZero() && now.Sub(job.lastCompleted) < s.cfg.DebounceWindow {
290+
} else if !job.lastCompleted.IsZero() && now.Sub(job.lastCompleted) < debounceWindow {
290291
state = MonitoredTorrentStateCooldown
291292
}
292293
}
@@ -343,13 +344,7 @@ func (s *Service) enqueue(instanceID int, hash string, torrentName string, track
343344

344345
settings := s.getSettings(baseCtx, instanceID)
345346
isAggressive := settings != nil && settings.Aggressive
346-
// Determine debounce window: aggressive uses retry interval; non-aggressive uses global cooldown.
347-
debounceWindow := s.cfg.DebounceWindow
348-
if isAggressive {
349-
if interval := time.Duration(settings.ReannounceIntervalSeconds) * time.Second; interval > 0 {
350-
debounceWindow = interval
351-
}
352-
}
347+
debounceWindow := s.effectiveDebounceWindow(settings)
353348

354349
if !job.lastCompleted.IsZero() && debounceWindow > 0 {
355350
if elapsed := now.Sub(job.lastCompleted); elapsed < debounceWindow {
@@ -812,6 +807,17 @@ func (s *Service) currentTime() time.Time {
812807
return time.Now()
813808
}
814809

810+
// effectiveDebounceWindow returns the debounce duration to use for cooldown checks.
811+
// aggressive mode uses the retry interval; otherwise the global debounce window applies.
812+
func (s *Service) effectiveDebounceWindow(settings *models.InstanceReannounceSettings) time.Duration {
813+
if settings != nil && settings.Aggressive {
814+
if interval := time.Duration(settings.ReannounceIntervalSeconds) * time.Second; interval > 0 {
815+
return interval
816+
}
817+
}
818+
return s.cfg.DebounceWindow
819+
}
820+
815821
func (s *Service) extractTrackerDomain(trackerURL string) string {
816822
if trackerURL == "" {
817823
return ""

0 commit comments

Comments
 (0)