Skip to content

Commit

Permalink
fix(backend): don't use synchronus sleep on asyncronus threads
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenclaw900 committed Nov 8, 2021
1 parent cdaa017 commit 8a5d801
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
warp = {version = "0.3.1", default-features = false, features = ["compression", "websocket", "tls"]}
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
simple_logger = "1.13.0"
log = "0.4.14"
include_dir = "0.6.2"
Expand Down
2 changes: 1 addition & 1 deletion src/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod systemdata;
mod terminal;
mod types;

#[tokio::main(flavor = "multi_thread", worker_threads = 4)]
#[tokio::main]
async fn main() {
const DIR: include_dir::Dir = include_dir::include_dir!("dist");

Expand Down
9 changes: 5 additions & 4 deletions src/backend/src/sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use futures::{SinkExt, StreamExt};
use nanoserde::{DeJson, SerJson};
use std::process::Command;
use std::sync::Arc;
use std::{thread, time};
use std::time::Duration;
use tokio::sync::{
mpsc::{self, Receiver},
Mutex,
};
use tokio::time::sleep;
use warp::ws::Message;

use crate::{systemdata, types};
Expand Down Expand Up @@ -52,7 +53,7 @@ async fn process_handler(
},
)))
.await;
thread::sleep(time::Duration::from_secs(1));
sleep(Duration::from_secs(1)).await;
}
});
loop {
Expand Down Expand Up @@ -140,7 +141,7 @@ async fn management_handler(
&systemdata::host().await,
)))
.await;
thread::sleep(time::Duration::from_secs(1));
sleep(Duration::from_secs(1)).await;
}
});
loop {
Expand Down Expand Up @@ -170,7 +171,7 @@ async fn service_handler(
},
)))
.await;
thread::sleep(time::Duration::from_secs(2));
sleep(Duration::from_secs(2)).await;
}
});
loop {
Expand Down
7 changes: 4 additions & 3 deletions src/backend/src/systemdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use std::fs;
use std::process::Command;
use std::str::from_utf8;
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::{thread, time};
use std::time::Duration;
use tokio::time::sleep;

use crate::types;

Expand All @@ -31,7 +32,7 @@ fn round_percent(unrounded: f64) -> f32 {
pub async fn cpu() -> f32 {
let times1 = cpu::time().await.unwrap();
let used1 = times1.system() + times1.user();
thread::sleep(time::Duration::from_secs(1));
sleep(Duration::from_secs(1)).await;
let times2 = cpu::time().await.unwrap();
let used2 = times2.system() + times2.user();

Expand Down Expand Up @@ -137,7 +138,7 @@ pub async fn processes() -> Vec<types::ProcessData> {
}
cpu_list.insert(element.as_ref().unwrap().pid(), cpu);
}
thread::sleep(time::Duration::from_millis(500));
sleep(Duration::from_millis(500)).await;
for element in processes {
let pid: i32;
let name: String;
Expand Down

0 comments on commit 8a5d801

Please sign in to comment.