Explorar o código

[rust/ntex] update to ntex-3.0 (#10357)

* ntex: update to ntex-3.0

* wip

* wip

* wip
Nikolay Kim hai 1 semana
pai
achega
e54375606b

+ 9 - 7
frameworks/Rust/ntex/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "ntex-bench"
-version = "2.1.0"
+version = "3.0.0"
 edition = "2018"
 
 [[bin]]
@@ -67,11 +67,10 @@ neon = ["ntex/neon"]
 neon-uring = ["ntex/neon-uring"]
 
 [dependencies]
-ntex = "2.13"
-ntex-compio = "0.3"
-ntex-neon = "0.1.31"
-ntex-net = "2.5.27"
-ntex-bytes = { version = "0.1.21", features=["simd"] }
+ntex = "3.0.0-pre.5"
+ntex-neon = "0.1.35"
+ntex-net = "3.0.0"
+ntex-bytes = { version = "1", 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"] }
@@ -86,11 +85,14 @@ serde = { version = "1", features = ["derive"] }
 serde_json = "1"
 log = { version = "0.4", features = ["release_max_level_off"] }
 tok_io = {version = "1", package = "tokio" }
-tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-2" }
+tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-3" }
 
 [target.'cfg(target_os = "linux")'.dependencies]
 compio-driver = { version = "*", features = ["io-uring"]}
 
+[patch.crates-io]
+buf-min = { git = "https://github.com/fafhrd91/buf-min.git" }
+
 [profile.release]
 opt-level = 3
 codegen-units = 1

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

@@ -39,7 +39,7 @@ pub struct PgConnection {
 
 impl PgConnection {
     pub async fn connect(db_url: &str) -> PgConnection {
-        let (cl, conn) = connect(db_url)
+        let (cl, conn) = connect(db_url, utils::db_config())
             .await
             .expect("can not connect to postgresql");
         ntex::rt::spawn(async move {

+ 4 - 12
frameworks/Rust/ntex/src/main.rs

@@ -2,7 +2,7 @@
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
 use ntex::http::header::{CONTENT_TYPE, SERVER};
-use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};
+use ntex::{http, util::BytesMut, web};
 use sonic_rs::Serialize;
 
 mod utils;
@@ -52,18 +52,10 @@ async fn main() -> std::io::Result<()> {
     ntex::server::build()
         .backlog(1024)
         .enable_affinity()
-        .bind("techempower", "0.0.0.0:8080", |cfg| {
-            cfg.memory_pool(PoolId::P1);
-            PoolId::P1.set_read_params(65535, 2048);
-            PoolId::P1.set_write_params(65535, 2048);
-
-            http::HttpService::build()
-                .keep_alive(http::KeepAlive::Os)
-                .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())
+        .bind("tfb", "0.0.0.0:8080", async |_| {
+            http::HttpService::h1(web::App::new().service(json).service(plaintext).finish())
         })?
+        .config("tfb", utils::config())
         .run()
         .await
 }

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

@@ -3,10 +3,9 @@
 static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
 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::PoolId};
+use ntex::http::{HttpService, Request, Response, StatusCode};
+use ntex::service::{cfg::SharedCfg, Service, ServiceCtx, ServiceFactory};
+use ntex::{web::Error, web::HttpResponse};
 
 mod db;
 mod utils;
@@ -64,13 +63,13 @@ impl Service<Request> for App {
 
 struct AppFactory;
 
-impl ServiceFactory<Request> for AppFactory {
+impl ServiceFactory<Request, SharedCfg> for AppFactory {
     type Response = Response;
     type Error = Error;
     type Service = App;
     type InitError = ();
 
-    async fn create(&self, _: ()) -> Result<Self::Service, Self::InitError> {
+    async fn create(&self, _: SharedCfg) -> Result<Self::Service, Self::InitError> {
         const DB_URL: &str =
             "postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
 
@@ -85,18 +84,8 @@ async fn main() -> std::io::Result<()> {
     ntex::server::build()
         .backlog(1024)
         .enable_affinity()
-        .bind("techempower", "0.0.0.0:8080", |cfg| {
-            cfg.memory_pool(PoolId::P1);
-            PoolId::P1.set_read_params(65535, 2048);
-            PoolId::P1.set_write_params(65535, 2048);
-
-            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)
-        })?
+        .bind("tfb", "0.0.0.0:8080", async |_| HttpService::h1(AppFactory))?
+        .config("tfb", utils::config())
         .run()
         .await
 }

+ 3 - 6
frameworks/Rust/ntex/src/main_plt.rs

@@ -3,7 +3,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
 use std::{future::Future, io, pin::Pin, task::ready, task::Context, task::Poll};
 
-use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::PoolId};
+use ntex::{fn_service, http::h1, io::Io, io::RecvError};
 use sonic_rs::Serialize;
 
 mod utils;
@@ -82,16 +82,13 @@ async fn main() -> io::Result<()> {
     ntex::server::build()
         .backlog(1024)
         .enable_affinity()
-        .bind("techempower", "0.0.0.0:8080", |cfg| {
-            cfg.memory_pool(PoolId::P1);
-            PoolId::P1.set_read_params(65535, 2048);
-            PoolId::P1.set_write_params(65535, 2048);
-
+        .bind("tfb", "0.0.0.0:8080", async |_| {
             fn_service(|io| App {
                 io,
                 codec: h1::Codec::default(),
             })
         })?
+        .config("tfb", utils::config())
         .run()
         .await
 }

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

@@ -2,7 +2,8 @@
 use std::{cmp, io, io::Write, mem::MaybeUninit, slice::from_raw_parts_mut};
 
 use atoi::FromRadix10;
-use ntex::{http::header::HeaderValue, util::BufMut, util::Bytes, util::BytesMut};
+use ntex::http::{header::HeaderValue, HttpServiceConfig, KeepAlive};
+use ntex::{io::IoConfig, time::Seconds, util::BufMut, util::Bytes, util::BytesMut, SharedCfg};
 use sonic_rs::writer::WriteExt;
 
 pub const HDR_SERVER: HeaderValue = HeaderValue::from_static("N");
@@ -15,6 +16,37 @@ pub const BODY_PLAIN_TEXT: Bytes = Bytes::from_static(b"Hello, World!");
 const HW: usize = 128 * 1024;
 pub const SIZE: usize = 27;
 
+pub fn config() -> SharedCfg {
+    thread_local! {
+        static CFG: SharedCfg = SharedCfg::new("tfb")
+        .add(
+            IoConfig::new()
+                .set_read_buf(65535, 2048, 128)
+                .set_write_buf(65535, 2048, 128),
+        )
+        .add(
+            HttpServiceConfig::new()
+                .set_keepalive(KeepAlive::Os)
+                .set_client_timeout(Seconds::ZERO)
+                .set_headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
+                .set_payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0),
+        ).into();
+    }
+    CFG.with(|cfg| *cfg)
+}
+
+pub fn db_config() -> SharedCfg {
+    thread_local! {
+        static CFG: SharedCfg = SharedCfg::new("tfb-db")
+        .add(
+            IoConfig::new()
+                .set_read_buf(65535, 2048, 128)
+                .set_write_buf(65535, 2048, 128),
+        ).into()
+    }
+    CFG.with(|cfg| *cfg)
+}
+
 pub fn get_query_param(query: Option<&str>) -> usize {
     let query = query.unwrap_or("");
     let q = if let Some(pos) = query.find('q') {