Minor token changes
This commit is contained in:
parent
dc79f736d0
commit
1fb78f8047
|
@ -1,4 +1,4 @@
|
||||||
import asyncdispatch, httpclient, uri, json, strutils, options
|
import asyncdispatch, httpclient, uri, strutils, json
|
||||||
import types, query, formatters, consts, apiutils, parser
|
import types, query, formatters, consts, apiutils, parser
|
||||||
|
|
||||||
proc getGraphProfile*(username: string): Future[Profile] {.async.} =
|
proc getGraphProfile*(username: string): Future[Profile] {.async.} =
|
||||||
|
|
|
@ -36,7 +36,6 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =
|
||||||
|
|
||||||
const rl = "x-rate-limit-"
|
const rl = "x-rate-limit-"
|
||||||
if not oldApi and resp.headers.hasKey(rl & "limit"):
|
if not oldApi and resp.headers.hasKey(rl & "limit"):
|
||||||
token.limit = parseInt(resp.headers[rl & "limit"])
|
|
||||||
token.remaining = parseInt(resp.headers[rl & "remaining"])
|
token.remaining = parseInt(resp.headers[rl & "remaining"])
|
||||||
token.reset = fromUnix(parseInt(resp.headers[rl & "reset"]))
|
token.reset = fromUnix(parseInt(resp.headers[rl & "reset"]))
|
||||||
|
|
||||||
|
@ -54,7 +53,7 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =
|
||||||
echo "bad token"
|
echo "bad token"
|
||||||
except:
|
except:
|
||||||
echo "error: ", url
|
echo "error: ", url
|
||||||
return nil
|
result = nil
|
||||||
finally:
|
finally:
|
||||||
if keepToken:
|
if keepToken:
|
||||||
token.release()
|
token.release()
|
||||||
|
|
|
@ -19,8 +19,7 @@ template `?`*(js: JsonNode): untyped =
|
||||||
template `with`*(ident, value, body): untyped =
|
template `with`*(ident, value, body): untyped =
|
||||||
block:
|
block:
|
||||||
let ident {.inject.} = value
|
let ident {.inject.} = value
|
||||||
if ident != nil:
|
if ident != nil: body
|
||||||
body
|
|
||||||
|
|
||||||
template `with`*(ident; value: JsonNode; body): untyped =
|
template `with`*(ident; value: JsonNode; body): untyped =
|
||||||
block:
|
block:
|
||||||
|
|
|
@ -19,11 +19,11 @@ proc fetchToken(): Future[Token] {.async.} =
|
||||||
except: discard
|
except: discard
|
||||||
|
|
||||||
if pos == -1: echo "token parse fail"; return
|
if pos == -1: echo "token parse fail"; return
|
||||||
result = Token(tok: resp[pos+3 .. pos+21], limit: 187, remaining: 187,
|
result = Token(tok: resp[pos+3 .. pos+21], remaining: 187,
|
||||||
reset: getTime() + 15.minutes, init: getTime())
|
reset: getTime() + 15.minutes, init: getTime())
|
||||||
|
|
||||||
proc expired(token: Token): bool {.inline.} =
|
proc expired(token: Token): bool {.inline.} =
|
||||||
const expirationTime = 1.hours
|
const expirationTime = 2.hours
|
||||||
result = token.init < getTime() - expirationTime
|
result = token.init < getTime() - expirationTime
|
||||||
|
|
||||||
proc isLimited(token: Token): bool {.inline.} =
|
proc isLimited(token: Token): bool {.inline.} =
|
||||||
|
@ -54,6 +54,6 @@ proc poolTokens*(amount: int) {.async.} =
|
||||||
|
|
||||||
proc initTokenPool*(cfg: Config) {.async.} =
|
proc initTokenPool*(cfg: Config) {.async.} =
|
||||||
while true:
|
while true:
|
||||||
if tokenPool.filterIt(not it.isLimited).len < cfg.minTokens:
|
if tokenPool.countIt(not it.isLimited) < cfg.minTokens:
|
||||||
await poolTokens(min(5, cfg.minTokens - tokenPool.len))
|
await poolTokens(min(3, cfg.minTokens - tokenPool.len))
|
||||||
await sleepAsync(4000)
|
await sleepAsync(4000)
|
||||||
|
|
|
@ -6,7 +6,6 @@ genPrefsType()
|
||||||
type
|
type
|
||||||
Token* = ref object
|
Token* = ref object
|
||||||
tok*: string
|
tok*: string
|
||||||
limit*: int
|
|
||||||
remaining*: int
|
remaining*: int
|
||||||
reset*: Time
|
reset*: Time
|
||||||
init*: Time
|
init*: Time
|
||||||
|
|
Loading…
Reference in New Issue