From e374f0421f41487247273dd500f45d2b7d3f0989 Mon Sep 17 00:00:00 2001 From: ridethepig Date: Thu, 9 Mar 2023 14:58:43 +0800 Subject: [PATCH] milestone 1 --- proj-2/balancebeam/implement_notes.md | 8 +++++++- proj-2/balancebeam/src/main.rs | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/proj-2/balancebeam/implement_notes.md b/proj-2/balancebeam/implement_notes.md index a10e8d4..cb67b43 100644 --- a/proj-2/balancebeam/implement_notes.md +++ b/proj-2/balancebeam/implement_notes.md @@ -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` 里面的例子就行了。 ## 附加任务? \ No newline at end of file diff --git a/proj-2/balancebeam/src/main.rs b/proj-2/balancebeam/src/main.rs index f6044e2..49fc28b 100644 --- a/proj-2/balancebeam/src/main.rs +++ b/proj-2/balancebeam/src/main.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); } } }