Skip to content

Commit

Permalink
Setting NextCheckAt due to TTL of a feed in feed.go.
Browse files Browse the repository at this point in the history
Add unit tests.
  • Loading branch information
shizunge committed Nov 23, 2023
1 parent 32779f5 commit 1efa6f7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 27 deletions.
11 changes: 10 additions & 1 deletion internal/model/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (f *Feed) CheckedNow() {
}

// ScheduleNextCheck set "next_check_at" of a feed based on the scheduler selected from the configuration.
func (f *Feed) ScheduleNextCheck(weeklyCount int) {
func (f *Feed) ScheduleNextCheck(weeklyCount int, newTTL int) {
switch config.Opts.PollingScheduler() {
case SchedulerEntryFrequency:
var intervalMinutes int
Expand All @@ -122,6 +122,15 @@ func (f *Feed) ScheduleNextCheck(weeklyCount int) {
default:
f.NextCheckAt = time.Now().Add(time.Minute * time.Duration(config.Opts.PollingFrequency()))
}
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
f.TTL = newTTL
if newTTL <= 0 {
return
}
minNextCheckAt := time.Now().Add(time.Minute * time.Duration(newTTL))
if f.NextCheckAt.IsZero() || f.NextCheckAt.Before(minNextCheckAt) {
f.NextCheckAt = minNextCheckAt
}
}

// FeedCreationRequest represents the request to create a feed.
Expand Down
78 changes: 74 additions & 4 deletions internal/model/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestFeedScheduleNextCheckDefault(t *testing.T) {

feed := &Feed{}
weeklyCount := 10
feed.ScheduleNextCheck(weeklyCount)
newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
Expand All @@ -99,7 +100,8 @@ func TestFeedScheduleNextCheckEntryCountBasedMaxInterval(t *testing.T) {
}
feed := &Feed{}
weeklyCount := maxInterval * 100
feed.ScheduleNextCheck(weeklyCount)
newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
Expand All @@ -126,7 +128,8 @@ func TestFeedScheduleNextCheckEntryCountBasedMinInterval(t *testing.T) {
}
feed := &Feed{}
weeklyCount := minInterval / 2
feed.ScheduleNextCheck(weeklyCount)
newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
Expand All @@ -151,7 +154,8 @@ func TestFeedScheduleNextCheckEntryFrequencyFactor(t *testing.T) {
}
feed := &Feed{}
weeklyCount := 7
feed.ScheduleNextCheck(weeklyCount)
newTTL := 0
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
Expand All @@ -161,3 +165,69 @@ func TestFeedScheduleNextCheckEntryFrequencyFactor(t *testing.T) {
t.Error(`The next_check_at should not be after the now + factor * count`)
}
}

func TestFeedScheduleNextCheckEntryFrequencySmallNewTTL(t *testing.T) {
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
maxInterval := 500
minInterval := 100
os.Clearenv()
os.Setenv("POLLING_SCHEDULER", "entry_frequency")
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))

var err error
parser := config.NewParser()
config.Opts, err = parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
feed := &Feed{}
weeklyCount := minInterval / 2
// TTL is smaller than minInterval.
newTTL := minInterval / 2
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
}

if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(minInterval))) {
t.Error(`The next_check_at should not be before the now + min interval`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(newTTL))) {
t.Error(`The next_check_at should not be before the now + TTL`)
}
}

func TestFeedScheduleNextCheckEntryFrequencyLargeNewTTL(t *testing.T) {
// If the feed has a TTL defined, we use it to make sure we don't check it too often.
maxInterval := 500
minInterval := 100
os.Clearenv()
os.Setenv("POLLING_SCHEDULER", "entry_frequency")
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MAX_INTERVAL", fmt.Sprintf("%d", maxInterval))
os.Setenv("SCHEDULER_ENTRY_FREQUENCY_MIN_INTERVAL", fmt.Sprintf("%d", minInterval))

var err error
parser := config.NewParser()
config.Opts, err = parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
feed := &Feed{}
// TTL is larger than minInterval.
weeklyCount := minInterval / 2
newTTL := minInterval * 2
feed.ScheduleNextCheck(weeklyCount, newTTL)

if feed.NextCheckAt.IsZero() {
t.Error(`The next_check_at must be set`)
}

if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(minInterval))) {
t.Error(`The next_check_at should not be before the now + min interval`)
}
if feed.NextCheckAt.Before(time.Now().Add(time.Minute * time.Duration(newTTL))) {
t.Error(`The next_check_at should not be before the now + TTL`)
}
}

Check failure on line 233 in internal/model/feed_test.go

View workflow job for this annotation

GitHub Actions / Golang Linter

File is not `gofmt`-ed with `-s` (gofmt)
30 changes: 8 additions & 22 deletions internal/reader/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"errors"
"log/slog"
"time"

"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/integration"
Expand Down Expand Up @@ -226,7 +225,7 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
}

originalFeed.CheckedNow()
originalFeed.ScheduleNextCheck(weeklyEntryCount)
originalFeed.ScheduleNextCheck(weeklyEntryCount, 0)

requestBuilder := fetcher.NewRequestBuilder()
requestBuilder.WithUsernameAndPassword(originalFeed.Username, originalFeed.Password)
Expand Down Expand Up @@ -280,26 +279,13 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
}

// If the feed has a TTL defined, we use it to make sure we don't check it too often.
if updatedFeed.TTL > 0 {
minNextCheckAt := time.Now().Add(time.Minute * time.Duration(updatedFeed.TTL))
slog.Debug("Feed TTL",
slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID),
slog.Int("ttl", updatedFeed.TTL),
slog.Time("next_check_at", originalFeed.NextCheckAt),
)

if originalFeed.NextCheckAt.IsZero() || originalFeed.NextCheckAt.Before(minNextCheckAt) {
slog.Debug("Updating next check date based on TTL",
slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID),
slog.Int("ttl", updatedFeed.TTL),
slog.Time("new_next_check_at", minNextCheckAt),
slog.Time("old_next_check_at", originalFeed.NextCheckAt),
)
originalFeed.NextCheckAt = minNextCheckAt
}
}
originalFeed.ScheduleNextCheck(weeklyEntryCount, updatedFeed.TTL)
slog.Debug("Updating next check date based on TTL",
slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID),
slog.Time("new_next_check_at", originalFeed.NextCheckAt),
slog.Int("ttl", updatedFeed.TTL),
)

originalFeed.Entries = updatedFeed.Entries
processor.ProcessFeedEntries(store, originalFeed, user, forceRefresh)
Expand Down

0 comments on commit 1efa6f7

Please sign in to comment.