|
@@ -1,10 +1,8 @@
|
|
|
#[global_allocator]
|
|
|
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
|
|
|
-
|
|
|
use std::{future::Future, io, pin::Pin, task::Context, task::Poll};
|
|
|
|
|
|
-use ntex::{fn_service, http::h1, io::Io, util::BufMut, util::PoolId};
|
|
|
-
|
|
|
+use ntex::{fn_service, http::h1, io::Io, util::ready, util::BufMut, util::PoolId};
|
|
|
mod utils;
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
@@ -33,54 +31,41 @@ impl Future for App {
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
let this = self.as_mut().get_mut();
|
|
|
- if this.io.is_closed() {
|
|
|
- return Poll::Ready(Ok(()));
|
|
|
- }
|
|
|
-
|
|
|
- let read = this.io.read();
|
|
|
- let write = this.io.write();
|
|
|
loop {
|
|
|
- match read.decode(&this.codec) {
|
|
|
- Ok(Some((req, _))) => {
|
|
|
- let _ = write.with_buf(|buf| {
|
|
|
- // make sure we've got room
|
|
|
- let remaining = buf.remaining_mut();
|
|
|
- if remaining < 1024 {
|
|
|
- buf.reserve(65535 - remaining);
|
|
|
- }
|
|
|
+ if let Some(Ok((req, _))) = ready!(this.io.poll_read_next(&this.codec, cx)) {
|
|
|
+ let _ = this.io.with_write_buf(|buf| {
|
|
|
+ // make sure we've got room
|
|
|
+ let remaining = buf.remaining_mut();
|
|
|
+ if remaining < 1024 {
|
|
|
+ buf.reserve(65535 - remaining);
|
|
|
+ }
|
|
|
|
|
|
- match req.path() {
|
|
|
- "/json" => {
|
|
|
- buf.extend_from_slice(JSON);
|
|
|
- this.codec.set_date_header(buf);
|
|
|
- let _ = simd_json::to_writer(
|
|
|
- crate::utils::Writer(buf),
|
|
|
- &Message {
|
|
|
- message: "Hello, World!",
|
|
|
- },
|
|
|
- );
|
|
|
- }
|
|
|
- "/plaintext" => {
|
|
|
- buf.extend_from_slice(PLAIN);
|
|
|
- this.codec.set_date_header(buf);
|
|
|
- buf.extend_from_slice(BODY);
|
|
|
- }
|
|
|
- _ => {
|
|
|
- buf.extend_from_slice(HTTPNFOUND);
|
|
|
- buf.extend_from_slice(HDR_SERVER);
|
|
|
- }
|
|
|
+ match req.path() {
|
|
|
+ "/json" => {
|
|
|
+ buf.extend_from_slice(JSON);
|
|
|
+ this.codec.set_date_header(buf);
|
|
|
+ let _ = simd_json::to_writer(
|
|
|
+ crate::utils::Writer(buf),
|
|
|
+ &Message {
|
|
|
+ message: "Hello, World!",
|
|
|
+ },
|
|
|
+ );
|
|
|
+ }
|
|
|
+ "/plaintext" => {
|
|
|
+ buf.extend_from_slice(PLAIN);
|
|
|
+ this.codec.set_date_header(buf);
|
|
|
+ buf.extend_from_slice(BODY);
|
|
|
+ }
|
|
|
+ _ => {
|
|
|
+ buf.extend_from_slice(HTTPNFOUND);
|
|
|
+ buf.extend_from_slice(HDR_SERVER);
|
|
|
}
|
|
|
- });
|
|
|
- }
|
|
|
- Ok(None) => break,
|
|
|
- _ => {
|
|
|
- this.io.close();
|
|
|
- return Poll::Ready(Err(()));
|
|
|
- }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return Poll::Ready(Ok(()));
|
|
|
}
|
|
|
}
|
|
|
- let _ = read.poll_read_ready(cx);
|
|
|
- Poll::Pending
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -91,7 +76,8 @@ async fn main() -> io::Result<()> {
|
|
|
// start http server
|
|
|
ntex::server::build()
|
|
|
.backlog(1024)
|
|
|
- .bind("techempower", "0.0.0.0:8080", || {
|
|
|
+ .bind("techempower", "0.0.0.0:8080", |cfg| {
|
|
|
+ cfg.memory_pool(PoolId::P1);
|
|
|
PoolId::P1.set_read_params(65535, 8192);
|
|
|
PoolId::P1.set_write_params(65535, 8192);
|
|
|
|
|
@@ -100,7 +86,6 @@ async fn main() -> io::Result<()> {
|
|
|
codec: h1::Codec::default(),
|
|
|
})
|
|
|
})?
|
|
|
- .memory_pool("techempower", PoolId::P1)
|
|
|
- .start()
|
|
|
+ .run()
|
|
|
.await
|
|
|
}
|