Skip to content

Commit

Permalink
Temporary fix to prevent early token expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 19, 2020
1 parent 57f8417 commit db100bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/tokens.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ proc fetchToken(): Future[Token] {.async.} =
echo "token parse fail"
return Token()

let time = getTime()
result = Token(tok: resp[pos+3 .. pos+21], remaining: 187,
reset: getTime() + 15.minutes, init: getTime())
reset: time + 15.minutes, init: time, lastUse: time)

proc expired(token: Token): bool {.inline.} =
const expirationTime = 2.hours
result = token.init < getTime() - expirationTime
const
expirationTime = 2.hours
maxLastUse = 1.hours
let time = getTime()
result = token.init < time - expirationTime or
token.lastUse < time - maxLastUse

proc isLimited(token: Token): bool {.inline.} =
token == nil or (token.remaining <= 1 and token.reset > getTime()) or
token.expired

proc release*(token: Token) =
if token != nil and not token.expired:
token.lastUse = getTime()
tokenPool.insert(token)

proc getToken*(): Future[Token] {.async.} =
Expand Down
1 change: 1 addition & 0 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type
remaining*: int
reset*: Time
init*: Time
lastUse*: Time

Error* = enum
null = 0
Expand Down

0 comments on commit db100bb

Please sign in to comment.