reduce tfclient CPU usage by 99.96%!

This commit is contained in:
c0repwn3r 2023-03-31 08:33:02 -04:00
parent 9a5ffcd622
commit 6199608aeb
Signed by: core
GPG Key ID: FDBF740DADDCEECF
6 changed files with 45 additions and 29 deletions

2
Cargo.lock generated
View File

@ -2480,7 +2480,7 @@ dependencies = [
[[package]]
name = "tfclient"
version = "0.1.4"
version = "0.1.6"
dependencies = [
"base64 0.21.0",
"base64-serde",

View File

@ -1,6 +1,6 @@
[package]
name = "tfclient"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
description = "An open-source reimplementation of a Defined Networking-compatible client"
license = "GPL-3.0-or-later"
@ -40,3 +40,7 @@ tar = "0.4.38"
hex = "0.4.3"
tempfile = "3.4.0"
sha2 = "0.10.6"
[target.x86_64-unknown-linux-gnu]
linker = "/usr/bin/clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Clink-arg=-Wl,--no-rosegment"]

View File

@ -1,5 +1,5 @@
use std::fs;
use std::sync::mpsc::{Receiver, TryRecvError};
use std::sync::mpsc::{Receiver, RecvError, TryRecvError};
use log::{error, info, warn};
@ -27,7 +27,7 @@ pub fn apiworker_main(_config: TFClientConfig, instance: String, url: String, tx
let client = Client::new(format!("tfclient/{}", env!("CARGO_PKG_VERSION")), server).unwrap();
loop {
match rx.try_recv() {
match rx.recv() {
Ok(msg) => {
match msg {
APIWorkerMessage::Shutdown => {
@ -206,13 +206,8 @@ pub fn apiworker_main(_config: TFClientConfig, instance: String, url: String, tx
}
},
Err(e) => {
match e {
TryRecvError::Empty => {}
TryRecvError::Disconnected => {
error!("apiworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
error!("error on command socket: {}", e);
return;
}
}
}

View File

@ -12,7 +12,8 @@ use crate::embedded_nebula::run_embedded_nebula;
pub enum NebulaWorkerMessage {
Shutdown,
ConfigUpdated
ConfigUpdated,
WakeUp
}
fn insert_private_key(instance: &str) -> Result<(), Box<dyn Error>> {
@ -86,9 +87,12 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter
last_restart_time = SystemTime::now();
}
}
match rx.try_recv() {
match rx.recv() {
Ok(msg) => {
match msg {
NebulaWorkerMessage::WakeUp => {
continue;
},
NebulaWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping");
info!("shutting down nebula binary");
@ -140,13 +144,8 @@ pub fn nebulaworker_main(_config: TFClientConfig, instance: String, _transmitter
}
},
Err(e) => {
match e {
TryRecvError::Empty => {}
TryRecvError::Disconnected => {
error!("nebulaworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
error!("nebulaworker command socket errored: {}", e);
return;
}
}
}

View File

@ -15,7 +15,8 @@ use crate::nebulaworker::NebulaWorkerMessage;
use crate::timerworker::TimerWorkerMessage;
pub enum SocketWorkerMessage {
Shutdown
Shutdown,
WakeUp
}
pub fn socketworker_main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSender, rx: Receiver<SocketWorkerMessage>) {
@ -51,23 +52,21 @@ fn _main(config: TFClientConfig, instance: String, transmitter: ThreadMessageSen
Err(e) => { Err(e)?; }
}
match rx.try_recv() {
match rx.recv() {
Ok(msg) => {
match msg {
SocketWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping");
break;
},
SocketWorkerMessage::WakeUp => {
continue;
}
}
},
Err(e) => {
match e {
TryRecvError::Empty => {}
TryRecvError::Disconnected => {
error!("socketworker command socket disconnected, shutting down to prevent orphaning");
break;
}
}
error!("socketworker command socket errored: {}", e);
break;
}
}
}

View File

@ -1,9 +1,12 @@
use std::ops::Add;
use std::sync::mpsc::{Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, SystemTime};
use log::{error, info};
use crate::apiworker::APIWorkerMessage;
use crate::daemon::ThreadMessageSender;
use crate::nebulaworker::NebulaWorkerMessage;
use crate::socketworker::SocketWorkerMessage;
pub enum TimerWorkerMessage {
Shutdown
@ -13,6 +16,8 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>) {
let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
loop {
thread::sleep(Duration::from_secs(10));
match rx.try_recv() {
Ok(msg) => {
match msg {
@ -43,5 +48,19 @@ pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>) {
}
}
}
match tx.nebula_thread.send(NebulaWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!("Error sending wakeup message to nebula worker thread: {}", e);
}
}
match tx.socket_thread.send(SocketWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!("Error sending wakeup message to socket worker thread: {}", e);
}
}
}
}