fixed key usage getting stuck on the first key
This commit is contained in:
parent
042af5acf2
commit
44e6f5370d
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "torn-key-pool"
|
name = "torn-key-pool"
|
||||||
version = "0.5.6"
|
version = "0.5.7"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Pyrit [2111649]"]
|
authors = ["Pyrit [2111649]"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -169,6 +169,7 @@ where
|
||||||
.execute(&mut tx)
|
.execute(&mut tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
// TODO: improve query
|
||||||
let key = sqlx::query_as(&indoc::formatdoc!(
|
let key = sqlx::query_as(&indoc::formatdoc!(
|
||||||
r#"
|
r#"
|
||||||
with key as (
|
with key as (
|
||||||
|
@ -183,9 +184,9 @@ where
|
||||||
where last_used >= date_trunc('minute', now())
|
where last_used >= date_trunc('minute', now())
|
||||||
and (cooldown is null or now() >= cooldown)
|
and (cooldown is null or now() >= cooldown)
|
||||||
and domains @> $1
|
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
|
update api_keys set
|
||||||
uses = key.uses + 1,
|
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]
|
#[test]
|
||||||
async fn test_flag_key_one() {
|
async fn test_flag_key_one() {
|
||||||
let (storage, key) = setup().await;
|
let (storage, key) = setup().await;
|
||||||
|
|
Loading…
Reference in a new issue