Add more logging to the token pool
This commit is contained in:
parent
138826fb4f
commit
adaa94d998
|
@ -14,6 +14,10 @@ var
|
||||||
clientPool: HttpPool
|
clientPool: HttpPool
|
||||||
tokenPool: seq[Token]
|
tokenPool: seq[Token]
|
||||||
lastFailed: Time
|
lastFailed: Time
|
||||||
|
enableLogging = false
|
||||||
|
|
||||||
|
template log(str) =
|
||||||
|
if enableLogging: echo "[tokens] ", str
|
||||||
|
|
||||||
proc getPoolJson*(): JsonNode =
|
proc getPoolJson*(): JsonNode =
|
||||||
var
|
var
|
||||||
|
@ -77,8 +81,10 @@ proc fetchToken(): Future[Token] {.async.} =
|
||||||
|
|
||||||
return Token(tok: tok, init: time, lastUse: time)
|
return Token(tok: tok, init: time, lastUse: time)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
echo "[tokens] fetching token failed: ", e.msg
|
||||||
|
if "Try again" notin e.msg:
|
||||||
|
echo "[tokens] fetching tokens paused, resuming in 30 minutes"
|
||||||
lastFailed = getTime()
|
lastFailed = getTime()
|
||||||
echo "fetching token failed: ", e.msg
|
|
||||||
|
|
||||||
proc expired(token: Token): bool =
|
proc expired(token: Token): bool =
|
||||||
let time = getTime()
|
let time = getTime()
|
||||||
|
@ -100,6 +106,9 @@ proc isReady(token: Token; api: Api): bool =
|
||||||
proc release*(token: Token; used=false; invalid=false) =
|
proc release*(token: Token; used=false; invalid=false) =
|
||||||
if token.isNil: return
|
if token.isNil: return
|
||||||
if invalid or token.expired:
|
if invalid or token.expired:
|
||||||
|
if invalid: log "discarding invalid token"
|
||||||
|
elif token.expired: log "discarding expired token"
|
||||||
|
|
||||||
let idx = tokenPool.find(token)
|
let idx = tokenPool.find(token)
|
||||||
if idx > -1: tokenPool.delete(idx)
|
if idx > -1: tokenPool.delete(idx)
|
||||||
elif used:
|
elif used:
|
||||||
|
@ -115,6 +124,7 @@ proc getToken*(api: Api): Future[Token] {.async.} =
|
||||||
if not result.isReady(api):
|
if not result.isReady(api):
|
||||||
release(result)
|
release(result)
|
||||||
result = await fetchToken()
|
result = await fetchToken()
|
||||||
|
log "added new token to pool"
|
||||||
tokenPool.add result
|
tokenPool.add result
|
||||||
|
|
||||||
if not result.isNil:
|
if not result.isNil:
|
||||||
|
@ -143,10 +153,12 @@ proc poolTokens*(amount: int) {.async.} =
|
||||||
except: discard
|
except: discard
|
||||||
|
|
||||||
if not newToken.isNil:
|
if not newToken.isNil:
|
||||||
|
log "added new token to pool"
|
||||||
tokenPool.add newToken
|
tokenPool.add newToken
|
||||||
|
|
||||||
proc initTokenPool*(cfg: Config) {.async.} =
|
proc initTokenPool*(cfg: Config) {.async.} =
|
||||||
clientPool = HttpPool()
|
clientPool = HttpPool()
|
||||||
|
enableLogging = cfg.enableDebug
|
||||||
|
|
||||||
while true:
|
while true:
|
||||||
if tokenPool.countIt(not it.isLimited(Api.timeline)) < cfg.minTokens:
|
if tokenPool.countIt(not it.isLimited(Api.timeline)) < cfg.minTokens:
|
||||||
|
|
Loading…
Reference in New Issue