From 53ede6765a1583538b2abc3142d858464b3cf01c Mon Sep 17 00:00:00 2001 From: Guillermo Serrahima Date: Thu, 16 Jul 2026 02:38:46 +0200 Subject: Implement fetched posts count tracking for catching gaps in cached posts --- main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 85c8712..6fb7bcf 100644 --- a/main.go +++ b/main.go @@ -373,14 +373,28 @@ func postsHandler(w http.ResponseWriter, r *http.Request) { } config.Logger.Debug("posts ready", "count", len(posts)) - // background refetch if cache is running low + // background refetch if cache is running low or has developed a gap oldestSent, hasOldest := config.Cache.GetOldestSent(session.DID) if !hasOldest || len(posts) > 0 { oldest := posts[len(posts)-1].CreatedAt count, _ := config.Cache.CountPostsOlderThan(session.DID, oldest) cursor, hasCursor := config.Cache.GetCursor(session.DID) - config.Logger.Debug("refetch check", "count", count, "pageSize", config.PageSize, "hasCursor", hasCursor, "oldest", oldest) - if count < config.PageSize && hasCursor { + + // skip right after a fresh fetch; expected count was just reset to match it + hasGap := false + if len(cached) > 0 { + if expectedCount, hasExpected := config.Cache.GetPostCount(session.DID); hasExpected { + hasGap = len(cached) < expectedCount + } + } + + config.Logger.Debug("refetch check", "count", count, "pageSize", config.PageSize, "hasCursor", hasCursor, "oldest", oldest, "hasGap", hasGap) + + switch { + case hasGap: + sessionCopy := session.Copy() + go fetchers.FetchAppBskyFeedPostsWithCursor(config, sessionCopy.DID, sessionCopy.PDS, sessionCopy, "") + case count < config.PageSize && hasCursor: sessionCopy := session.Copy() go fetchers.FetchAppBskyFeedPostsWithCursor(config, sessionCopy.DID, sessionCopy.PDS, sessionCopy, cursor) } -- cgit v1.2.3