fix sign error causing capicity overflow at runtime

This commit is contained in:
TotallyNot 2022-09-19 03:56:21 +02:00
parent 83b6253ede
commit 768c63b731
2 changed files with 8 additions and 40 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "torn-key-pool" name = "torn-key-pool"
version = "0.4.1" version = "0.4.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/TotallyNot/torn-api.rs.git" repository = "https://github.com/TotallyNot/torn-api.rs.git"

View file

@ -14,29 +14,17 @@ pub enum PgStorageError {
Unavailable(KeyDomain), Unavailable(KeyDomain),
} }
#[derive(Debug, Clone, FromRow, Eq)] #[derive(Debug, Clone, FromRow)]
pub struct PgKey { pub struct PgKey {
pub id: i32, pub id: i32,
pub key: String, pub key: String,
pub uses: i16, pub uses: i16,
} }
impl Ord for PgKey { #[derive(Debug, Clone, FromRow)]
fn cmp(&self, other: &Self) -> std::cmp::Ordering { pub struct PgKeyPoolStorage {
other.uses.cmp(&self.uses) pool: PgPool,
} limit: i16,
}
impl PartialOrd for PgKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for PgKey {
fn eq(&self, other: &Self) -> bool {
self.uses == other.uses
}
} }
impl ApiKey for PgKey { impl ApiKey for PgKey {
@ -45,12 +33,6 @@ impl ApiKey for PgKey {
} }
} }
#[derive(Debug, Clone, FromRow)]
pub struct PgKeyPoolStorage {
pool: PgPool,
limit: i16,
}
impl PgKeyPoolStorage { impl PgKeyPoolStorage {
pub fn new(pool: PgPool, limit: i16) -> Self { pub fn new(pool: PgPool, limit: i16) -> Self {
Self { pool, limit } Self { pool, limit }
@ -206,7 +188,7 @@ impl KeyPoolStorage for PgKeyPoolStorage {
return Ok(Err(PgStorageError::Unavailable(domain))); return Ok(Err(PgStorageError::Unavailable(domain)));
} }
keys.sort_unstable(); keys.sort_unstable_by(|k1, k2| k1.uses.cmp(&k2.uses));
let mut result = Vec::with_capacity(number as usize); let mut result = Vec::with_capacity(number as usize);
let (max, rest) = keys.split_last_mut().unwrap(); let (max, rest) = keys.split_last_mut().unwrap();
@ -287,7 +269,6 @@ impl KeyPoolStorage for PgKeyPoolStorage {
mod test { mod test {
use std::sync::{Arc, Once}; use std::sync::{Arc, Once};
use sqlx::Row;
use tokio::test; use tokio::test;
use super::*; use super::*;
@ -303,7 +284,7 @@ mod test {
.await .await
.unwrap(); .unwrap();
sqlx::query("update api_keys set uses=0") sqlx::query("update api_keys set uses=id")
.execute(&pool) .execute(&pool)
.await .await
.unwrap(); .unwrap();
@ -332,11 +313,6 @@ mod test {
#[test] #[test]
async fn test_concurrent() { async fn test_concurrent() {
let storage = Arc::new(setup().await); let storage = Arc::new(setup().await);
let before: i64 = sqlx::query("select sum(uses) as uses from api_keys")
.fetch_one(&storage.pool)
.await
.unwrap()
.get("uses");
let keys = storage let keys = storage
.acquire_many_keys(KeyDomain::Public, 30) .acquire_many_keys(KeyDomain::Public, 30)
@ -344,13 +320,5 @@ mod test {
.unwrap(); .unwrap();
assert_eq!(keys.len(), 30); assert_eq!(keys.len(), 30);
let after: i64 = sqlx::query("select sum(uses) as uses from api_keys")
.fetch_one(&storage.pool)
.await
.unwrap()
.get("uses");
assert_eq!(after, before + 30);
} }
} }