From e0e9394eee6c4cabf935c37caf503e90dfb8e2ca Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 3 Aug 2026 14:31:54 -0700 Subject: [PATCH] Advance the Feedly fetch watermark only when the ID walk completes, back-dated to cover clock skew. --- .../Feedly/FeedlyAccountDelegate.swift | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 555f38a5b..ce3fdc0da 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -44,6 +44,10 @@ import Secrets // is picked up on subsequent syncs, since missing articles are recomputed each time. private static let maxArticleDownloadChunksPerSync = 20 + // The watermark is a local clock reading compared against Feedly’s server-side crawl + // times — back-dating it covers clock skew, and re-fetching the overlap is idempotent. + private static let articleFetchOverlapInterval: TimeInterval = 5 * 60 + let isOPMLImportSupported = false var isOPMLImportInProgress = false @@ -173,6 +177,8 @@ import Secrets Self.refreshAllMessage(summary: summary) } + var ingestTruncated = false + do { try await account.logActivity(kind: .refreshAll, successMessage: successMessage) { () -> RefreshAllSummary in var summary = RefreshAllSummary() @@ -190,14 +196,15 @@ import Secrets refreshProgress.completeTask() summary.feedListChanges = try await refreshFeedList(for: account) refreshProgress.completeTask() - let ingestedIDs = try await ingestStreamArticleIDs(for: account, userID: credentials.username) + let (ingestedIDs, truncated) = try await ingestStreamArticleIDs(for: account, userID: credentials.username) + ingestTruncated = truncated refreshProgress.completeTask() summary.statusRefreshCounts = try await refreshArticleStatusReturningCounts(for: account, includeStarred: true) refreshProgress.completeTask() // The ingest walk just fetched exactly the IDs changed since the last sync — // reuse them instead of walking global.all a second time with the same bounds. // On a first sync there is no updated set: everything is new. - let updatedIDs = accountSettings?.lastArticleFetchStartTime == nil ? Set() : ingestedIDs + let updatedIDs = accountSettings?.lastArticleFetchStartTime == nil ? Set() : ingestedIDs let missingIDs = await account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDateAsync() refreshProgress.completeTask() // Updated articles first — missing ones are recomputed every sync, so they @@ -209,7 +216,11 @@ import Secrets refreshProgress.completeTask() return summary } - accountSettings?.lastArticleFetchStartTime = startDate + // Don’t advance the watermark when the ID walk stopped at the page cap — + // the unwalked window would fall outside every future fetch and be lost. + if !ingestTruncated { + accountSettings?.lastArticleFetchStartTime = startDate.addingTimeInterval(-Self.articleFetchOverlapInterval) + } accountSettings?.lastRefreshCompletedDate = Date() Self.logger.debug("FeedlyAccountDelegate: Sync took \(-startDate.timeIntervalSinceNow, privacy: .public) seconds") } catch { @@ -946,14 +957,14 @@ private extension FeedlyAccountDelegate { /// status sync has something to attach to. Returns the collected IDs so refreshAll can /// reuse them as the updated-articles set instead of walking the same stream twice. @discardableResult - func ingestStreamArticleIDs(for account: Account, userID: String) async throws -> Set { + func ingestStreamArticleIDs(for account: Account, userID: String) async throws -> (ids: Set, truncated: Bool) { let resource = FeedlyCategoryResourceID.Global.all(for: userID) // Bounded — walking the entire global.all history every sync was a big part of // the request volume that got users rate limited. let newerThan = max(accountSettings?.lastArticleFetchStartTime ?? .distantPast, Date().bySubtracting(days: Self.streamIngestDaysLimit)) - return try await account.logActivity(kind: .fetchArticleIDs, detail: "All articles", successMessage: { "\($0.count) article IDs" }, { () -> Set in + return try await account.logActivity(kind: .fetchArticleIDs, detail: "All articles", successMessage: { "\($0.ids.count) article IDs" }, { () -> (ids: Set, truncated: Bool) in var collected = Set() var continuation: String? var pageCount = 0 @@ -967,7 +978,7 @@ private extension FeedlyAccountDelegate { if continuation != nil { Self.logger.info("Feedly: stopped the article ID walk at the page cap") } - return collected + return (collected, continuation != nil) }) }