mirror of
https://github.com/zedeus/nitter.git
synced 2025-03-04 10:01:24 +00:00
Move limited flag to be account-level
This commit is contained in:
parent
28d3ed7d9f
commit
1aa9b0dba6
2 changed files with 22 additions and 31 deletions
49
src/auth.nim
49
src/auth.nim
|
@ -32,13 +32,6 @@ template log(str: varargs[string, `$`]) =
|
||||||
proc snowflakeToEpoch(flake: int64): int64 =
|
proc snowflakeToEpoch(flake: int64): int64 =
|
||||||
int64(((flake shr 22) + 1288834974657) div 1000)
|
int64(((flake shr 22) + 1288834974657) div 1000)
|
||||||
|
|
||||||
proc hasExpired(account: GuestAccount): bool =
|
|
||||||
let
|
|
||||||
created = snowflakeToEpoch(account.id)
|
|
||||||
now = epochTime().int64
|
|
||||||
daysOld = int(now - created) div dayInSeconds
|
|
||||||
return daysOld > 30
|
|
||||||
|
|
||||||
proc getAccountPoolHealth*(): JsonNode =
|
proc getAccountPoolHealth*(): JsonNode =
|
||||||
let now = epochTime().int
|
let now = epochTime().int
|
||||||
|
|
||||||
|
@ -58,14 +51,14 @@ proc getAccountPoolHealth*(): JsonNode =
|
||||||
oldest = created
|
oldest = created
|
||||||
average += created
|
average += created
|
||||||
|
|
||||||
|
if account.limited:
|
||||||
|
limited.incl account.id
|
||||||
|
|
||||||
for api in account.apis.keys:
|
for api in account.apis.keys:
|
||||||
let
|
let
|
||||||
apiStatus = account.apis[api]
|
apiStatus = account.apis[api]
|
||||||
reqs = apiMaxReqs[api] - apiStatus.remaining
|
reqs = apiMaxReqs[api] - apiStatus.remaining
|
||||||
|
|
||||||
if apiStatus.limited:
|
|
||||||
limited.incl account.id
|
|
||||||
|
|
||||||
# no requests made with this account and endpoint since the limit reset
|
# no requests made with this account and endpoint since the limit reset
|
||||||
if apiStatus.reset < now:
|
if apiStatus.reset < now:
|
||||||
continue
|
continue
|
||||||
|
@ -103,6 +96,9 @@ proc getAccountPoolDebug*(): JsonNode =
|
||||||
"pending": account.pending,
|
"pending": account.pending,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if account.limited:
|
||||||
|
accountJson["limited"] = %true
|
||||||
|
|
||||||
for api in account.apis.keys:
|
for api in account.apis.keys:
|
||||||
let
|
let
|
||||||
apiStatus = account.apis[api]
|
apiStatus = account.apis[api]
|
||||||
|
@ -110,13 +106,11 @@ proc getAccountPoolDebug*(): JsonNode =
|
||||||
|
|
||||||
if apiStatus.reset > now.int:
|
if apiStatus.reset > now.int:
|
||||||
obj["remaining"] = %apiStatus.remaining
|
obj["remaining"] = %apiStatus.remaining
|
||||||
|
obj["reset"] = %apiStatus.reset
|
||||||
|
|
||||||
if "remaining" notin obj and not apiStatus.limited:
|
if "remaining" notin obj:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if apiStatus.limited:
|
|
||||||
obj["limited"] = %true
|
|
||||||
|
|
||||||
accountJson{"apis", $api} = obj
|
accountJson{"apis", $api} = obj
|
||||||
list[$account.id] = accountJson
|
list[$account.id] = accountJson
|
||||||
|
|
||||||
|
@ -132,14 +126,16 @@ proc isLimited(account: GuestAccount; api: Api): bool =
|
||||||
if account.isNil:
|
if account.isNil:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
if account.limited and api != Api.userTweets:
|
||||||
|
if (epochTime().int - account.limitedAt) > dayInSeconds:
|
||||||
|
account.limited = false
|
||||||
|
log "resetting limit: ", account.id
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
if api in account.apis:
|
if api in account.apis:
|
||||||
let limit = account.apis[api]
|
let limit = account.apis[api]
|
||||||
|
return limit.remaining <= 10 and limit.reset > epochTime().int
|
||||||
if limit.limited and (epochTime().int - limit.limitedAt) > dayInSeconds:
|
|
||||||
account.apis[api].limited = false
|
|
||||||
log "resetting limit, api: ", api, ", id: ", account.id
|
|
||||||
|
|
||||||
return limit.limited or (limit.remaining <= 10 and limit.reset > epochTime().int)
|
|
||||||
else:
|
else:
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
@ -148,7 +144,7 @@ proc isReady(account: GuestAccount; api: Api): bool =
|
||||||
|
|
||||||
proc invalidate*(account: var GuestAccount) =
|
proc invalidate*(account: var GuestAccount) =
|
||||||
if account.isNil: return
|
if account.isNil: return
|
||||||
log "invalidating expired account: ", account.id
|
log "invalidating: ", account.id
|
||||||
|
|
||||||
# TODO: This isn't sufficient, but it works for now
|
# TODO: This isn't sufficient, but it works for now
|
||||||
let idx = accountPool.find(account)
|
let idx = accountPool.find(account)
|
||||||
|
@ -171,9 +167,9 @@ proc getGuestAccount*(api: Api): Future[GuestAccount] {.async.} =
|
||||||
raise noAccountsError()
|
raise noAccountsError()
|
||||||
|
|
||||||
proc setLimited*(account: GuestAccount; api: Api) =
|
proc setLimited*(account: GuestAccount; api: Api) =
|
||||||
account.apis[api].limited = true
|
account.limited = true
|
||||||
account.apis[api].limitedAt = epochTime().int
|
account.limitedAt = epochTime().int
|
||||||
log "rate limited, api: ", api, ", reqs left: ", account.apis[api].remaining, ", id: ", account.id
|
log "rate limited by api: ", api, ", reqs left: ", account.apis[api].remaining, ", id: ", account.id
|
||||||
|
|
||||||
proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
|
proc setRateLimit*(account: GuestAccount; api: Api; remaining, reset: int) =
|
||||||
# avoid undefined behavior in race conditions
|
# avoid undefined behavior in race conditions
|
||||||
|
@ -203,9 +199,4 @@ proc initAccountPool*(cfg: Config; path: string) =
|
||||||
echo "[accounts] ERROR: ", path, " not found. This file is required to authenticate API requests."
|
echo "[accounts] ERROR: ", path, " not found. This file is required to authenticate API requests."
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
let accountsPrePurge = accountPool.len
|
|
||||||
#accountPool.keepItIf(not it.hasExpired)
|
|
||||||
|
|
||||||
log "Successfully added ", accountPool.len, " valid accounts."
|
log "Successfully added ", accountPool.len, " valid accounts."
|
||||||
if accountsPrePurge > accountPool.len:
|
|
||||||
log "Purged ", accountsPrePurge - accountPool.len, " expired accounts."
|
|
||||||
|
|
|
@ -30,14 +30,14 @@ type
|
||||||
RateLimit* = object
|
RateLimit* = object
|
||||||
remaining*: int
|
remaining*: int
|
||||||
reset*: int
|
reset*: int
|
||||||
limited*: bool
|
|
||||||
limitedAt*: int
|
|
||||||
|
|
||||||
GuestAccount* = ref object
|
GuestAccount* = ref object
|
||||||
id*: int64
|
id*: int64
|
||||||
oauthToken*: string
|
oauthToken*: string
|
||||||
oauthSecret*: string
|
oauthSecret*: string
|
||||||
pending*: int
|
pending*: int
|
||||||
|
limited*: bool
|
||||||
|
limitedAt*: int
|
||||||
apis*: Table[Api, RateLimit]
|
apis*: Table[Api, RateLimit]
|
||||||
|
|
||||||
Error* = enum
|
Error* = enum
|
||||||
|
|
Loading…
Reference in a new issue