Browse Source

upgrade to ntex-0.6 (#7876)

Nikolay Kim 2 years ago
parent
commit
9acf01f8e2

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

@@ -1,6 +1,6 @@
 [package]
 name = "ntex"
-version = "0.3.0"
+version = "0.4.0"
 edition = "2018"
 
 [[bin]]
@@ -37,13 +37,14 @@ tokio = ["ntex/tokio"]
 async-std = ["ntex/async-std"]
 
 [dependencies]
-ntex = "0.5.30"
+ntex = "0.6.1"
+ntex-bytes = { version = "0.1.19", features=["simd"] }
 mimalloc = { version = "0.1.25", default-features = false }
 snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
 yarte = { version = "0.15", features = ["bytes-buf", "json"] }
 buf-min = { version = "0.7", features = ["ntex-bytes"] }
 env_logger = "0.10"
-nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand"] }
+nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
 atoi = "2.0"
 num_cpus = "1.13"
 smallvec = "1.6.1"
@@ -51,7 +52,7 @@ serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 log = { version = "0.4", features = ["release_max_level_off"] }
 tok_io = {version = "1", package = "tokio" }
-tokio-postgres = { git="https://github.com/fafhrd91/postgres.git" }
+tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-0.6" }
 
 [profile.release]
 opt-level = 3

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

@@ -1,4 +1,3 @@
-#![allow(clippy::uninit_vec)]
 use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};
 
 use nanorand::{Rng, WyRand};
@@ -119,7 +118,7 @@ impl PgConnection {
     }
 
     pub async fn update(&self, num: usize) -> Bytes {
-        let mut rng = self.rng.clone();
+        let mut rng = nanorand::tls_rng();
         let mut queries = SmallVec::<[_; 32]>::new();
         (0..num).for_each(|_| {
             let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;

+ 11 - 20
frameworks/Rust/ntex/src/main_db.rs

@@ -2,36 +2,27 @@
 #[global_allocator]
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
-use std::{future::Future, pin::Pin, rc::Rc, task::Context, task::Poll};
-
 use ntex::http::header::{CONTENT_TYPE, SERVER};
 use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
 use ntex::service::{Service, ServiceFactory};
 use ntex::web::{Error, HttpResponse};
-use ntex::{time::Seconds, util::PoolId};
+use ntex::{time::Seconds, util::PoolId, util::BoxFuture};
 
 mod db;
 mod utils;
 
-struct App(Rc<db::PgConnection>);
+struct App(db::PgConnection);
 
 impl Service<Request> for App {
     type Response = Response;
     type Error = Error;
-    type Future = Pin<Box<dyn Future<Output = Result<Response, Error>>>>;
-
-    #[inline]
-    fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
-        Poll::Ready(Ok(()))
-    }
-
-    fn call(&self, req: Request) -> Self::Future {
-        let db = self.0.clone();
+    type Future<'f> = BoxFuture<'f, Result<Response, Error>> where Self: 'f;
 
+    fn call(&self, req: Request) -> Self::Future<'_> {
         Box::pin(async move {
             match req.path() {
                 "/db" => {
-                    let body = db.get_world().await;
+                    let body = self.0.get_world().await;
                     let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
                     res.headers_mut().insert(SERVER, utils::HDR_SERVER);
                     res.headers_mut()
@@ -39,7 +30,7 @@ impl Service<Request> for App {
                     Ok(res)
                 }
                 "/fortunes" => {
-                    let body = db.tell_fortune().await;
+                    let body = self.0.tell_fortune().await;
                     let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
                     res.headers_mut().insert(SERVER, utils::HDR_SERVER);
                     res.headers_mut()
@@ -47,7 +38,7 @@ impl Service<Request> for App {
                     Ok(res)
                 }
                 "/query" => {
-                    let worlds = db
+                    let worlds = self.0
                         .get_worlds(utils::get_query_param(req.uri().query()))
                         .await;
                     let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
@@ -57,7 +48,7 @@ impl Service<Request> for App {
                     Ok(res)
                 }
                 "/update" => {
-                    let worlds = db.update(utils::get_query_param(req.uri().query())).await;
+                    let worlds = self.0.update(utils::get_query_param(req.uri().query())).await;
                     let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
                     res.headers_mut().insert(SERVER, utils::HDR_SERVER);
                     res.headers_mut()
@@ -77,13 +68,13 @@ impl ServiceFactory<Request> for AppFactory {
     type Error = Error;
     type Service = App;
     type InitError = ();
-    type Future = Pin<Box<dyn Future<Output = Result<Self::Service, Self::InitError>>>>;
+    type Future<'f> = BoxFuture<'f, Result<Self::Service, Self::InitError>>;
 
-    fn new_service(&self, _: ()) -> Self::Future {
+    fn create(&self, _: ()) -> Self::Future<'_> {
         const DB_URL: &str =
             "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
 
-        Box::pin(async move { Ok(App(Rc::new(db::PgConnection::connect(DB_URL).await))) })
+        Box::pin(async move { Ok(App(db::PgConnection::connect(DB_URL).await)) })
     }
 }
 

+ 4 - 2
frameworks/Rust/ntex/src/utils.rs

@@ -11,6 +11,8 @@ pub const HDR_HTML_CONTENT_TYPE: HeaderValue =
     HeaderValue::from_static("text/html; charset=utf-8");
 pub const BODY_PLAIN_TEXT: Bytes = Bytes::from_static(b"Hello, World!");
 
+const LW: usize = 1024;
+const HW: usize = 128 * 1024;
 pub const SIZE: usize = 27;
 
 pub fn get_query_param(query: Option<&str>) -> usize {
@@ -25,7 +27,7 @@ pub fn get_query_param(query: Option<&str>) -> usize {
 
 pub fn reserve(buf: &mut BytesMut) {
     let remaining = buf.remaining_mut();
-    if remaining < 1024 {
-        buf.reserve(65535 - remaining);
+    if remaining < LW {
+        buf.reserve(HW);
     }
 }