From b64903895bed4d530edf038d0ff9cb658f74db89 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Sat, 18 Mar 2023 10:49:00 -0700 Subject: [PATCH] use thread::available_parallelism instead of num_cpus crate Signed-off-by: Andrew Whitehead --- askar-storage/Cargo.toml | 3 +-- askar-storage/src/backend/sqlite/provision.rs | 20 ++++++++++++------- askar-storage/tests/migration.rs | 4 +++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/askar-storage/Cargo.toml b/askar-storage/Cargo.toml index c29ede95..439efc80 100644 --- a/askar-storage/Cargo.toml +++ b/askar-storage/Cargo.toml @@ -22,7 +22,7 @@ all_backends = ["any", "postgres", "sqlite"] any = [] migration = ["rmp-serde", "sqlx/macros"] postgres = ["sqlx", "sqlx/postgres", "sqlx/tls"] -sqlite = ["num_cpus", "sqlx", "sqlx/sqlite"] +sqlite = ["sqlx", "sqlx/sqlite"] pg_test = ["postgres"] [dev-dependencies] @@ -41,7 +41,6 @@ hex = "0.4" hmac = "0.12" itertools = "0.10" log = { version = "0.4", optional = true } -num_cpus = { version = "1.0", optional = true } once_cell = "1.5" percent-encoding = "2.0" rmp-serde = { version= "1.1", optional = true } diff --git a/askar-storage/src/backend/sqlite/provision.rs b/askar-storage/src/backend/sqlite/provision.rs index a0d9768a..de105e70 100644 --- a/askar-storage/src/backend/sqlite/provision.rs +++ b/askar-storage/src/backend/sqlite/provision.rs @@ -1,5 +1,6 @@ use std::{ - borrow::Cow, fs::remove_file, io::ErrorKind as IoErrorKind, str::FromStr, time::Duration, + borrow::Cow, fs::remove_file, io::ErrorKind as IoErrorKind, str::FromStr, + thread::available_parallelism, time::Duration, }; use sqlx::{ @@ -22,9 +23,9 @@ use crate::{ protect::{KeyCache, PassKey, StoreKeyMethod, StoreKeyReference}, }; -const DEFAULT_MIN_CONNECTIONS: u32 = 1; -const DEFAULT_LOWER_MAX_CONNECTIONS: u32 = 2; -const DEFAULT_UPPER_MAX_CONNECTIONS: u32 = 8; +const DEFAULT_MIN_CONNECTIONS: usize = 1; +const DEFAULT_LOWER_MAX_CONNECTIONS: usize = 2; +const DEFAULT_UPPER_MAX_CONNECTIONS: usize = 8; const DEFAULT_BUSY_TIMEOUT: Duration = Duration::from_secs(5); const DEFAULT_JOURNAL_MODE: SqliteJournalMode = SqliteJournalMode::Wal; const DEFAULT_LOCKING_MODE: SqliteLockingMode = SqliteLockingMode::Normal; @@ -69,16 +70,21 @@ impl SqliteStoreOptions { .parse() .map_err(err_map!(Input, "Error parsing 'max_connections' parameter"))? } else { - (num_cpus::get() as u32) + available_parallelism() + .map_err(err_map!( + Unexpected, + "Error determining available parallelism" + ))? + .get() .max(DEFAULT_LOWER_MAX_CONNECTIONS) - .min(DEFAULT_UPPER_MAX_CONNECTIONS) + .min(DEFAULT_UPPER_MAX_CONNECTIONS) as u32 }; let min_connections = if let Some(min_conn) = opts.query.remove("min_connections") { min_conn .parse() .map_err(err_map!(Input, "Error parsing 'min_connections' parameter"))? } else { - DEFAULT_MIN_CONNECTIONS + DEFAULT_MIN_CONNECTIONS as u32 }; let journal_mode = if let Some(mode) = opts.query.remove("journal_mode") { SqliteJournalMode::from_str(&mode) diff --git a/askar-storage/tests/migration.rs b/askar-storage/tests/migration.rs index 32df1411..102ee761 100644 --- a/askar-storage/tests/migration.rs +++ b/askar-storage/tests/migration.rs @@ -1,3 +1,5 @@ +#![cfg(all(feature = "sqlite", feature = "migration"))] + use std::path::PathBuf; use askar_storage::future::block_on; @@ -29,7 +31,7 @@ fn prepare_db() { } #[test] -fn test_migration() { +fn test_sqlite_migration() { prepare_db(); let res = block_on(async {