2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2023-08-18 22:25:14 +00:00
|
|
|
import asyncdispatch, times, json, random, strutils, tables
|
|
|
|
import types
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
# max requests at a time per account to avoid race conditions
|
|
|
|
const maxConcurrentReqs = 5
|
2021-01-18 06:47:51 +00:00
|
|
|
|
2020-07-09 07:18:14 +00:00
|
|
|
var
|
2023-08-18 22:25:14 +00:00
|
|
|
accountPool: seq[GuestAccount]
|
2022-06-05 19:47:25 +00:00
|
|
|
enableLogging = false
|
|
|
|
|
|
|
|
template log(str) =
|
2023-08-18 22:25:14 +00:00
|
|
|
if enableLogging: echo "[accounts] ", str
|
2021-01-13 13:32:26 +00:00
|
|
|
|
2022-01-05 23:42:18 +00:00
|
|
|
proc getPoolJson*(): JsonNode =
|
|
|
|
var
|
|
|
|
list = newJObject()
|
|
|
|
totalReqs = 0
|
|
|
|
totalPending = 0
|
|
|
|
reqsPerApi: Table[string, int]
|
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
for account in accountPool:
|
|
|
|
totalPending.inc(account.pending)
|
|
|
|
list[account.id] = %*{
|
2022-01-05 21:49:16 +00:00
|
|
|
"apis": newJObject(),
|
2023-08-18 22:25:14 +00:00
|
|
|
"pending": account.pending,
|
2022-01-05 21:49:16 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
for api in account.apis.keys:
|
|
|
|
list[account.id]["apis"][$api] = %account.apis[api].remaining
|
2022-01-05 23:42:18 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
maxReqs =
|
|
|
|
case api
|
2023-08-18 22:25:14 +00:00
|
|
|
of Api.search: 50
|
2023-07-22 02:06:04 +00:00
|
|
|
of Api.photoRail: 180
|
2023-08-18 22:25:14 +00:00
|
|
|
of Api.userTweets, Api.userTweetsAndReplies, Api.userMedia,
|
|
|
|
Api.userRestId, Api.userScreenName,
|
|
|
|
Api.tweetDetail, Api.tweetResult,
|
|
|
|
Api.list, Api.listTweets, Api.listMembers, Api.listBySlug: 500
|
|
|
|
reqs = maxReqs - account.apis[api].remaining
|
2022-01-05 23:42:18 +00:00
|
|
|
|
|
|
|
reqsPerApi[$api] = reqsPerApi.getOrDefault($api, 0) + reqs
|
|
|
|
totalReqs.inc(reqs)
|
|
|
|
|
|
|
|
return %*{
|
2023-08-18 22:25:14 +00:00
|
|
|
"amount": accountPool.len,
|
2022-01-05 23:42:18 +00:00
|
|
|
"requests": totalReqs,
|
|
|
|
"pending": totalPending,
|
|
|
|
"apis": reqsPerApi,
|
2023-08-18 22:25:14 +00:00
|
|
|
"accounts": list
|
2022-01-05 23:42:18 +00:00
|
|
|
}
|
2021-01-13 13:32:26 +00:00
|
|
|
|
|
|
|
proc rateLimitError*(): ref RateLimitError =
|
2022-01-05 21:48:45 +00:00
|
|
|
newException(RateLimitError, "rate limited")
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc isLimited(account: GuestAccount; api: Api): bool =
|
|
|
|
if account.isNil:
|
2022-01-05 21:48:45 +00:00
|
|
|
return true
|
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
if api in account.apis:
|
|
|
|
let limit = account.apis[api]
|
2022-01-05 23:19:09 +00:00
|
|
|
return (limit.remaining <= 10 and limit.reset > epochTime().int)
|
2022-01-05 21:48:45 +00:00
|
|
|
else:
|
|
|
|
return false
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc isReady(account: GuestAccount; api: Api): bool =
|
|
|
|
not (account.isNil or account.pending > maxConcurrentReqs or account.isLimited(api))
|
2022-01-05 22:38:46 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc release*(account: GuestAccount; used=false; invalid=false) =
|
|
|
|
if account.isNil: return
|
|
|
|
if invalid:
|
|
|
|
log "discarding invalid account: " & account.id
|
2022-06-05 19:47:25 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
let idx = accountPool.find(account)
|
|
|
|
if idx > -1: accountPool.delete(idx)
|
2022-01-05 22:38:46 +00:00
|
|
|
elif used:
|
2023-08-18 22:25:14 +00:00
|
|
|
dec account.pending
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc getGuestAccount*(api: Api): Future[GuestAccount] {.async.} =
|
|
|
|
for i in 0 ..< accountPool.len:
|
2022-01-05 22:38:46 +00:00
|
|
|
if result.isReady(api): break
|
2021-01-18 06:47:51 +00:00
|
|
|
release(result)
|
2023-08-18 22:25:14 +00:00
|
|
|
result = accountPool.sample()
|
2021-01-13 13:32:26 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
if not result.isNil and result.isReady(api):
|
2022-01-05 22:38:46 +00:00
|
|
|
inc result.pending
|
|
|
|
else:
|
2023-08-18 22:25:14 +00:00
|
|
|
log "no accounts available for API: " & $api
|
2021-01-13 13:32:26 +00:00
|
|
|
raise rateLimitError()
|
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
|
2022-01-05 22:38:46 +00:00
|
|
|
# avoid undefined behavior in race conditions
|
2023-08-18 22:25:14 +00:00
|
|
|
if api in account.apis:
|
|
|
|
let limit = account.apis[api]
|
2022-01-05 22:38:46 +00:00
|
|
|
if limit.reset >= reset and limit.remaining < remaining:
|
|
|
|
return
|
2023-08-18 22:25:14 +00:00
|
|
|
if limit.reset == reset and limit.remaining >= remaining:
|
|
|
|
account.apis[api].remaining = remaining
|
|
|
|
return
|
2022-01-05 22:38:46 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
account.apis[api] = RateLimit(remaining: remaining, reset: reset)
|
2020-06-01 00:16:24 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
proc initAccountPool*(cfg: Config; accounts: JsonNode) =
|
2022-06-05 19:47:25 +00:00
|
|
|
enableLogging = cfg.enableDebug
|
2020-11-07 20:31:03 +00:00
|
|
|
|
2023-08-18 22:25:14 +00:00
|
|
|
for account in accounts:
|
|
|
|
accountPool.add GuestAccount(
|
|
|
|
id: account{"user", "id_str"}.getStr,
|
|
|
|
oauthToken: account{"oauth_token"}.getStr,
|
|
|
|
oauthSecret: account{"oauth_token_secret"}.getStr,
|
|
|
|
)
|