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]
name = "torn-key-pool"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
license = "MIT"
repository = "https://github.com/TotallyNot/torn-api.rs.git"

View file

@ -14,29 +14,17 @@ pub enum PgStorageError {
Unavailable(KeyDomain),
}
#[derive(Debug, Clone, FromRow, Eq)]
#[derive(Debug, Clone, FromRow)]
pub struct PgKey {
pub id: i32,
pub key: String,
pub uses: i16,
}
impl Ord for PgKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.uses.cmp(&self.uses)
}
}
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
}
#[derive(Debug, Clone, FromRow)]
pub struct PgKeyPoolStorage {
pool: PgPool,
limit: i16,
}
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 {
pub fn new(pool: PgPool, limit: i16) -> Self {
Self { pool, limit }
@ -206,7 +188,7 @@ impl KeyPoolStorage for PgKeyPoolStorage {
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 (max, rest) = keys.split_last_mut().unwrap();
@ -287,7 +269,6 @@ impl KeyPoolStorage for PgKeyPoolStorage {
mod test {
use std::sync::{Arc, Once};
use sqlx::Row;
use tokio::test;
use super::*;
@ -303,7 +284,7 @@ mod test {
.await
.unwrap();
sqlx::query("update api_keys set uses=0")
sqlx::query("update api_keys set uses=id")
.execute(&pool)
.await
.unwrap();
@ -332,11 +313,6 @@ mod test {
#[test]
async fn test_concurrent() {
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
.acquire_many_keys(KeyDomain::Public, 30)
@ -344,13 +320,5 @@ mod test {
.unwrap();
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);
}
}