From 4af26022b590ca081e2b269dde1029c2a4ba1b02 Mon Sep 17 00:00:00 2001 From: drpepper66666 <138406307+drpepper66666@users.noreply.github.com> Date: Mon, 3 Jul 2023 00:26:02 +0200 Subject: [PATCH] Update tokens.nim --- src/tokens.nim | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/tokens.nim b/src/tokens.nim index 6ef81f5..e07565c 100644 --- a/src/tokens.nim +++ b/src/tokens.nim @@ -2,20 +2,19 @@ import asyncdispatch, httpclient, times, sequtils, json, random import strutils, tables import types, consts +import std/random const maxConcurrentReqs = 5 # max requests at a time per token, to avoid race conditions maxLastUse = 1.hours # if a token is unused for 60 minutes, it expires maxAge = 2.hours + 55.minutes # tokens expire after 3 hours - failDelay = initDuration(minutes=30) + failDelay = initDuration(minutes=0) var tokenPool: seq[Token] lastFailed: Time enableLogging = false -let headers = newHttpHeaders({"authorization": auth}) - template log(str) = if enableLogging: echo "[tokens] ", str @@ -67,6 +66,9 @@ proc fetchToken(): Future[Token] {.async.} = if getTime() - lastFailed < failDelay: raise rateLimitError() + let auth = sample(bearerTokens) + let headers = newHttpHeaders({"authorization": auth}) + let client = newAsyncHttpClient(headers=headers) try: @@ -76,7 +78,7 @@ proc fetchToken(): Future[Token] {.async.} = tok = tokNode.getStr($(tokNode.getInt)) time = getTime() - return Token(tok: tok, init: time, lastUse: time) + return Token(tok: tok, bearerTok: auth, init: time, lastUse: time) except Exception as e: echo "[tokens] fetching token failed: ", e.msg if "Try again" notin e.msg: