Skip to content

Commit 536f6cf

Browse files
authored
ntex: Upgrade to ntex-2.0 (#9185)
1 parent 31599d2 commit 536f6cf

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

frameworks/Rust/ntex/Cargo.toml

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

66
[[bin]]
@@ -37,22 +37,23 @@ tokio = ["ntex/tokio"]
3737
async-std = ["ntex/async-std"]
3838

3939
[dependencies]
40-
ntex = "1.0.0"
40+
ntex = "=2.0.3"
4141
ntex-bytes = { version = "0.1.21", features=["simd"] }
4242
mimalloc = { version = "0.1.25", default-features = false }
4343
snmalloc-rs = { version = "0.3.3", features = ["native-cpu"] }
4444
yarte = { version = "0.15", features = ["bytes-buf", "json"] }
4545
buf-min = { version = "0.7", features = ["ntex-bytes"] }
46-
env_logger = "0.10"
46+
env_logger = "0.11"
4747
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
4848
atoi = "2.0"
4949
num_cpus = "1.16"
50-
smallvec = "1.11"
50+
smallvec = "1.13"
51+
futures = "0.3"
5152
serde = { version = "1.0", features = ["derive"] }
5253
serde_json = "1.0"
5354
log = { version = "0.4", features = ["release_max_level_off"] }
5455
tok_io = {version = "1", package = "tokio" }
55-
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-1.0" }
56+
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git", branch="ntex-2" }
5657

5758
[profile.release]
5859
opt-level = 3

frameworks/Rust/ntex/src/db.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::{cell::RefCell, fmt::Write as FmtWrite};
22

3+
use futures::stream::{futures_unordered::FuturesUnordered, StreamExt};
34
use nanorand::{Rng, WyRand};
45
use ntex::util::{BufMut, Bytes, BytesMut};
56
use smallvec::SmallVec;
67
use tokio_postgres::types::ToSql;
7-
use tokio_postgres::{connect, Client, Statement};
8+
use tokio_postgres::{connect, Client, Row, Statement};
89
use yarte::{ywrite_html, Serialize};
910

1011
use super::utils;
@@ -82,7 +83,7 @@ impl PgConnection {
8283
let row = self.cl.query_one(&self.world, &[&random_id]).await.unwrap();
8384

8485
let mut body = self.buf.borrow_mut();
85-
utils::reserve(&mut body);
86+
utils::reserve(&mut body, 256);
8687
World {
8788
id: row.get(0),
8889
randomnumber: row.get(1),
@@ -91,25 +92,28 @@ impl PgConnection {
9192
body.split().freeze()
9293
}
9394

95+
async fn get_one_world(&self, id: i32) -> Row {
96+
self.cl.query_one(&self.world, &[&id]).await.unwrap()
97+
}
98+
9499
pub async fn get_worlds(&self, num: usize) -> Bytes {
95100
let mut rng = self.rng.clone();
96-
let mut queries = SmallVec::<[_; 32]>::new();
101+
let mut queries = FuturesUnordered::new();
97102
(0..num).for_each(|_| {
98103
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
99-
queries.push(self.cl.query_one(&self.world, &[&w_id]));
104+
queries.push(self.get_one_world(w_id))
100105
});
101106

102107
let mut worlds = SmallVec::<[_; 32]>::new();
103-
for fut in queries {
104-
let row = fut.await.unwrap();
108+
while let Some(row) = queries.next().await {
105109
worlds.push(World {
106110
id: row.get(0),
107111
randomnumber: row.get(1),
108112
})
109113
}
110114

111115
let mut body = self.buf.borrow_mut();
112-
utils::reserve(&mut body);
116+
utils::reserve(&mut body, 8 * 1024);
113117
body.put_u8(b'[');
114118
worlds.iter().for_each(|w| {
115119
w.to_bytes_mut(&mut *body);
@@ -121,16 +125,15 @@ impl PgConnection {
121125
}
122126

123127
pub async fn update(&self, num: usize) -> Bytes {
124-
let mut rng = nanorand::tls_rng();
125-
let mut queries = SmallVec::<[_; 32]>::new();
128+
let mut rng = self.rng.clone();
129+
let mut queries = FuturesUnordered::new();
126130
(0..num).for_each(|_| {
127131
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
128-
queries.push(self.cl.query_one(&self.world, &[&w_id]));
132+
queries.push(self.get_one_world(w_id))
129133
});
130134

131135
let mut worlds = SmallVec::<[_; 32]>::new();
132-
for fut in queries.into_iter() {
133-
let row = fut.await.unwrap();
136+
while let Some(row) = queries.next().await {
134137
worlds.push(World {
135138
id: row.get(0),
136139
randomnumber: (rng.generate::<u32>() % 10_000 + 1) as i32,
@@ -148,7 +151,7 @@ impl PgConnection {
148151
let _ = self.cl.query(&self.updates[num - 1], &params).await;
149152

150153
let mut body = self.buf.borrow_mut();
151-
utils::reserve(&mut body);
154+
utils::reserve(&mut body, 8 * 1024);
152155
body.put_u8(b'[');
153156
worlds.iter().for_each(|w| {
154157
w.to_bytes_mut(&mut *body);
@@ -174,7 +177,7 @@ impl PgConnection {
174177
fortunes.sort_by(|it, next| it.message.cmp(next.message));
175178

176179
let mut body = std::mem::replace(&mut *self.buf.borrow_mut(), BytesMut::new());
177-
utils::reserve(&mut body);
180+
utils::reserve(&mut body, 8 * 1024);
178181
ywrite_html!(body, "{{> fortune }}");
179182
let result = body.split().freeze();
180183
let _ = std::mem::replace(&mut *self.buf.borrow_mut(), body);

frameworks/Rust/ntex/src/main_plt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Future for App {
3535
Ok((req, _)) => {
3636
let _ = this.io.with_write_buf(|buf| {
3737
buf.with_bytes_mut(|buf| {
38-
utils::reserve(buf);
38+
utils::reserve(buf, 2 * 1024);
3939
match req.path() {
4040
"/json" => {
4141
buf.extend_from_slice(JSON);

frameworks/Rust/ntex/src/utils.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub const HDR_HTML_CONTENT_TYPE: HeaderValue =
1111
HeaderValue::from_static("text/html; charset=utf-8");
1212
pub const BODY_PLAIN_TEXT: Bytes = Bytes::from_static(b"Hello, World!");
1313

14-
const LW: usize = 1024;
1514
const HW: usize = 128 * 1024;
1615
pub const SIZE: usize = 27;
1716

@@ -25,9 +24,9 @@ pub fn get_query_param(query: Option<&str>) -> usize {
2524
cmp::min(500, cmp::max(1, q) as usize)
2625
}
2726

28-
pub fn reserve(buf: &mut BytesMut) {
27+
pub fn reserve(buf: &mut BytesMut, lw: usize) {
2928
let remaining = buf.remaining_mut();
30-
if remaining < LW {
29+
if remaining < lw {
3130
buf.reserve(HW);
3231
}
3332
}

0 commit comments

Comments
 (0)