fixed key usage getting stuck on the first key

This commit is contained in:
TotallyNot 2023-02-27 21:39:13 +01:00
parent 042af5acf2
commit 44e6f5370d
2 changed files with 23 additions and 3 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "torn-key-pool"
version = "0.5.6"
version = "0.5.7"
edition = "2021"
authors = ["Pyrit [2111649]"]
license = "MIT"

View file

@ -169,6 +169,7 @@ where
.execute(&mut tx)
.await?;
// TODO: improve query
let key = sqlx::query_as(&indoc::formatdoc!(
r#"
with key as (
@ -183,9 +184,9 @@ where
where last_used >= date_trunc('minute', now())
and (cooldown is null or now() >= cooldown)
and domains @> $1
order by uses asc
order by uses asc limit 1
)
limit 1
order by uses asc limit 1
)
update api_keys set
uses = key.uses + 1,
@ -764,6 +765,25 @@ pub(crate) mod test {
}
}
#[test]
async fn uses_spread() {
let (storage, _) = setup().await;
storage
.store_key(1, "ABC".to_owned(), vec![Domain::All])
.await
.unwrap();
for _ in 0..10 {
_ = storage.acquire_key(Domain::All).await.unwrap();
}
let keys = storage.read_user_keys(1).await.unwrap();
assert_eq!(keys.len(), 2);
for key in keys {
assert_eq!(key.uses, 5);
}
}
#[test]
async fn test_flag_key_one() {
let (storage, key) = setup().await;