Browse Source

upgrade ntex to 0.4 (#6682)

* upgrade ntex

* bump version
Nikolay Kim 4 years ago
parent
commit
4ee6fb946b

+ 3 - 4
frameworks/Rust/ntex/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "ntex"
-version = "0.2.0"
+version = "0.2.1"
 edition = "2018"
 
 [[bin]]
@@ -16,13 +16,12 @@ name = "ntex-raw"
 path = "src/main_raw.rs"
 
 [dependencies]
-ntex = "0.3.17"
+ntex = "0.4.0-b.1"
 mimalloc = { version = "0.1.25", default-features = false }
 snmalloc-rs = { version = "0.2.26", features = ["1mib", "native-cpu"] }
 yarte = { version = "0.15", features = ["bytes-buf", "json"] }
 env_logger = "0.8"
 nanorand = { version = "0.5", default-features = false, features = ["std", "wyrand"] }
-bytes = "1.0"
 atoi = "0.4"
 num_cpus = "1.13"
 futures = "0.3"
@@ -32,7 +31,7 @@ simd-json = "0.4.6"
 simd-json-derive = "0.2.2"
 serde = { version = "1.0", features = ["derive"] }
 log = { version = "0.4", features = ["release_max_level_off"] }
-tokio = "1"
+tokio = { version = "1", default-features = false }
 tokio-postgres = { git="https://github.com/fafhrd91/postgres.git" }
 
 [profile.release]

+ 1 - 1
frameworks/Rust/ntex/ntex-db.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.52.1
+FROM rust:1.53.0
 
 # Disable simd at jsonescape
 # ENV CARGO_CFG_JSONESCAPE_DISABLE_AUTO_SIMD=

+ 1 - 1
frameworks/Rust/ntex/ntex-raw.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.52.1
+FROM rust:1.53.0
 
 # Disable simd at jsonescape
 # ENV CARGO_CFG_JSONESCAPE_DISABLE_AUTO_SIMD=

+ 1 - 1
frameworks/Rust/ntex/ntex.dockerfile

@@ -1,4 +1,4 @@
-FROM rust:1.52.1
+FROM rust:1.53.0
 
 # Disable simd at jsonescape
 # ENV CARGO_CFG_JSONESCAPE_DISABLE_AUTO_SIMD=

+ 14 - 7
frameworks/Rust/ntex/src/db.rs

@@ -1,14 +1,16 @@
 use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
 
-use bytes::{Bytes, BytesMut};
 use futures::{Future, FutureExt};
 use nanorand::{WyRand, RNG};
+use ntex::util::{Bytes, BytesMut};
 use smallvec::SmallVec;
 use tokio_postgres::types::ToSql;
 use tokio_postgres::{connect, Client, NoTls, Statement};
 use yarte::{ywrite_html, Serialize};
 
-#[derive(Copy, Clone, Serialize, Debug)]
+use crate::utils::Writer;
+
+#[derive(Copy, Clone, Serialize, Debug, serde::Serialize)]
 pub struct World {
     pub id: i32,
     pub randomnumber: i32,
@@ -72,11 +74,16 @@ impl PgConnection {
         let random_id = (self.rng.borrow_mut().generate::<u32>() % 10_000 + 1) as i32;
         self.cl.query(&self.world, &[&random_id]).map(|rows| {
             let rows = rows.unwrap();
-            World {
-                id: rows[0].get(0),
-                randomnumber: rows[0].get(1),
-            }
-            .to_bytes::<BytesMut>(40)
+            let mut body = BytesMut::new();
+            simd_json::to_writer(
+                Writer(&mut body),
+                &World {
+                    id: rows[0].get(0),
+                    randomnumber: rows[0].get(1),
+                },
+            )
+            .unwrap();
+            body.freeze()
         })
     }
 

+ 1 - 2
frameworks/Rust/ntex/src/main.rs

@@ -1,9 +1,8 @@
 #[global_allocator]
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
-use bytes::Bytes;
 use ntex::http::header::{HeaderValue, CONTENT_TYPE, SERVER};
-use ntex::{http, web};
+use ntex::{http, util::Bytes, web};
 use yarte::Serialize;
 
 mod utils;

+ 7 - 4
frameworks/Rust/ntex/src/main_db.rs

@@ -3,13 +3,12 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
 use std::{pin::Pin, task::Context, task::Poll};
 
-use bytes::BytesMut;
 use futures::future::{ok, Future, FutureExt};
 use ntex::http::header::{HeaderValue, CONTENT_TYPE, SERVER};
 use ntex::http::{HttpService, KeepAlive, Request, Response};
 use ntex::service::{Service, ServiceFactory};
+use ntex::util::BytesMut;
 use ntex::web::{Error, HttpResponse};
-use yarte::Serialize;
 
 mod db;
 mod utils;
@@ -48,20 +47,24 @@ impl Service for App {
                 self.0
                     .get_worlds(utils::get_query_param(req.uri().query()))
                     .map(|worlds| {
+                        let mut body = BytesMut::with_capacity(35 * worlds.len());
+                        let _ = simd_json::to_writer(crate::utils::Writer(&mut body), &worlds);
                         Ok(HttpResponse::Ok()
                             .header(SERVER, HeaderValue::from_static("N"))
                             .header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
-                            .body(worlds.to_bytes::<BytesMut>(35 * worlds.len())))
+                            .body(body.freeze()))
                     }),
             ),
             "/update" => Box::pin(
                 self.0
                     .update(utils::get_query_param(req.uri().query()))
                     .map(|worlds| {
+                        let mut body = BytesMut::with_capacity(35 * worlds.len());
+                        let _ = simd_json::to_writer(crate::utils::Writer(&mut body), &worlds);
                         Ok(HttpResponse::Ok()
                             .header(SERVER, HeaderValue::from_static("N"))
                             .header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
-                            .body(worlds.to_bytes::<BytesMut>(35 * worlds.len())))
+                            .body(body.freeze()))
                     }),
             ),
             _ => Box::pin(ok(Response::new(http::StatusCode::NOT_FOUND))),

