-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http)!: support !Send handler functions
- Loading branch information
Showing
3 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use async_net::{TcpListener, TcpStream}; | ||
use futures_lite::prelude::*; | ||
use std::thread; | ||
use trillium_http::{Conn, Stopper}; | ||
|
||
async fn handler(mut conn: Conn<TcpStream>) -> Conn<TcpStream> { | ||
let rc = std::rc::Rc::new(()); | ||
conn.set_status(200); | ||
std::future::ready(()).await; | ||
conn.set_response_body("ok"); | ||
let _ = rc.clone(); | ||
conn | ||
} | ||
|
||
pub fn main() { | ||
env_logger::init(); | ||
let stopper = Stopper::new(); | ||
let (send, receive) = async_channel::unbounded(); | ||
let core_ids = core_affinity::get_core_ids().unwrap(); | ||
let handles = core_ids | ||
.into_iter() | ||
.map(|id| { | ||
let stopper = stopper.clone(); | ||
let receive = receive.clone(); | ||
thread::spawn(move || { | ||
if !core_affinity::set_for_current(id) { | ||
log::warn!("unable to set core affinity"); | ||
} | ||
let executor = async_executor::LocalExecutor::new(); | ||
|
||
futures_lite::future::block_on(executor.run(async { | ||
while let Ok(transport) = receive.recv().await { | ||
let stopper = stopper.clone(); | ||
|
||
let future = async move { | ||
match Conn::map(transport, stopper, handler).await { | ||
Ok(_) => {} | ||
Err(e) => log::error!("{e}"), | ||
} | ||
}; | ||
executor.spawn(future).detach(); | ||
} | ||
})); | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
async_io::block_on(async move { | ||
let port = std::env::var("PORT") | ||
.unwrap_or("8080".into()) | ||
.parse::<u16>() | ||
.unwrap(); | ||
|
||
let listener = TcpListener::bind(("0.0.0.0", port)).await.unwrap(); | ||
let mut incoming = stopper.stop_stream(listener.incoming()); | ||
while let Some(Ok(stream)) = incoming.next().await { | ||
send.send(stream).await.unwrap(); | ||
} | ||
}); | ||
|
||
for handle in handles { | ||
handle.join().unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters