Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

20.3 最后的项目:Web服务器的优雅停机与清理

20.3.0. 回顾

在上一篇文章中,我们完成了多线程 Web 服务器,但仍然有一些可以改进之处。这篇文章我们就来完善代码。

注意:本文衔接于 20.2. 最后的项目:多线程Web服务器。如果你想详细了解从零开始构建 Web 服务器的过程,请阅读完第 20 章的所有文章。

20.3.1. 为 ThreadPool 实现 Drop trait

当我们想要关闭服务器(使用不太优雅的 Ctrl + C 方法停止主线程)时,所有其他线程也会立即停止,即使它们仍在处理请求。

用于管理清理的 trait 是 Drop trait。我们只需要在本地编写 drop 函数来覆盖默认实现,让线程能够在关闭之前完成当前正在处理的工作。我们还需要某种方式来阻止线程接收新请求,并为停机做好准备。

让我们为 ThreadPool 实现 Drop trait:

#![allow(unused)]
fn main() {
impl Drop for ThreadPool {
    fn drop(&mut self) {
        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            worker.thread.join().unwrap();
        }
    }
}
}

逻辑就是遍历每一个 worker,然后调用 workerthread 字段的 join 方法(详见 16.1. 使用多线程同时运行代码)。

运行 cargo check

error[E0507]: cannot move out of `worker.thread` which is behind a mutable reference
   --> src/lib.rs:42:13
    |
 42 |             worker.thread.join().unwrap();
    |             ^^^^^^^^^^^^^ ------ `worker.thread` moved due to this method call
    |             |
    |             move occurs because `worker.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
    |
note: `JoinHandle::<T>::join` takes ownership of the receiver `self`, which moves `worker.thread`
   --> /Users/stanyin/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/join_handle.rs:149:17
    |
149 |     pub fn join(self) -> Result<T> {
    |                 ^^^^

For more information about this error, try `rustc --explain E0507`.
error: could not compile `web_server` (lib) due to 1 previous error

报错信息显示我们无法把 workerthread 字段移出来,因为我们只有每个 worker 的可变引用,但 join 需要 JoinHandle 的所有权(也就是 worker.thread 的所有权)。

为了满足所有权要求,我们需要修改 Workerthread 字段的类型,用 Option<T> 包裹 thread::JoinHandle<()>。这样我们就可以调用 Option<T>::take 来获得所有权:

#![allow(unused)]
fn main() {
struct Worker {
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}
}

凡是使用了 thread 字段的地方,也都必须因 Option<T> 而更新:

#![allow(unused)]
fn main() {
impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
        let thread = thread::spawn(move || loop {
            let job = receiver.lock().unwrap().recv().unwrap();
            println!("Worker {} got a job; executing.", id);
            job();
        });

        Worker {
            id,
            thread: Some(thread),
        }
    }
}
}

thread 字段的值从 thread 改为 Some(thread)

#![allow(unused)]
fn main() {
impl Drop for ThreadPool {
    fn drop(&mut self) {
        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}
}

使用 if let 模式匹配,在 worker.threadSome 时取出其中的值(使用 take 可以获得所有权,而不是可变引用)。

20.3.2. 向线程发出信号以退出

这样修改后可以编译通过,但仍未达到预期效果。调用 drop 并不会真正关停线程,因为线程仍然卡在 loop 中等待工作。

如果用这个 drop 方法丢弃 ThreadPool,主线程会永远阻塞,等待第一个线程结束(因为每个线程都一直在循环寻找工作,不会跳出循环)。

我们需要 ThreadPoolsender 字段有两种状态:一种是附带任务的存活状态,另一种是终止状态:

#![allow(unused)]
fn main() {
pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: Option<mpsc::Sender<Job>>,
}
}

使用 Option<T> 可以让它表示这两种状态。

凡是使用了 sender 字段的地方也都必须修改:

#![allow(unused)]
fn main() {
impl Drop for ThreadPool {
    fn drop(&mut self) {
        drop(self.sender.take());

        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}
}

添加 drop(self.sender.take()); 来显式丢弃发送端,这样就会关闭通道。发生这种情况时,worker 无限循环中执行的所有 recv 调用都会返回错误,worker 也会停止运行。

#![allow(unused)]
fn main() {
pub fn new(size: usize) -> ThreadPool {
    assert!(size > 0);
    let (sender, receiver) = mpsc::channel();
    let mut workers = Vec::with_capacity(size);

    let receiver = Arc::new(Mutex::new(receiver));
    for id in 0..size {
        workers.push(Worker::new(id, Arc::clone(&receiver)));
    }

    ThreadPool {
        workers,
        sender: Some(sender),
    }
}
}

Some 包裹返回值中的 sender 字段。

#![allow(unused)]
fn main() {
pub fn execute<F>(&self, f: F)
where
    F: FnOnce() + Send + 'static,
{
    let job = Box::new(f);

    self.sender.as_ref().unwrap().send(job).unwrap();
}
}

因为 sender 现在是 Option,我们调用 as_ref 得到 Option<&Sender>(对内部发送端的引用),而不会把它从 self 中移出。随后再 unwrap 并调用 sendsend 在发送端上接受 &self,并把任务移入通道。

这样改仍然不够优雅,因为 worker 无限循环中执行的所有 recv 调用都会返回错误。最好不要因为错误而退出,所以还需要再改一处:

#![allow(unused)]
fn main() {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
    let thread = thread::spawn(move || loop {
        let job = receiver.lock().unwrap().recv();

        match job {
            Ok(job) => {
                println!("Worker {} got a job; executing.", id);
                job();
            }
            Err(_) => {
                println!("Worker {} disconnected; shutting down.", id);
                break;
            }
        }
    });
}

去掉 job 上最后一个 unwrap,转而使用 match 分支:Ok 变体就执行 jobErr 变体则打印 worker 正在断开连接,然后跳出循环。

20.3.3. 试运行

为了测试修改后的行为,我们修改 main.rs,让服务器只接受两个请求(通过 take 限制迭代次数):

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    let pool = ThreadPool::new(4);

    for stream in listener.incoming().take(2) {
        let stream = stream.unwrap();

        pool.execute(|| {
            handle_connection(stream);
        });
    }

    println!("Shutting down.");
}

浏览器显示 Hello 页面,终端记录工作线程优雅停机 控制台输出如下(某次可能的运行结果;任务分配与交错顺序是不确定的):

Shutting down.
Shutting down worker 0
Worker 0 got a job; executing.
Worker 3 got a job; executing.
Worker 1 disconnected; shutting down.
Worker 2 disconnected; shutting down.
Worker 3 disconnected; shutting down.
Worker 0 disconnected; shutting down.
Shutting down worker 1
Shutting down worker 2
Shutting down worker 3

你可能会看到不同的 Worker id,以及 “got a job”“disconnected”“Shutting down worker” 行的不同交错顺序,但整体模式应该类似。

20.3.4. 总结

main.rs:

use std::{
    fs,
    io::{prelude::*, BufReader},
    net::{TcpListener, TcpStream},
    thread,
    time::Duration,
};
use web_server::ThreadPool;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    let pool = ThreadPool::new(4);

    for stream in listener.incoming().take(2) {
        let stream = stream.unwrap();

        pool.execute(|| {
            handle_connection(stream);
        });
    }

    println!("Shutting down.");
}

fn handle_connection(mut stream: TcpStream) {
    let buf_reader = BufReader::new(&stream);
    let request_line = buf_reader.lines().next().unwrap().unwrap();

    let (status_line, filename) = match &request_line[..] {
        "GET / HTTP/1.1" => ("HTTP/1.1 200 OK", "hello.html"),
        "GET /sleep HTTP/1.1" => {
            thread::sleep(Duration::from_secs(5));
            ("HTTP/1.1 200 OK", "hello.html")
        }
        _ => ("HTTP/1.1 404 NOT FOUND", "404.html"),
    };

    let contents = fs::read_to_string(filename).unwrap();
    let length = contents.len();

    let response =
        format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}");

    stream.write_all(response.as_bytes()).unwrap();
}

lib.rs:

#![allow(unused)]
fn main() {
use std::{
    sync::{mpsc, Arc, Mutex},
    thread,
};

pub struct ThreadPool {
    workers: Vec<Worker>,
    sender: Option<mpsc::Sender<Job>>,
}

impl Drop for ThreadPool {
    fn drop(&mut self) {
        drop(self.sender.take());

        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {
                thread.join().unwrap();
            }
        }
    }
}

type Job = Box<dyn FnOnce() + Send + 'static>;

impl ThreadPool {
    /// Create a new ThreadPool.
    ///
    /// The size is the number of threads in the pool.
    ///
    /// # Panics
    ///
    /// The `new` function will panic if the size is zero.
    pub fn new(size: usize) -> ThreadPool {
        assert!(size > 0);
        let (sender, receiver) = mpsc::channel();
        let mut workers = Vec::with_capacity(size);

        let receiver = Arc::new(Mutex::new(receiver));
        for id in 0..size {
            workers.push(Worker::new(id, Arc::clone(&receiver)));
        }

        ThreadPool {
            workers,
            sender: Some(sender),
        }
    }

    pub fn execute<F>(&self, f: F)
    where
        F: FnOnce() + Send + 'static,
    {
        let job = Box::new(f);

        self.sender.as_ref().unwrap().send(job).unwrap();
    }
}

struct Worker {
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}

impl Worker {
    fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
        let thread = thread::spawn(move || loop {
            let job = receiver.lock().unwrap().recv();

            match job {
                Ok(job) => {
                    println!("Worker {} got a job; executing.", id);
                    job();
                }
                Err(_) => {
                    println!("Worker {} disconnected; shutting down.", id);
                    break;
                }
            }
        });

        Worker {
            id,
            thread: Some(thread),
        }
    }
}
}

hello.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>

404.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hello!</title>
</head>
<body>
<h1>Oops!</h1>
<p>Sorry, I don't know what you're asking for.</p>
</body>
</html>