fix, feat: fix high cpu usage, add console-subscriber

This commit is contained in:
user0-07161 2025-10-04 22:33:45 +02:00
parent f7fb81630d
commit 86910cb29a
3 changed files with 938 additions and 10 deletions

924
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,3 +9,9 @@ async-trait = "0.1.89"
clap = { version = "4.5.48", features = ["derive"] }
once_cell = "1.21.3"
tokio = { version = "1.47.1", features = ["full"] }
console-subscriber = { version = "0.4.1", optional = true }
tracing = "0.1.41"
tracing-subscriber = "0.3.20"
[features]
tokio-console = ["tokio/tracing", "console-subscriber"]

View file

@ -2,7 +2,6 @@ use std::{
collections::HashSet,
net::{SocketAddr, TcpListener, TcpStream},
str::FromStr,
time::Duration,
};
use anyhow::{Result, bail};
@ -15,8 +14,8 @@ use tokio::{
Mutex,
broadcast::{self, Receiver, Sender},
},
time::sleep,
};
use tracing::instrument;
use crate::{
channels::Channel,
@ -40,7 +39,7 @@ pub static JOINED_CHANNELS: Lazy<Mutex<HashSet<Channel>>> =
pub static SENDER: Lazy<Mutex<Option<Sender<Message>>>> = Lazy::new(|| Mutex::new(None));
#[allow(dead_code)]
#[derive(Clone)]
#[derive(Clone, Debug)]
struct ServerInfo {
ip: String,
port: String,
@ -51,6 +50,9 @@ struct ServerInfo {
#[tokio::main]
async fn main() -> Result<()> {
#[cfg(feature = "tokio-console")]
console_subscriber::init();
let info = ServerInfo {
ip: "0.0.0.0".into(),
port: "6667".into(),
@ -72,16 +74,15 @@ async fn main() -> Result<()> {
let tx_thread = tx.clone();
let info = info.clone();
spawn(async move {
handle_connection(stream, info, /*&mut rx_thread,*/ tx_thread)
.await
.unwrap()
});
spawn(handle_connection(
stream, info, /*&mut rx_thread,*/ tx_thread,
));
}
Ok(())
}
#[instrument]
async fn handle_connection(stream: TcpStream, info: ServerInfo, tx: Sender<Message>) -> Result<()> {
let stream_tcp = stream.try_clone()?;
let mut message_receiver = tx.clone().subscribe();
@ -110,7 +111,6 @@ async fn handle_connection(stream: TcpStream, info: ServerInfo, tx: Sender<Messa
}
}
},
_ = sleep(Duration::from_millis(200)) => {},
}
}