Browse Source

ntex: upgrade to ntex-1.0 (#8680)

Nikolay Kim 1 year ago
parent
commit
101e4fbdf1

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

@@ -1,6 +1,6 @@
 [package]
 name = "ntex"
-version = "0.7.0"
+version = "1.0.0"
 edition = "2018"
 
 [[bin]]
@@ -37,8 +37,8 @@ tokio = ["ntex/tokio"]
 async-std = ["ntex/async-std"]
 
 [dependencies]
-ntex = "0.7.2"
-ntex-bytes = { version = "0.1.19", features=["simd"] }
+ntex = "1.0.0"
+ntex-bytes = { version = "0.1.21", 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"] }
@@ -52,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-1.0" }
 
 [profile.release]
 opt-level = 3

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

@@ -55,7 +55,9 @@ async fn main() -> std::io::Result<()> {
 
             http::HttpService::build()
                 .keep_alive(http::KeepAlive::Os)
-                .client_timeout(Seconds(0))
+                .client_timeout(Seconds::ZERO)
+                .headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
+                .payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
                 .h1(web::App::new().service(json).service(plaintext).finish())
         })?
         .workers(num_cpus::get())

+ 46 - 48
frameworks/Rust/ntex/src/main_db.rs

@@ -7,7 +7,7 @@ use ntex::http::header::{CONTENT_TYPE, SERVER};
 use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
 use ntex::service::{Service, ServiceCtx, ServiceFactory};
 use ntex::web::{Error, HttpResponse};
-use ntex::{time::Seconds, util::BoxFuture, util::PoolId};
+use ntex::{time::Seconds, util::PoolId};
 
 mod db;
 mod utils;
@@ -17,52 +17,49 @@ struct App(db::PgConnection);
 impl Service<Request> for App {
     type Response = Response;
     type Error = Error;
-    type Future<'f> = BoxFuture<'f, Result<Response, Error>> where Self: 'f;
 
-    fn call<'a>(&'a self, req: Request, _: ServiceCtx<'a, Self>) -> Self::Future<'a> {
-        Box::pin(async move {
-            match req.path() {
-                "/db" => {
-                    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()
-                        .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
-                    Ok(res)
-                }
-                "/fortunes" => {
-                    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()
-                        .insert(CONTENT_TYPE, utils::HDR_HTML_CONTENT_TYPE);
-                    Ok(res)
-                }
-                "/query" => {
-                    let worlds = self
-                        .0
-                        .get_worlds(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()
-                        .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
-                    Ok(res)
-                }
-                "/update" => {
-                    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()
-                        .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
-                    Ok(res)
-                }
-                _ => Ok(Response::new(StatusCode::NOT_FOUND)),
+    async fn call(&self, req: Request, _: ServiceCtx<'_, Self>) -> Result<Response, Error> {
+        match req.path() {
+            "/db" => {
+                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()
+                    .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
+                Ok(res)
             }
-        })
+            "/fortunes" => {
+                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()
+                    .insert(CONTENT_TYPE, utils::HDR_HTML_CONTENT_TYPE);
+                Ok(res)
+            }
+            "/query" => {
+                let worlds = self
+                    .0
+                    .get_worlds(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()
+                    .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
+                Ok(res)
+            }
+            "/update" => {
+                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()
+                    .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
+                Ok(res)
+            }
+            _ => Ok(Response::new(StatusCode::NOT_FOUND)),
+        }
     }
 }
 
@@ -73,13 +70,12 @@ impl ServiceFactory<Request> for AppFactory {
     type Error = Error;
     type Service = App;
     type InitError = ();
-    type Future<'f> = BoxFuture<'f, Result<Self::Service, Self::InitError>>;
 
-    fn create(&self, _: ()) -> Self::Future<'_> {
+    async fn create(&self, _: ()) -> Result<Self::Service, Self::InitError> {
         const DB_URL: &str =
             "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
 
-        Box::pin(async move { Ok(App(db::PgConnection::connect(DB_URL).await)) })
+        Ok(App(db::PgConnection::connect(DB_URL).await))
     }
 }
 
@@ -97,6 +93,8 @@ async fn main() -> std::io::Result<()> {
             HttpService::build()
                 .keep_alive(KeepAlive::Os)
                 .client_timeout(Seconds(0))
+                .headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
+                .payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
                 .h1(AppFactory)
         })?
         .workers(num_cpus::get())