Skip to content

Commit

Permalink
use thread::available_parallelism instead of num_cpus crate
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
  • Loading branch information
andrewwhitehead committed Mar 30, 2023
1 parent 0c00021 commit b649038
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 1 addition & 2 deletions askar-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 }
Expand Down
20 changes: 13 additions & 7 deletions askar-storage/src/backend/sqlite/provision.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion askar-storage/tests/migration.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(all(feature = "sqlite", feature = "migration"))]

use std::path::PathBuf;

use askar_storage::future::block_on;
Expand Down Expand Up @@ -29,7 +31,7 @@ fn prepare_db() {
}

#[test]
fn test_migration() {
fn test_sqlite_migration() {
prepare_db();

let res = block_on(async {
Expand Down

0 comments on commit b649038

Please sign in to comment.