milestone 1
This commit is contained in:
parent
df9a26b6f5
commit
e374f0421f
@ -22,7 +22,13 @@
|
||||
5. 接下来,`balancebeam` 试图从上游服务器读取响应。如果响应被破坏或在接收时有其他问题,它将通知客户端。
|
||||
6. 最后,`balancebeam` 将响应转发给客户。
|
||||
|
||||
然后是封装库 `request.rs` `response.rs`,就像 project 1 里面的 gimili wrapper 一样,又臭又长。分别用来基于 TCP 字节流来处理 HTTP 请求和响应。分别提供了 `read_from_stream` 和 `write_to_stream` 这两个接口供 main 处理两个 TCP 链接( Client HTTP req -> Proxy HTTP req -> Server, Server HTTP resp -> Proxy HTTP resp -> Client)。
|
||||
然后是封装库 `request.rs` `response.rs`,就像 project 1 里面的 gimili wrapper 一样,又臭又长。分别用来基于 TCP 字节流来处理 HTTP 请求和响应。分别提供了 `read_from_stream` 和 `write_to_stream` 这两个接口供 main 处理两个 TCP 链接( Client HTTP req -> Proxy HTTP req -> Server, Server HTTP resp -> Proxy HTTP resp -> Client)。具体的里面的接口我也没细看,等到 Milestone 2 需要修改的时候再说吧。
|
||||
|
||||
### Milestone 1
|
||||
|
||||
讲义上没有提出明确的实现目标,简单的把代码改成多线程而已。主要就是把 `state` 用 `Arc` 给共享掉(反正是不可变借用,十分的安全)。
|
||||
|
||||
`ThreadPool` 和 `std::thread` 没啥太大区别,都封装的很好了,直接看一下 `doc.rs` 里面的例子就行了。
|
||||
|
||||
|
||||
## 附加任务?
|
||||
@ -82,10 +82,23 @@ fn main() {
|
||||
active_health_check_path: options.active_health_check_path,
|
||||
max_requests_per_minute: options.max_requests_per_minute,
|
||||
};
|
||||
let astate = std::sync::Arc::new(state);
|
||||
// let thread_pool = threadpool::ThreadPool::new(4);
|
||||
for stream in listener.incoming() {
|
||||
if let Ok(stream) = stream {
|
||||
// Handle the connection!
|
||||
handle_connection(stream, &state);
|
||||
// ------ std thread ------
|
||||
let thd_state = astate.clone();
|
||||
std::thread::spawn(move || {
|
||||
handle_connection(stream, &thd_state);
|
||||
});
|
||||
// ------ thread pool ------
|
||||
// let thd_state = astate.clone();
|
||||
// thread_pool.execute(move || {
|
||||
// handle_connection(stream, &thd_state);
|
||||
// });
|
||||
// ------ single thread ------
|
||||
// handle_connection(stream, &state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user