Improve retry failure handling

Fixes #1388
This commit is contained in:
Zed
2026-06-08 23:59:50 +02:00
parent fb9107cff6
commit 5a4c8dd12c
2 changed files with 8 additions and 3 deletions
+4
View File
@@ -182,10 +182,12 @@ template fetchImpl(result, fetchBody) {.dirty.} =
template retry(bod) {.dirty.} = template retry(bod) {.dirty.} =
var session: Session var session: Session
var retrySuccess = false
for i in 0 ..< maxRetries: for i in 0 ..< maxRetries:
try: try:
session = nil session = nil
bod bod
retrySuccess = true
break break
except RateLimitError: except RateLimitError:
let api = if session.isNil: req.cookie.endpoint let api = if session.isNil: req.cookie.endpoint
@@ -199,6 +201,8 @@ template retry(bod) {.dirty.} =
session = nil session = nil
if retryDelayMs > 0: if retryDelayMs > 0:
await sleepAsync(retryDelayMs) await sleepAsync(retryDelayMs)
if not retrySuccess:
raise rateLimitError()
proc fetch*(req: ApiReq): Future[JsonNode] {.async.} = proc fetch*(req: ApiReq): Future[JsonNode] {.async.} =
retry: retry:
+4 -3
View File
@@ -144,9 +144,10 @@ proc getCachedUsername*(userId: string): Future[string] {.async.} =
else: else:
let user = await getGraphUserById(userId) let user = await getGraphUserById(userId)
result = user.username result = user.username
await setEx(key, baseCacheTime, result) if result.len > 0:
if result.len > 0 and user.id.len > 0: await setEx(key, baseCacheTime, result)
await all(cacheUserId(result, user.id), cache(user)) if user.id.len > 0:
await all(cacheUserId(result, user.id), cache(user))
# proc getCachedTweet*(id: int64): Future[Tweet] {.async.} = # proc getCachedTweet*(id: int64): Future[Tweet] {.async.} =
# if id == 0: return # if id == 0: return