Skip to content

Commit

Permalink
Minor token changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 2, 2020
1 parent dc79f73 commit 1fb78f8
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/api.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asyncdispatch, httpclient, uri, json, strutils, options
import asyncdispatch, httpclient, uri, strutils, json
import types, query, formatters, consts, apiutils, parser

proc getGraphProfile*(username: string): Future[Profile] {.async.} =
Expand Down
3 changes: 1 addition & 2 deletions src/apiutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =

const rl = "x-rate-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.reset = fromUnix(parseInt(resp.headers[rl & "reset"]))

Expand All @@ -54,7 +53,7 @@ proc fetch*(url: Uri; retried=false; oldApi=false): Future[JsonNode] {.async.} =
echo "bad token"
except:
echo "error: ", url
return nil
result = nil
finally:
if keepToken:
token.release()
Expand Down
3 changes: 1 addition & 2 deletions src/parserutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ template `?`*(js: JsonNode): untyped =
template `with`*(ident, value, body): untyped =
block:
let ident {.inject.} = value
if ident != nil:
body
if ident != nil: body

template `with`*(ident; value: JsonNode; body): untyped =
block:
Expand Down
8 changes: 4 additions & 4 deletions src/tokens.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ proc fetchToken(): Future[Token] {.async.} =
except: discard

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())

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

proc isLimited(token: Token): bool {.inline.} =
Expand Down Expand Up @@ -54,6 +54,6 @@ proc poolTokens*(amount: int) {.async.} =

proc initTokenPool*(cfg: Config) {.async.} =
while true:
if tokenPool.filterIt(not it.isLimited).len < cfg.minTokens:
await poolTokens(min(5, cfg.minTokens - tokenPool.len))
if tokenPool.countIt(not it.isLimited) < cfg.minTokens:
await poolTokens(min(3, cfg.minTokens - tokenPool.len))
await sleepAsync(4000)
1 change: 0 additions & 1 deletion src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ genPrefsType()
type
Token* = ref object
tok*: string
limit*: int
remaining*: int
reset*: Time
init*: Time
Expand Down

0 comments on commit 1fb78f8

Please sign in to comment.