Skip to content

Commit e543756

Browse files
authored
[rust/ntex] update to ntex-3.0 (#10357)
* ntex: update to ntex-3.0 * wip * wip * wip
1 parent 80c46e9 commit e543756

File tree

6 files changed

+57
-45
lines changed

6 files changed

+57
-45
lines changed

frameworks/Rust/ntex/Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-bench"
3-
version = "2.1.0"
3+
version = "3.0.0"
44
edition = "2018"
55

66
[[bin]]
@@ -67,11 +67,10 @@ neon = ["ntex/neon"]
6767
neon-uring = ["ntex/neon-uring"]
6868

6969
[dependencies]
70-
ntex = "2.13"
71-
ntex-compio = "0.3"
72-
ntex-neon = "0.1.31"
73-
ntex-net = "2.5.27"
74-
ntex-bytes = { version = "0.1.21", features=["simd"] }
70+
ntex = "3.0.0-pre.5"
71+
ntex-neon = "0.1.35"
72+
ntex-net = "3.0.0"
73+
ntex-bytes = { version = "1", features=["simd"] }
7574
mimalloc = { version = "0.1.25", default-features = false }
7675
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
7776
yarte = { version = "0.15", features = ["bytes-buf", "json"] }
@@ -86,11 +85,14 @@ serde = { version = "1", features = ["derive"] }
8685
serde_json = "1"
8786
log = { version = "0.4", features = ["release_max_level_off"] }
8887
tok_io = {version = "1", package = "tokio" }
89-
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-2" }
88+
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-3" }
9089

9190
[target.'cfg(target_os = "linux")'.dependencies]
9291
compio-driver = { version = "*", features = ["io-uring"]}
9392

93+
[patch.crates-io]
94+
buf-min = { git = "https://github.com/fafhrd91/buf-min.git" }
95+
9496
[profile.release]
9597
opt-level = 3
9698
codegen-units = 1

frameworks/Rust/ntex/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct PgConnection {
3939

4040
impl PgConnection {
4141
pub async fn connect(db_url: &str) -> PgConnection {
42-
let (cl, conn) = connect(db_url)
42+
let (cl, conn) = connect(db_url, utils::db_config())
4343
.await
4444
.expect("can not connect to postgresql");
4545
ntex::rt::spawn(async move {

frameworks/Rust/ntex/src/main.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use ntex::http::header::{CONTENT_TYPE, SERVER};
5-
use ntex::{http, time::Seconds, util::BytesMut, util::PoolId, web};
5+
use ntex::{http, util::BytesMut, web};
66
use sonic_rs::Serialize;
77

88
mod utils;
@@ -52,18 +52,10 @@ async fn main() -> std::io::Result<()> {
5252
ntex::server::build()
5353
.backlog(1024)
5454
.enable_affinity()
55-
.bind("techempower", "0.0.0.0:8080", |cfg| {
56-
cfg.memory_pool(PoolId::P1);
57-
PoolId::P1.set_read_params(65535, 2048);
58-
PoolId::P1.set_write_params(65535, 2048);
59-
60-
http::HttpService::build()
61-
.keep_alive(http::KeepAlive::Os)
62-
.client_timeout(Seconds::ZERO)
63-
.headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
64-
.payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
65-
.h1(web::App::new().service(json).service(plaintext).finish())
55+
.bind("tfb", "0.0.0.0:8080", async |_| {
56+
http::HttpService::h1(web::App::new().service(json).service(plaintext).finish())
6657
})?
58+
.config("tfb", utils::config())
6759
.run()
6860
.await
6961
}

frameworks/Rust/ntex/src/main_db.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
44

55
use ntex::http::header::{CONTENT_TYPE, SERVER};
6-
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
7-
use ntex::service::{Service, ServiceCtx, ServiceFactory};
8-
use ntex::web::{Error, HttpResponse};
9-
use ntex::{time::Seconds, util::PoolId};
6+
use ntex::http::{HttpService, Request, Response, StatusCode};
7+
use ntex::service::{cfg::SharedCfg, Service, ServiceCtx, ServiceFactory};
8+
use ntex::{web::Error, web::HttpResponse};
109

1110
mod db;
1211
mod utils;
@@ -64,13 +63,13 @@ impl Service<Request> for App {
6463

6564
struct AppFactory;
6665

67-
impl ServiceFactory<Request> for AppFactory {
66+
impl ServiceFactory<Request, SharedCfg> for AppFactory {
6867
type Response = Response;
6968
type Error = Error;
7069
type Service = App;
7170
type InitError = ();
7271

73-
async fn create(&self, _: ()) -> Result<Self::Service, Self::InitError> {
72+
async fn create(&self, _: SharedCfg) -> Result<Self::Service, Self::InitError> {
7473
const DB_URL: &str =
7574
"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
7675

@@ -85,18 +84,8 @@ async fn main() -> std::io::Result<()> {
8584
ntex::server::build()
8685
.backlog(1024)
8786
.enable_affinity()
88-
.bind("techempower", "0.0.0.0:8080", |cfg| {
89-
cfg.memory_pool(PoolId::P1);
90-
PoolId::P1.set_read_params(65535, 2048);
91-
PoolId::P1.set_write_params(65535, 2048);
92-
93-
HttpService::build()
94-
.keep_alive(KeepAlive::Os)
95-
.client_timeout(Seconds(0))
96-
.headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
97-
.payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
98-
.h1(AppFactory)
99-
})?
87+
.bind("tfb", "0.0.0.0:8080", async |_| HttpService::h1(AppFactory))?
88+
.config("tfb", utils::config())
10089
.run()
10190
.await
10291
}

frameworks/Rust/ntex/src/main_plt.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
33

44
use std::{future::Future, io, pin::Pin, task::ready, task::Context, task::Poll};
55

6-
use ntex::{fn_service, http::h1, io::Io, io::RecvError, util::PoolId};
6+
use ntex::{fn_service, http::h1, io::Io, io::RecvError};
77
use sonic_rs::Serialize;
88

99
mod utils;
@@ -82,16 +82,13 @@ async fn main() -> io::Result<()> {
8282
ntex::server::build()
8383
.backlog(1024)
8484
.enable_affinity()
85-
.bind("techempower", "0.0.0.0:8080", |cfg| {
86-
cfg.memory_pool(PoolId::P1);
87-
PoolId::P1.set_read_params(65535, 2048);
88-
PoolId::P1.set_write_params(65535, 2048);
89-
85+
.bind("tfb", "0.0.0.0:8080", async |_| {
9086
fn_service(|io| App {
9187
io,
9288
codec: h1::Codec::default(),
9389
})
9490
})?
91+
.config("tfb", utils::config())
9592
.run()
9693
.await
9794
}

frameworks/Rust/ntex/src/utils.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
use std::{cmp, io, io::Write, mem::MaybeUninit, slice::from_raw_parts_mut};
33

44
use atoi::FromRadix10;
5-
use ntex::{http::header::HeaderValue, util::BufMut, util::Bytes, util::BytesMut};
5+
use ntex::http::{header::HeaderValue, HttpServiceConfig, KeepAlive};
6+
use ntex::{io::IoConfig, time::Seconds, util::BufMut, util::Bytes, util::BytesMut, SharedCfg};
67
use sonic_rs::writer::WriteExt;
78

89
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!");
1516
const HW: usize = 128 * 1024;
1617
pub const SIZE: usize = 27;
1718

19+
pub fn config() -> SharedCfg {
20+
thread_local! {
21+
static CFG: SharedCfg = SharedCfg::new("tfb")
22+
.add(
23+
IoConfig::new()
24+
.set_read_buf(65535, 2048, 128)
25+
.set_write_buf(65535, 2048, 128),
26+
)
27+
.add(
28+
HttpServiceConfig::new()
29+
.set_keepalive(KeepAlive::Os)
30+
.set_client_timeout(Seconds::ZERO)
31+
.set_headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
32+
.set_payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0),
33+
).into();
34+
}
35+
CFG.with(|cfg| *cfg)
36+
}
37+
38+
pub fn db_config() -> SharedCfg {
39+
thread_local! {
40+
static CFG: SharedCfg = SharedCfg::new("tfb-db")
41+
.add(
42+
IoConfig::new()
43+
.set_read_buf(65535, 2048, 128)
44+
.set_write_buf(65535, 2048, 128),
45+
).into()
46+
}
47+
CFG.with(|cfg| *cfg)
48+
}
49+
1850
pub fn get_query_param(query: Option<&str>) -> usize {
1951
let query = query.unwrap_or("");
2052
let q = if let Some(pos) = query.find('q') {

0 commit comments

Comments
 (0)