Skip to content

Commit 1909169

Browse files
committed
Allow manually specifying the number of threads
1 parent f7d6df0 commit 1909169

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "Receives syslog messages from nginx and stores them in a PostgreS
44
authors = ["Dennis Schubert <[email protected]>"]
55
repository = "https://github.com/denschub/nginx-syslog-postgresql-bridge"
66
license = "MIT"
7-
version = "2.0.3"
7+
version = "2.1.0"
88
edition = "2021"
99

1010
[profile.release]
@@ -14,7 +14,6 @@ lto = "fat"
1414
anyhow = "1"
1515
chrono = { version = "0.4", features = ["serde"] }
1616
clap = { version = "4", features = ["cargo", "derive", "env", "wrap_help"] }
17-
num_cpus = "1"
1817
serde = { version = "1", features = ["derive"] }
1918
serde_json = "1"
2019
sqlx = { version = "0.8", features = [

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.0
2+
3+
This version introduces a new setting, `--threads`/`THREADS` that allows limiting the number of worker threads and the size of the database connection pool. If this flag is not set, the number of available CPU cores will be used, which matches the current behavior.
4+
15
# 2.0.3
26

37
This version does not contain any functional changes. It only updates third-party dependencies.

src/main.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ use sqlx::postgres::PgPoolOptions;
44

55
use nginx_syslog_postgres_bridge::{Bridge, Settings};
66

7-
#[tokio::main]
8-
async fn main() -> Result<()> {
7+
fn main() -> Result<()> {
98
let settings = Settings::parse();
9+
10+
let mut rt = tokio::runtime::Builder::new_multi_thread();
11+
if let Some(threads) = settings.threads {
12+
rt.worker_threads(threads);
13+
}
14+
15+
rt.enable_all()
16+
.build()?
17+
.block_on(async { run(settings).await })
18+
}
19+
20+
async fn run(settings: Settings) -> Result<()> {
1021
tracing_subscriber::fmt::init();
1122

1223
let udp_socket = tokio::net::UdpSocket::bind(settings.listen_addr).await?;
1324
let db_pool = PgPoolOptions::new()
1425
.max_connections(
15-
num_cpus::get()
26+
tokio::runtime::Handle::current()
27+
.metrics()
28+
.num_workers()
1629
.try_into()
17-
.expect("number of CPU cores should fit into an u32"),
30+
.expect("num_workers to be less than 2^32"),
1831
)
1932
.connect_with(settings.database_url)
2033
.await?;

src/settings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ pub struct Settings {
1717
/// Maximum number of messages in the processing queue
1818
#[clap(long, short, env = "QUEUE_SIZE", default_value = "10000")]
1919
pub queue_size: usize,
20+
21+
/// Limits the number of threads used - defaults to the number of CPU cores
22+
#[clap(long, env = "THREADS")]
23+
pub threads: Option<usize>,
2024
}

0 commit comments

Comments
 (0)