Skip to content

Commit d533a1a

Browse files
committed
use strict thread per core pattern
1 parent df0b48e commit d533a1a

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

frameworks/Rust/xitca-web/Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/xitca-web/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-di
102102
diesel-async = { git = "https://github.com/weiznich/diesel_async", rev = "5b8262b" }
103103
mio = { git = "https://github.com/fakeshadow/mio", rev = "9bae6012b7ecfc6083350785f71a5e8265358178" }
104104

105-
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
106-
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
107-
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
108-
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
109-
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
110-
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "4f4e349" }
105+
xitca-codegen = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }
106+
xitca-http = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }
107+
xitca-postgres = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }
108+
xitca-server = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }
109+
xitca-service = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }
110+
xitca-web = { git = "http://github.com/HFQR/xitca-web", rev = "693a5ce" }

frameworks/Rust/xitca-web/src/main_unrealistic.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ fn main() -> io::Result<()> {
3333
let mut ids = core_affinity::get_core_ids().unwrap();
3434

3535
let worker = move |id: Option<core_affinity::CoreId>| {
36+
if let Some(id) = id {
37+
let _ = core_affinity::set_for_current(id);
38+
}
39+
3640
tokio::runtime::Builder::new_current_thread()
3741
.enable_all()
38-
.on_thread_start(move || {
39-
if let Some(id) = id {
40-
let _ = core_affinity::set_for_current(id);
41-
}
42-
})
4342
.build_local(&Default::default())
4443
.unwrap()
4544
.block_on(async {
@@ -91,7 +90,7 @@ fn main() -> io::Result<()> {
9190
Ok(())
9291
}
9392

94-
async fn handler<'h>(req: Request<'h>, res: Response<'h>, state: &State<db::Client>) -> Response<'h, 3> {
93+
async fn handler<'h>(req: Request<'h, State<db::Client>>, res: Response<'h>) -> Response<'h, 3> {
9594
// unrealistic due to no http method check
9695
match req.path {
9796
// unrealistic due to no dynamic path matching
@@ -115,7 +114,7 @@ async fn handler<'h>(req: Request<'h>, res: Response<'h>, state: &State<db::Clie
115114
// all database related categories are unrealistic. please reference db_unrealistic module for detail.
116115
"/fortunes" => {
117116
use sailfish::TemplateOnce;
118-
let fortunes = state.client.tell_fortune().await.unwrap().render_once().unwrap();
117+
let fortunes = req.ctx.client.tell_fortune().await.unwrap().render_once().unwrap();
119118
res.status(StatusCode::OK)
120119
.header("content-type", "text/html; charset=utf-8")
121120
.header("server", "X")
@@ -124,18 +123,18 @@ async fn handler<'h>(req: Request<'h>, res: Response<'h>, state: &State<db::Clie
124123
"/db" => {
125124
// unrealistic due to no error handling. any db/serialization error will cause process crash.
126125
// the same goes for all following unwraps on database related functions.
127-
let world = state.client.get_world().await.unwrap();
128-
json_response(res, state, &world)
126+
let world = req.ctx.client.get_world().await.unwrap();
127+
json_response(res, req.ctx, &world)
129128
}
130129
p if p.starts_with("/q") => {
131130
let num = p["/queries?q=".len()..].parse_query();
132-
let worlds = state.client.get_worlds(num).await.unwrap();
133-
json_response(res, state, &worlds)
131+
let worlds = req.ctx.client.get_worlds(num).await.unwrap();
132+
json_response(res, req.ctx, &worlds)
134133
}
135134
p if p.starts_with("/u") => {
136135
let num = p["/updates?q=".len()..].parse_query();
137-
let worlds = state.client.update(num).await.unwrap();
138-
json_response(res, state, &worlds)
136+
let worlds = req.ctx.client.update(num).await.unwrap();
137+
json_response(res, req.ctx, &worlds)
139138
}
140139
_ => res.status(StatusCode::NOT_FOUND).header("server", "X").body(&[]),
141140
}

0 commit comments

Comments
 (0)