这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ func NewConfigOptions() *configOptions {
return validateChoices(rawValue, []string{"round_robin", "entry_frequency"})
},
},
"POLLING_JITTER": {
ParsedDuration: 10 * time.Minute,
RawValue: "10",
ValueType: minuteType,
Validator: func(rawValue string) error {
return validateGreaterOrEqualThan(rawValue, 1)
},
},
"PORT": {
ParsedStringValue: "",
RawValue: "",
Expand Down Expand Up @@ -902,6 +910,10 @@ func (c *configOptions) PollingScheduler() string {
return c.options["POLLING_SCHEDULER"].ParsedStringValue
}

func (c *configOptions) PollingJitter() time.Duration {
return c.options["POLLING_JITTER"].ParsedDuration
}

func (c *configOptions) Port() string {
return c.options["PORT"].ParsedStringValue
}
Expand Down
15 changes: 15 additions & 0 deletions internal/model/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package model // import "miniflux.app/v2/internal/model"
import (
"fmt"
"io"
"math/rand"
"time"

"miniflux.app/v2/internal/config"
Expand Down Expand Up @@ -143,6 +144,20 @@ func (f *Feed) ScheduleNextCheck(weeklyCount int, refreshDelay time.Duration) ti
interval = min(interval, config.Opts.SchedulerEntryFrequencyMaxInterval())
}

// Apply a small random jitter to spread next checks and reduce thundering herds.
jitterMax := config.Opts.PollingJitter()

randomJitter := time.Duration(rand.Int63n(int64(jitterMax + 1)))
interval += randomJitter

// Re-apply max clamping after randomJitter to avoid exceeding configured caps.
switch config.Opts.PollingScheduler() {
case SchedulerRoundRobin:
interval = min(interval, config.Opts.SchedulerRoundRobinMaxInterval())
case SchedulerEntryFrequency:
interval = min(interval, config.Opts.SchedulerEntryFrequencyMaxInterval())
}

f.NextCheckAt = time.Now().Add(interval)
return interval
}
Expand Down
15 changes: 9 additions & 6 deletions internal/model/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ func TestFeedCheckedNow(t *testing.T) {
}

func checkTargetInterval(t *testing.T, feed *Feed, targetInterval time.Duration, timeBefore time.Time, message string) {
if feed.NextCheckAt.Before(timeBefore.Add(targetInterval)) {
t.Errorf(`The next_check_at should be after timeBefore + %s`, message)
}
if feed.NextCheckAt.After(time.Now().Add(targetInterval)) {
t.Errorf(`The next_check_at should be before now + %s`, message)
}
// Allow a positive jitter up to 10 minutes added by the scheduler.
jitterMax := 10 * time.Minute

if feed.NextCheckAt.Before(timeBefore.Add(targetInterval)) {
t.Errorf(`The next_check_at should be after timeBefore + %s`, message)
}
if feed.NextCheckAt.After(time.Now().Add(targetInterval + jitterMax)) {
t.Errorf(`The next_check_at should be before now + %s (with jitter)`, message)
}
}

func TestFeedScheduleNextCheckRoundRobinDefault(t *testing.T) {
Expand Down