feat: add startup
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -533,7 +533,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "room-rs"
|
name = "room-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"futures-channel",
|
"futures-channel",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "room-rs"
|
name = "room-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.2"
|
||||||
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang@Pixelbook <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ use futures_channel::mpsc::unbounded;
|
|||||||
use futures_util::{future, pin_mut, stream::TryStreamExt, StreamExt};
|
use futures_util::{future, pin_mut, stream::TryStreamExt, StreamExt};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tungstenite::protocol::Message;
|
use tungstenite::protocol::Message;
|
||||||
|
use rust_util::util_time;
|
||||||
use rust_util::XResult;
|
use rust_util::XResult;
|
||||||
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
||||||
use crate::msg::{RoomMessageDown, RoomMessage, RoomMessageType, RoomMessageDownType};
|
use crate::msg::{RoomMessageDown, RoomMessage, RoomMessageType, RoomMessageDownType};
|
||||||
@@ -29,6 +30,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||||||
static TOTAL_CREATED_CONN: AtomicU64 = AtomicU64::new(0);
|
static TOTAL_CREATED_CONN: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
static ref STARTUP_MILLIS: u128 = util_time::get_current_millis();
|
||||||
static ref TEXT_MESSAGE_HANDLES: Vec<Box<dyn HandleTextMessage>>= {
|
static ref TEXT_MESSAGE_HANDLES: Vec<Box<dyn HandleTextMessage>>= {
|
||||||
let handles:Vec<Box<dyn HandleTextMessage>> = vec![
|
let handles:Vec<Box<dyn HandleTextMessage>> = vec![
|
||||||
Box::new(HandleTextMessageExit),
|
Box::new(HandleTextMessageExit),
|
||||||
@@ -328,7 +330,7 @@ async fn main() -> Result<(), IoError> {
|
|||||||
|
|
||||||
let try_socket = TcpListener::bind(&listen_addr).await;
|
let try_socket = TcpListener::bind(&listen_addr).await;
|
||||||
let listener = try_socket.unwrap_or_else(|_| panic!("Failed to bind ok: {}", listen_addr));
|
let listener = try_socket.unwrap_or_else(|_| panic!("Failed to bind ok: {}", listen_addr));
|
||||||
success!("Listening on: {}", listen_addr);
|
success!("Listening on: {}, startup at millis: {}", listen_addr, *STARTUP_MILLIS);
|
||||||
|
|
||||||
while let Ok((stream, addr)) = listener.accept().await {
|
while let Ok((stream, addr)) = listener.accept().await {
|
||||||
TOTAL_CREATED_CONN.fetch_add(1, Ordering::Relaxed);
|
TOTAL_CREATED_CONN.fetch_add(1, Ordering::Relaxed);
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
use rust_util::util_time;
|
||||||
use crate::msg::RoomMessageDown;
|
use crate::msg::RoomMessageDown;
|
||||||
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
use crate::types::{PeerMap, RoomMap, Tx, TxSendMessage};
|
||||||
use crate::{NAME, VERSION, TOTAL_CREATED_CONN};
|
use crate::{NAME, VERSION, TOTAL_CREATED_CONN};
|
||||||
|
use simpledateformat::format_human;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct HandleContext {
|
pub struct HandleContext {
|
||||||
@@ -37,11 +40,14 @@ impl HandleTextMessage for HandleTextMessageStatics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
|
fn handle(&self, handle_context: &mut HandleContext, tx: &Tx, _addr: SocketAddr, _msg: &str) {
|
||||||
|
let current_millis = util_time::get_current_millis();
|
||||||
|
let diff_millis = iff!(current_millis > *crate::STARTUP_MILLIS, current_millis - *crate::STARTUP_MILLIS, 0);
|
||||||
tx.send_text(format!(
|
tx.send_text(format!(
|
||||||
"room count: {}\npeer count: {}\ntotal conn count:{}",
|
"room count: {}\npeer count: {}\ntotal created conn count:{}\nstartup: {}",
|
||||||
handle_context.room_map.lock().unwrap().len(),
|
handle_context.room_map.lock().unwrap().len(),
|
||||||
handle_context.peer_map.lock().unwrap().len(),
|
handle_context.peer_map.lock().unwrap().len(),
|
||||||
TOTAL_CREATED_CONN.load(Ordering::Relaxed),
|
TOTAL_CREATED_CONN.load(Ordering::Relaxed),
|
||||||
|
format_human(Duration::from_millis(diff_millis as u64)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user