Improve token management logic
This commit is contained in:
parent
7af71ec480
commit
5b185c79de
2 changed files with 3 additions and 7 deletions
|
@ -53,10 +53,8 @@ proc fetch*(url: Uri; oldApi=false): Future[JsonNode] {.async.} =
|
|||
result = newJNull()
|
||||
|
||||
if not oldApi and resp.headers.hasKey(rl & "reset"):
|
||||
let time = fromUnix(parseInt(resp.headers[rl & "reset"]))
|
||||
if token.reset != time:
|
||||
token.remaining = parseInt(resp.headers[rl & "limit"])
|
||||
token.reset = time
|
||||
token.remaining = parseInt(resp.headers[rl & "remaining"])
|
||||
token.reset = fromUnix(parseInt(resp.headers[rl & "reset"]))
|
||||
|
||||
if result.getError notin {invalidToken, forbidden, badToken}:
|
||||
token.lastUse = getTime()
|
||||
|
|
|
@ -57,7 +57,7 @@ template expired(token: Token): untyped =
|
|||
token.lastUse < time - maxLastUse
|
||||
|
||||
template isLimited(token: Token): untyped =
|
||||
token == nil or (token.remaining <= 1 and token.reset > getTime()) or
|
||||
token == nil or (token.remaining <= 5 and token.reset > getTime()) or
|
||||
token.expired
|
||||
|
||||
proc release*(token: Token; invalid=false) =
|
||||
|
@ -79,8 +79,6 @@ proc getToken*(): Future[Token] {.async.} =
|
|||
if result == nil:
|
||||
raise rateLimitError()
|
||||
|
||||
dec result.remaining
|
||||
|
||||
proc poolTokens*(amount: int) {.async.} =
|
||||
var futs: seq[Future[Token]]
|
||||
for i in 0 ..< amount:
|
||||
|
|
Loading…
Reference in a new issue