Last active
November 21, 2019 22:27
-
-
Save nayato/e6eea3b9dd93ae9fe1ac8ff188518b37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use actix_codec::{BytesCodec, Framed}; | |
use futures::StreamExt; | |
use parking_lot::Mutex; | |
use std::cell::RefCell; | |
use std::rc::Rc; | |
use std::sync::Arc; | |
use std::time::Duration; | |
use tokio::net::TcpListener; | |
use tokio::runtime::current_thread; | |
fn main() { | |
std::thread::spawn(|| { | |
let mut crt = tokio::runtime::current_thread::Runtime::new().unwrap(); | |
crt.block_on(async { | |
let mut listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); | |
loop { | |
let (socket, _) = listener.accept().await.unwrap(); | |
current_thread::spawn(async move { | |
let framed = Framed::new(socket, BytesCodec); | |
let size: Rc<RefCell<usize>> = Rc::new(RefCell::new(0)); | |
let size_mon = size.clone(); | |
current_thread::spawn(async move { | |
loop { | |
tokio::timer::delay_for(Duration::from_secs(5)); | |
println!("acc size: {}", size_mon.borrow()); | |
} | |
}); | |
framed | |
.for_each(|data| { | |
*size.borrow_mut() += i.unwrap().len(); | |
futures::future::ready(()) | |
}) | |
.await; | |
}); | |
} | |
}); | |
}); | |
let rt = tokio::runtime::Runtime::new().unwrap(); | |
rt.block_on(async { | |
let mut listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); | |
loop { | |
let (socket, _) = listener.accept().await.unwrap(); | |
tokio::spawn(async move { | |
let framed = Framed::new(socket, BytesCodec); | |
let size: Arc<Mutex<usize>> = Arc::new(Mutex::new(0)); | |
let size_mon = size.clone(); | |
tokio::spawn(async move { | |
loop { | |
tokio::timer::delay_for(Duration::from_secs(5)); | |
println!("acc size: {}", size_mon.lock()); | |
} | |
}); | |
framed | |
.for_each(|data| { | |
*size.lock() += i.unwrap().len(); | |
futures::future::ready(()) | |
}) | |
.await; | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment