经过漫长的旅程,我们终于快到了书的终点。这章中,我将会创建一个项目来展示我们曾经学过的知识,并且也可以回顾我们之前学过的知识
下面是我们创建web服务器的计划
- 学习一些关于TCP和HTTP的知识。
- 在socket上监听TCP的连接。
- 解析少量的HTTP请求。
- 创建一个适当的 response
- 用线程池来提供吞吐量。
在实现之前,我们需要说一个细节,我们实现的方法不是最好的实现web服务器的方法。在 crate.io中有很多产品级的库,可以提供更多完整的web服务以及线程池。
因为Rust是个系统编程语言呢,我们可以达到更低的抽象级别,而且是别的编程语言达不到的级别。我们将会手续底层的HTTP服务器,还有线程池,这样你可以学习到总体的思想核技术,在以后你可能会用到这些。
The error tells us we can’t call join because we only have a mutable borrow of each worker and join takes ownership of its argument.
We did this in Listing 17-15: if Worker
holds an Option<thread::JoinHandle<()>>
instead, we can call the take
method on the Option to move the value out of the Some
variant and leave a None
variant in its place.
We could do more here! If you want to continue enhancing this project, here are some ideas:
- Add more documentation to
ThreadPool
and its public methods. - Add tests of the library’s functionality.
- Change calls to
unwrap
to more robust error handling. - Use
ThreadPool
to perform some task other than serving web requests. - Find a thread pool crate on crates.io and implement a similar web server using the crate instead. Then compare its API and robustness to the thread pool we implemented.