Skip to content

Commit

Permalink
Merge torrust#959: dev: remove async trait dep
Browse files Browse the repository at this point in the history
64850af dev: remove async trait dep (Cameron Garnham)

Pull request description:

  Remove Unneeded Async Trait Dependency

ACKs for top commit:
  da2ce7:
    ACK 64850af

Tree-SHA512: 54a83db83d11b0829c80e3daf4dd5776bd6b4e4709553645ffe742dcdba458c24dfa195e2c1b8abac1a59152144aa5419698d9edbf25af657f1e6c85943fda78
  • Loading branch information
da2ce7 committed Jul 13, 2024
2 parents f2629ce + 64850af commit 8ff668f
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 183 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ version = "3.0.0-alpha.12-develop"
[dependencies]
anyhow = "1"
aquatic_udp_protocol = "0"
async-trait = "0"
axum = { version = "0", features = ["macros"] }
axum-client-ip = "0"
axum-extra = { version = "0", features = ["query"] }
Expand Down
28 changes: 13 additions & 15 deletions src/core/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub mod sqlite;

use std::marker::PhantomData;

use async_trait::async_trait;
use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::PersistentTorrents;

Expand Down Expand Up @@ -79,7 +78,6 @@ where
}

/// The persistence trait. It contains all the methods to interact with the database.
#[async_trait]
pub trait Database: Sync + Send {
/// It instantiates a new database driver.
///
Expand Down Expand Up @@ -126,7 +124,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error>;
fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error>;

/// It saves the torrent metrics data into the database.
///
Expand All @@ -135,7 +133,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to save.
async fn save_persistent_torrent(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error>;
fn save_persistent_torrent(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error>;

// Whitelist

Expand All @@ -146,7 +144,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error>;
fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error>;

/// It checks if the torrent is whitelisted.
///
Expand All @@ -157,7 +155,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn get_info_hash_from_whitelist(&self, info_hash: &InfoHash) -> Result<Option<InfoHash>, Error>;
fn get_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<Option<InfoHash>, Error>;

/// It adds the torrent to the whitelist.
///
Expand All @@ -166,7 +164,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to save.
async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;

/// It checks if the torrent is whitelisted.
///
Expand All @@ -175,8 +173,8 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> Result<bool, Error> {
Ok(self.get_info_hash_from_whitelist(info_hash).await?.is_some())
fn is_info_hash_whitelisted(&self, info_hash: InfoHash) -> Result<bool, Error> {
Ok(self.get_info_hash_from_whitelist(info_hash)?.is_some())
}

/// It removes the torrent from the whitelist.
Expand All @@ -186,7 +184,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to save.
async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;
fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error>;

// Authentication keys

Expand All @@ -197,19 +195,19 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error>;
fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error>;

/// It gets an expiring authentication key from the database.
///
/// It returns `Some(ExpiringKey)` if a [`ExpiringKey`](crate::core::auth::ExpiringKey)
/// with the input [`Key`](crate::core::auth::Key) exists, `None` otherwise.
/// with the input [`Key`] exists, `None` otherwise.
///
/// # Context: Authentication Keys
///
/// # Errors
///
/// Will return `Err` if unable to load.
async fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error>;
fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error>;

/// It adds an expiring authentication key to the database.
///
Expand All @@ -218,7 +216,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to save.
async fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error>;
fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error>;

/// It removes an expiring authentication key from the database.
///
Expand All @@ -227,5 +225,5 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error>;
fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error>;
}
22 changes: 10 additions & 12 deletions src/core/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::str::FromStr;
use std::time::Duration;

use async_trait::async_trait;
use r2d2::Pool;
use r2d2_mysql::mysql::prelude::Queryable;
use r2d2_mysql::mysql::{params, Opts, OptsBuilder};
Expand All @@ -22,7 +21,6 @@ pub struct Mysql {
pool: Pool<MySqlConnectionManager>,
}

#[async_trait]
impl Database for Mysql {
/// It instantiates a new `MySQL` database driver.
///
Expand Down Expand Up @@ -106,7 +104,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::load_persistent_torrents`](crate::core::databases::Database::load_persistent_torrents).
async fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error> {
fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let torrents = conn.query_map(
Expand All @@ -121,7 +119,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::load_keys`](crate::core::databases::Database::load_keys).
async fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error> {
fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let keys = conn.query_map(
Expand All @@ -136,7 +134,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::load_whitelist`](crate::core::databases::Database::load_whitelist).
async fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error> {
fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let info_hashes = conn.query_map("SELECT info_hash FROM whitelist", |info_hash: String| {
Expand All @@ -147,7 +145,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::save_persistent_torrent`](crate::core::databases::Database::save_persistent_torrent).
async fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> {
fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> {
const COMMAND : &str = "INSERT INTO torrents (info_hash, completed) VALUES (:info_hash_str, :completed) ON DUPLICATE KEY UPDATE completed = VALUES(completed)";

let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;
Expand All @@ -160,7 +158,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::get_info_hash_from_whitelist`](crate::core::databases::Database::get_info_hash_from_whitelist).
async fn get_info_hash_from_whitelist(&self, info_hash: &InfoHash) -> Result<Option<InfoHash>, Error> {
fn get_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<Option<InfoHash>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let select = conn.exec_first::<String, _, _>(
Expand All @@ -174,7 +172,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::add_info_hash_to_whitelist`](crate::core::databases::Database::add_info_hash_to_whitelist).
async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let info_hash_str = info_hash.to_string();
Expand All @@ -188,7 +186,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::remove_info_hash_from_whitelist`](crate::core::databases::Database::remove_info_hash_from_whitelist).
async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let info_hash = info_hash.to_string();
Expand All @@ -199,7 +197,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::get_key_from_keys`](crate::core::databases::Database::get_key_from_keys).
async fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error> {
fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let query = conn.exec_first::<(String, i64), _, _>(
Expand All @@ -216,7 +214,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::add_key_to_keys`](crate::core::databases::Database::add_key_to_keys).
async fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error> {
fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let key = auth_key.key.to_string();
Expand All @@ -231,7 +229,7 @@ impl Database for Mysql {
}

/// Refer to [`databases::Database::remove_key_from_keys`](crate::core::databases::Database::remove_key_from_keys).
async fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
let mut conn = self.pool.get().map_err(|e| (e, DRIVER))?;

conn.exec_drop("DELETE FROM `keys` WHERE key = :key", params! { "key" => key.to_string() })?;
Expand Down
22 changes: 10 additions & 12 deletions src/core/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::panic::Location;
use std::str::FromStr;

use async_trait::async_trait;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use torrust_tracker_primitives::info_hash::InfoHash;
Expand All @@ -18,7 +17,6 @@ pub struct Sqlite {
pool: Pool<SqliteConnectionManager>,
}

#[async_trait]
impl Database for Sqlite {
/// It instantiates a new `SQLite3` database driver.
///
Expand Down Expand Up @@ -90,7 +88,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::load_persistent_torrents`](crate::core::databases::Database::load_persistent_torrents).
async fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error> {
fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let mut stmt = conn.prepare("SELECT info_hash, completed FROM torrents")?;
Expand All @@ -106,7 +104,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::load_keys`](crate::core::databases::Database::load_keys).
async fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error> {
fn load_keys(&self) -> Result<Vec<auth::ExpiringKey>, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let mut stmt = conn.prepare("SELECT key, valid_until FROM keys")?;
Expand All @@ -127,7 +125,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::load_whitelist`](crate::core::databases::Database::load_whitelist).
async fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error> {
fn load_whitelist(&self) -> Result<Vec<InfoHash>, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let mut stmt = conn.prepare("SELECT info_hash FROM whitelist")?;
Expand All @@ -144,7 +142,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::save_persistent_torrent`](crate::core::databases::Database::save_persistent_torrent).
async fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> {
fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let insert = conn.execute(
Expand All @@ -163,7 +161,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::get_info_hash_from_whitelist`](crate::core::databases::Database::get_info_hash_from_whitelist).
async fn get_info_hash_from_whitelist(&self, info_hash: &InfoHash) -> Result<Option<InfoHash>, Error> {
fn get_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<Option<InfoHash>, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let mut stmt = conn.prepare("SELECT info_hash FROM whitelist WHERE info_hash = ?")?;
Expand All @@ -176,7 +174,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::add_info_hash_to_whitelist`](crate::core::databases::Database::add_info_hash_to_whitelist).
async fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
fn add_info_hash_to_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let insert = conn.execute("INSERT INTO whitelist (info_hash) VALUES (?)", [info_hash.to_string()])?;
Expand All @@ -192,7 +190,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::remove_info_hash_from_whitelist`](crate::core::databases::Database::remove_info_hash_from_whitelist).
async fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
fn remove_info_hash_from_whitelist(&self, info_hash: InfoHash) -> Result<usize, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let deleted = conn.execute("DELETE FROM whitelist WHERE info_hash = ?", [info_hash.to_string()])?;
Expand All @@ -210,7 +208,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::get_key_from_keys`](crate::core::databases::Database::get_key_from_keys).
async fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error> {
fn get_key_from_keys(&self, key: &Key) -> Result<Option<auth::ExpiringKey>, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let mut stmt = conn.prepare("SELECT key, valid_until FROM keys WHERE key = ?")?;
Expand All @@ -230,7 +228,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::add_key_to_keys`](crate::core::databases::Database::add_key_to_keys).
async fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error> {
fn add_key_to_keys(&self, auth_key: &auth::ExpiringKey) -> Result<usize, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let insert = conn.execute(
Expand All @@ -249,7 +247,7 @@ impl Database for Sqlite {
}

/// Refer to [`databases::Database::remove_key_from_keys`](crate::core::databases::Database::remove_key_from_keys).
async fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
fn remove_key_from_keys(&self, key: &Key) -> Result<usize, Error> {
let conn = self.pool.get().map_err(|e| (e, DRIVER))?;

let deleted = conn.execute("DELETE FROM keys WHERE key = ?", [key.to_string()])?;
Expand Down
Loading

0 comments on commit 8ff668f

Please sign in to comment.