Skip to content

Commit c6536e8

Browse files
gudvinrfguillot
authored andcommitted
refactor(http): use time.Duration for refresh interval
It's not clear which units of time used for refresh interval. Convert to time.Duration for clarity.
1 parent 30453ad commit c6536e8

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

internal/config/config_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,12 +784,22 @@ func TestForceRefreshInterval(t *testing.T) {
784784
t.Fatalf(`Parsing failure: %v`, err)
785785
}
786786

787-
expected := 42
787+
expected := 42 * time.Second
788788
result := opts.ForceRefreshInterval()
789789

790790
if result != expected {
791791
t.Fatalf(`Unexpected FORCE_REFRESH_INTERVAL value, got %v instead of %v`, result, expected)
792792
}
793+
794+
sorted := opts.SortedOptions(false)
795+
i := slices.IndexFunc(sorted, func(opt *option) bool {
796+
return opt.Key == "FORCE_REFRESH_INTERVAL"
797+
})
798+
799+
expectedSerialized := 42
800+
if got := sorted[i].Value; got != expectedSerialized {
801+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
802+
}
793803
}
794804

795805
func TestDefaultBatchSizeValue(t *testing.T) {

internal/config/options.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
defaultBasePath = ""
3030
defaultWorkerPoolSize = 16
3131
defaultPollingFrequency = 60
32-
defaultForceRefreshInterval = 30
32+
defaultForceRefreshInterval = 30 * time.Second
3333
defaultBatchSize = 100
3434
defaultPollingScheduler = "round_robin"
3535
defaultSchedulerEntryFrequencyMinInterval = 5 * time.Minute
@@ -130,7 +130,7 @@ type options struct {
130130
cleanupArchiveUnreadDays int
131131
cleanupArchiveBatchSize int
132132
cleanupRemoveSessionsDays int
133-
forceRefreshInterval int
133+
forceRefreshInterval time.Duration
134134
batchSize int
135135
schedulerEntryFrequencyMinInterval time.Duration
136136
schedulerEntryFrequencyMaxInterval time.Duration
@@ -392,7 +392,7 @@ func (o *options) WorkerPoolSize() int {
392392
}
393393

394394
// ForceRefreshInterval returns the force refresh interval
395-
func (o *options) ForceRefreshInterval() int {
395+
func (o *options) ForceRefreshInterval() time.Duration {
396396
return o.forceRefreshInterval
397397
}
398398

@@ -769,7 +769,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
769769
"OAUTH2_REDIRECT_URL": o.oauth2RedirectURL,
770770
"OAUTH2_USER_CREATION": o.oauth2UserCreationAllowed,
771771
"DISABLE_LOCAL_AUTH": o.disableLocalAuth,
772-
"FORCE_REFRESH_INTERVAL": o.forceRefreshInterval,
772+
"FORCE_REFRESH_INTERVAL": int(o.forceRefreshInterval.Seconds()),
773773
"POLLING_FREQUENCY": o.pollingFrequency,
774774
"POLLING_LIMIT_PER_HOST": o.pollingLimitPerHost,
775775
"POLLING_PARSING_ERROR_LIMIT": o.pollingParsingErrorLimit,

internal/config/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (p *parser) parseLines(lines []string) (err error) {
139139
case "WORKER_POOL_SIZE":
140140
p.opts.workerPoolSize = parseInt(value, defaultWorkerPoolSize)
141141
case "FORCE_REFRESH_INTERVAL":
142-
p.opts.forceRefreshInterval = parseInt(value, defaultForceRefreshInterval)
142+
p.opts.forceRefreshInterval = parseInterval(value, time.Second, defaultForceRefreshInterval)
143143
case "BATCH_SIZE":
144144
p.opts.batchSize = parseInt(value, defaultBatchSize)
145145
case "POLLING_FREQUENCY":

internal/http/request/context.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package request // import "miniflux.app/v2/internal/http/request"
66
import (
77
"net/http"
88
"strconv"
9+
"time"
910

1011
"miniflux.app/v2/internal/model"
1112
)
@@ -135,13 +136,13 @@ func FlashErrorMessage(r *http.Request) string {
135136
}
136137

137138
// LastForceRefresh returns the last force refresh timestamp.
138-
func LastForceRefresh(r *http.Request) int64 {
139+
func LastForceRefresh(r *http.Request) time.Time {
139140
jsonStringValue := getContextStringValue(r, LastForceRefreshContextKey)
140141
timestamp, err := strconv.ParseInt(jsonStringValue, 10, 64)
141142
if err != nil {
142-
return 0
143+
return time.Time{}
143144
}
144-
return timestamp
145+
return time.Unix(timestamp, 0)
145146
}
146147

147148
// ClientIP returns the client IP address stored in the context.

internal/ui/category_refresh.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func (h *handler) refreshCategory(w http.ResponseWriter, r *http.Request) int64
3232
sess := session.New(h.store, request.SessionID(r))
3333

3434
// Avoid accidental and excessive refreshes.
35-
if time.Now().UTC().Unix()-request.LastForceRefresh(r) < int64(config.Opts.ForceRefreshInterval())*60 {
36-
time := config.Opts.ForceRefreshInterval()
37-
sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", time, time))
35+
if time.Since(request.LastForceRefresh(r)) < config.Opts.ForceRefreshInterval() {
36+
seconds := int(config.Opts.ForceRefreshInterval().Seconds())
37+
sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", seconds, seconds))
3838
} else {
3939
userID := request.UserID(r)
4040
// We allow the end-user to force refresh all its feeds in this category

internal/ui/feed_refresh.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func (h *handler) refreshAllFeeds(w http.ResponseWriter, r *http.Request) {
3737
sess := session.New(h.store, request.SessionID(r))
3838

3939
// Avoid accidental and excessive refreshes.
40-
if time.Now().UTC().Unix()-request.LastForceRefresh(r) < int64(config.Opts.ForceRefreshInterval())*60 {
41-
time := config.Opts.ForceRefreshInterval()
42-
sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", time, time))
40+
if time.Since(request.LastForceRefresh(r)) < config.Opts.ForceRefreshInterval() {
41+
seconds := int(config.Opts.ForceRefreshInterval().Seconds())
42+
sess.NewFlashErrorMessage(printer.Plural("alert.too_many_feeds_refresh", seconds, seconds))
4343
} else {
4444
userID := request.UserID(r)
4545
// We allow the end-user to force refresh all its feeds

0 commit comments

Comments
 (0)