Merge branch 'feature/cache-control'

This commit is contained in:
Brent Simmons committed 2026-03-16 17:24:07 -07:00
commit ab2f9d16ff
2 files changed
+29 -10

No files matched your search

@@ -156,6 +156,7 @@ import os
}
if let cacheControlInfo = CacheControlInfo(urlResponse: httpResponse) {
Self.logger.debug("LocalAccountRefresher: setting cacheControlInfo maxAge: \(cacheControlInfo.maxAge) url: \(url.absoluteString)")
feed.cacheControlInfo = cacheControlInfo
}
@@ -281,18 +282,26 @@ private extension LocalAccountRefresher {
return false
}
static let cacheControlMinMaxAge: TimeInterval = 30 * 60 // 30 minutes
static let cacheControlMaxMaxAge: TimeInterval = 5 * 60 * 60 // 5 hours
static func feedShouldBeSkippedForCacheControlReasons(_ feed: Feed) -> Bool {
// We support Cache-Control only for openrss.org. The rest of the feed-providing
// universe hasn’t dealt with Cache-Control, and we routinely see days-long
// max-ages for even fast-moving feeds.
//
// However, openrss.org does make sure their Cache-Control headers are
// intentional, and we should honor those.
guard let cacheControlInfo = feed.cacheControlInfo, !cacheControlInfo.canResume else {
return false
}
// openrss.org gets unclamped Cache-Control — they configure it correctly.
if SpecialCase.urlStringContainSpecialCase(feed.url, [SpecialCase.openRSSOrgHostName]) {
if let cacheControlInfo = feed.cacheControlInfo, !cacheControlInfo.canResume {
Self.logger.info("LocalAccountRefresher: Dropping request for special case Cache-Control reasons: \(feed.url)")
return true
}
Self.logger.info("LocalAccountRefresher: Dropping request for Cache-Control reasons (openrss.org): \(feed.url)")
return true
}
// All other feeds: honor Cache-Control with clamped max-age
// (min 30 minutes, max 5 hours) because many sites misconfigure it.
// We’ve seen max-age as long as one year (for a feed that updates frequently).
if !cacheControlInfo.canResume(minMaxAge: cacheControlMinMaxAge, maxMaxAge: cacheControlMaxMaxAge) {
Self.logger.info("LocalAccountRefresher: Dropping request for Cache-Control reasons: \(feed.url)")
return true
}
return false
@@ -22,6 +22,16 @@ nonisolated public struct CacheControlInfo: Codable, Equatable {
Date() >= resumeDate
}
/// canResume with a clamped maxAge — the actual maxAge
/// is clamped to [minMaxAge, maxMaxAge]. We do this because
/// sites tend to misconfigure their max age — we’ve seen
/// feeds that make this as long as one year, which is
/// clearly not intentional.
public func canResume(minMaxAge: TimeInterval, maxMaxAge: TimeInterval) -> Bool {
let clampedMaxAge = max(minMaxAge, min(maxMaxAge, maxAge))
return Date() >= dateCreated + clampedMaxAge
}
public init(dateCreated: Date, maxAge: TimeInterval) {
self.dateCreated = dateCreated
self.maxAge = maxAge