Skip to content

Commit

Permalink
server: introduced worker_rlimit_nofile (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaobo Liu <cppcoffee@gmail.com>
  • Loading branch information
cppcoffee authored Jan 4, 2024
1 parent 1859d56 commit 15a7844
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing-subscriber = { version = "0.3", features = ["std", "fmt", "env-filter"]
humantime = "2.1"

signal-hook = { version = "0.3", features = ["iterator", "extended-siginfo"] }
rlimit = "0.10"

# mallocator
tikv-jemallocator = "0.5"
3 changes: 2 additions & 1 deletion server/config/netguard.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ queue_start = 0
queue_count = 2

worker_priority = -20
worker_rlimit_nofile = 200000

# TODO: performance tuning
# cpu_affinity = 0xffffffff
Expand All @@ -25,7 +26,7 @@ key = ".netguard/rsa.pub"
allow_skew = 60

[log]
path = "/tmp/error.log"
path = "logs/error.log"
level = "error"

[filter]
Expand Down
1 change: 1 addition & 0 deletions server/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn main() -> Result<()> {
setup_logger();

util::set_process_priority(config.setting.worker_priority);
util::set_rlimit_nofile(config.setting.worker_rlimit_nofile)?;

let conntrack_map = Arc::new(ConntrackMap::new());

Expand Down
6 changes: 6 additions & 0 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Setting {
pub queue_start: u16,
pub queue_count: u16,
pub worker_priority: i32,
pub worker_rlimit_nofile: u64,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -73,6 +74,10 @@ impl Config {
bail!("queue_count must be greater than 0");
}

if self.setting.worker_rlimit_nofile == 0 {
bail!("worker_rlimit_nofile must be greater than 0");
}

if !self.auth.key.exists() {
bail!("auth key file not found: {}", self.auth.key.display());
}
Expand Down Expand Up @@ -104,6 +109,7 @@ mod tests {
queue_start: 0,
queue_count: 0,
worker_priority: 0,
worker_rlimit_nofile: 100000,
},
auth: Auth {
port: 0,
Expand Down
7 changes: 7 additions & 0 deletions server/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ pub fn set_thread_priority() -> Result<()> {
Ok(())
}

#[inline]
pub fn set_process_priority(n: i32) {
unsafe {
libc::nice(n);
}
}

#[inline]
pub fn set_rlimit_nofile(n: u64) -> Result<u64> {
let value = rlimit::increase_nofile_limit(n)?;
Ok(value)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 15a7844

Please sign in to comment.