+ 7 - 6
frameworks/Rust/ntex/src/main_raw.rs

@@ -7,7 +7,6 @@ use ntex::fn_service;
 use ntex::framed::{ReadTask, State, WriteTask};
 use ntex::http::h1;
 use ntex::rt::net::TcpStream;
-use yarte::Serialize;
 
 mod utils;
 
@@ -19,7 +18,7 @@ const HTTPNFOUND: &[u8] = b"HTTP/1.1 400 OK\r\n";
 const HDR_SERVER: &[u8] = b"Server: N\r\n";
 const BODY: &[u8] = b"Hello, World!";
 
-#[derive(Serialize)]
+#[derive(serde::Serialize)]
 pub struct Message {
     pub message: &'static str,
 }
@@ -55,10 +54,12 @@ impl Future for App {
                             "/json" => {
                                 buf.extend_from_slice(JSON);
                                 this.codec.set_date_header(buf);
-                                Message {
-                                    message: "Hello, World!",
-                                }
-                                .to_bytes_mut(buf);
+                                let _ = simd_json::to_writer(
+                                    crate::utils::Writer(buf),
+                                    &Message {
+                                        message: "Hello, World!",
+                                    },
+                                );
                             }
                             "/plaintext" => {
                                 buf.extend_from_slice(PLAIN);

+ 14 - 1
frameworks/Rust/ntex/src/utils.rs

@@ -1,7 +1,8 @@
 #![allow(dead_code)]
-use std::cmp;
+use std::{cmp, io};
 
 use atoi::FromRadix10;
+use ntex::util::{BufMut, BytesMut};
 
 pub const SIZE: usize = 27;
 
@@ -14,3 +15,15 @@ pub fn get_query_param(query: Option<&str>) -> u16 {
     };
     cmp::min(500, cmp::max(1, q))
 }
+
+pub struct Writer<'a>(pub &'a mut BytesMut);
+
+impl<'a> io::Write for Writer<'a> {
+    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+        self.0.put_slice(buf);
+        Ok(buf.len())
+    }
+    fn flush(&mut self) -> io::Result<()> {
+        Ok(())
+    }
+}