From 24f5003c2bf63a35163737001c2932d6f44abf76 Mon Sep 17 00:00:00 2001 From: Louis Grasset Date: Fri, 27 Oct 2023 18:38:20 +0200 Subject: [PATCH] fix(cache): assume the sync is useless if latest post is already cached (#99) --- src/services/tweets-getter.service.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/services/tweets-getter.service.ts b/src/services/tweets-getter.service.ts index 7a34859..e245f98 100644 --- a/src/services/tweets-getter.service.ts +++ b/src/services/tweets-getter.service.ts @@ -38,16 +38,28 @@ export const tweetsGetterService = async ( const tweets: Tweet[] = []; const tweetsIds = twitterClient.getTweets(TWITTER_HANDLE, 200); - let hasRateLimit = false; + let hasRateLimitReached = false; + let latestTweetAlreadySynced = false; + let tweetsCount = 0; for await (const tweet of tweetsIds) { const rateLimitTimeout = setTimeout( - () => (hasRateLimit = true), + () => (hasRateLimitReached = true), 1000 * API_RATE_LIMIT, ); - if (hasRateLimit || isTweetCached(tweet, cache)) { + + if ( + latestTweetAlreadySynced || + hasRateLimitReached || + isTweetCached(tweet, cache) + ) { continue; } + // Skip posts sync if the latest one has already synced + if (tweetsCount === 0 && isTweetCached(tweet, cache)) { + latestTweetAlreadySynced = true; + } + const t: Tweet = { ...tweet, timestamp: (tweet.timestamp ?? 0) * 1000, @@ -59,9 +71,10 @@ export const tweetsGetterService = async ( tweets.unshift(eligibleTweet); } clearTimeout(rateLimitTimeout); + tweetsCount++; } - if (hasRateLimit) { + if (hasRateLimitReached) { log.warn( `rate limit reached, more than ${API_RATE_LIMIT}s to fetch a single tweet`, );