Purge expired accounts after parsing

This commit is contained in:
Zed 2023-11-01 05:09:44 +00:00
parent e1838e0933
commit 209f453b79
1 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,5 @@
#SPDX-License-Identifier: AGPL-3.0-only #SPDX-License-Identifier: AGPL-3.0-only
import std/[asyncdispatch, times, json, random, strutils, tables, packedsets, os] import std/[asyncdispatch, times, json, random, sequtils, strutils, tables, packedsets, os]
import types import types
import experimental/parser/guestaccount import experimental/parser/guestaccount
@ -30,6 +30,16 @@ var
template log(str: varargs[string, `$`]) = template log(str: varargs[string, `$`]) =
if enableLogging: echo "[accounts] ", str.join("") if enableLogging: echo "[accounts] ", str.join("")
proc snowflakeToEpoch(flake: int64): int64 =
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 (24 * 60 * 60)
return daysOld > 30
proc getAccountPoolHealth*(): JsonNode = proc getAccountPoolHealth*(): JsonNode =
let now = epochTime().int let now = epochTime().int
@ -42,9 +52,7 @@ proc getAccountPoolHealth*(): JsonNode =
average = 0'i64 average = 0'i64
for account in accountPool: for account in accountPool:
# Twitter snowflake conversion let created = snowflakeToEpoch(account.id)
let created = int64(((account.id shr 22) + 1288834974657) div 1000)
if created > newest: if created > newest:
newest = created newest = created
if created < oldest: if created < oldest:
@ -188,3 +196,10 @@ proc initAccountPool*(cfg: Config; path: string) =
else: else:
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."
if accountsPrePurge > accountPool.len:
log "Purged ", accountsPrePurge - accountPool.len, " expired accounts."