diff --git a/src/apiutils.nim b/src/apiutils.nim index 6ceab6f..5fbd17c 100644 --- a/src/apiutils.nim +++ b/src/apiutils.nim @@ -182,10 +182,12 @@ template fetchImpl(result, fetchBody) {.dirty.} = template retry(bod) {.dirty.} = var session: Session + var retrySuccess = false for i in 0 ..< maxRetries: try: session = nil bod + retrySuccess = true break except RateLimitError: let api = if session.isNil: req.cookie.endpoint @@ -199,6 +201,8 @@ template retry(bod) {.dirty.} = session = nil if retryDelayMs > 0: await sleepAsync(retryDelayMs) + if not retrySuccess: + raise rateLimitError() proc fetch*(req: ApiReq): Future[JsonNode] {.async.} = retry: diff --git a/src/redis_cache.nim b/src/redis_cache.nim index bfd271f..e503e46 100644 --- a/src/redis_cache.nim +++ b/src/redis_cache.nim @@ -144,9 +144,10 @@ proc getCachedUsername*(userId: string): Future[string] {.async.} = else: let user = await getGraphUserById(userId) result = user.username - await setEx(key, baseCacheTime, result) - if result.len > 0 and user.id.len > 0: - await all(cacheUserId(result, user.id), cache(user)) + if result.len > 0: + await setEx(key, baseCacheTime, result) + if user.id.len > 0: + await all(cacheUserId(result, user.id), cache(user)) # proc getCachedTweet*(id: int64): Future[Tweet] {.async.} = # if id == 0: return