@@ -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