print server addr, make able to shutdown gracefully

This commit is contained in:
AJ ONeal 2018-10-02 14:55:58 -06:00
parent ebf0a89406
commit 1416356db9
2 changed files with 67 additions and 24 deletions

View File

@ -10,13 +10,17 @@ use std::thread;
use std::time::Duration;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let counter = Arc::new(Mutex::new(0usize));
let addr = "127.0.0.1:7878";
let listener = TcpListener::bind(&addr).unwrap();
println!("Listening on http://{}", &addr);
let counter = Arc::new(Mutex::new(0usize));
let tasks = ThreadPool::new(100);
for stream in listener.incoming() {
let stream = stream.unwrap();
for stream in listener.incoming()/*.take(2)*/ {
println!("accepted new connection");
if let Ok(stream) = stream {
let counter = Arc::clone(&counter);
tasks.run(move || {
@ -30,6 +34,9 @@ fn main() {
}
}
println!("Shutting down...");
}
fn handle_connection(counter: Arc<Mutex<usize>>, mut stream: TcpStream) {
let mut buffer = [0; 512];

View File

@ -16,29 +16,65 @@ type Job = Box<dyn FnBox + Send + 'static>;
struct Worker {
id: usize,
thread: thread::JoinHandle<()>,
thread: Option<thread::JoinHandle<()>>,
}
enum Message {
NewJob(Job),
Terminate,
}
impl Worker {
pub fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
pub fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
Worker {
id,
thread: thread::spawn(move || {
thread: Some(thread::spawn(move || {
loop {
let job = receiver.lock().expect("unrecoverable poisened panicked thread state").recv().unwrap();
if let Ok(job) = receiver.lock().expect("unrecoverable poisened panicked thread state").recv() {
match job {
Message::NewJob(job) => {
println!("such busy, so worker #{}!", id);
job.call_box();
}
}),
Message::Terminate => {
println!("such sad, so dead #{}!", id);
break;
}
}
} else {
// TODO respawn? (unless in terminate mode)
// or why would the recv fail? already terminated?
// thread panic not handled?
break;
}
}
})),
}
}
}
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Job>,
sender: mpsc::Sender<Message>,
}
impl Drop for ThreadPool {
fn drop(&mut self) {
// Each worker will only receive Terminate once
for _ in &mut self.workers {
self.sender.send(Message::Terminate).unwrap();
}
// Each worker will join once complete
for worker in &mut self.workers {
println!("Shutting down worker {} ...", worker.id);
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
println!("Killed {}!", worker.id);
}
}
}
}
impl ThreadPool {
@ -64,6 +100,6 @@ impl ThreadPool {
pub fn run<F: FnOnce() + Send + 'static>(&self, task: F)
{
let task = Box::new(task);
self.sender.send(task).unwrap();
self.sender.send(Message::NewJob(task)).unwrap();
}
}