-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jtgeibel/tokio-threadpool
Switch from `futures-cpupool` to `tokio-threadpool`
- Loading branch information
Showing
4 changed files
with
223 additions
and
54 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,57 @@ | ||
#![deny(warnings, clippy::all)] | ||
|
||
use conduit::{Handler, Request, Response}; | ||
use conduit_hyper::Server; | ||
use conduit_router::RouteBuilder; | ||
use futures::Future; | ||
use tokio::runtime; | ||
|
||
use std::collections::HashMap; | ||
use std::io::{Cursor, Error}; | ||
use std::thread::sleep; | ||
|
||
fn main() { | ||
let app = build_conduit_handler(); | ||
let addr = ([127, 0, 0, 1], 12345).into(); | ||
let server = Server::bind(&addr, app).map_err(|e| { | ||
eprintln!("server error: {}", e); | ||
}); | ||
|
||
let mut rt = runtime::Builder::new() | ||
// Set the max number of concurrent requests (tokio defaults to 100) | ||
.blocking_threads(2) | ||
.build() | ||
.unwrap(); | ||
rt.spawn(server); | ||
rt.shutdown_on_idle().wait().unwrap(); | ||
} | ||
|
||
fn build_conduit_handler() -> impl Handler { | ||
let mut router = RouteBuilder::new(); | ||
router.get("/", endpoint); | ||
router.get("/panic", panic); | ||
router | ||
} | ||
|
||
fn endpoint(_: &mut dyn Request) -> Result<Response, Error> { | ||
let body = "Hello world!"; | ||
|
||
sleep(std::time::Duration::from_secs(2)); | ||
|
||
let mut headers = HashMap::new(); | ||
headers.insert( | ||
"Content-Type".to_string(), | ||
vec!["text/plain; charset=utf-8".to_string()], | ||
); | ||
headers.insert("Content-Length".to_string(), vec![body.len().to_string()]); | ||
Ok(Response { | ||
status: (200, "OK"), | ||
headers, | ||
body: Box::new(Cursor::new(body)), | ||
}) | ||
} | ||
|
||
fn panic(_: &mut dyn Request) -> Result<Response, Error> { | ||
// For now, connection is immediately closed | ||
panic!("message"); | ||
} |
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
Oops, something went wrong.