Skip to content

Commit 5455c13

Browse files
authored
upgrade to ntex 0.5 (#6988)
1 parent b502121 commit 5455c13

File tree

5 files changed

+41
-38
lines changed

5 files changed

+41
-38
lines changed

frameworks/Rust/ntex/Cargo.toml

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

66
[[bin]]
@@ -16,7 +16,7 @@ name = "ntex-raw"
1616
path = "src/main_raw.rs"
1717

1818
[dependencies]
19-
ntex = "0.4.0-b.12"
19+
ntex = "=0.5.0-b.0"
2020
mimalloc = { version = "0.1.25", default-features = false }
2121
snmalloc-rs = { version = "0.2.26", features = ["1mib", "native-cpu"] }
2222
yarte = { version = "0.15", features = ["bytes-buf", "json"] }
@@ -27,9 +27,10 @@ num_cpus = "1.13"
2727
futures = "0.3"
2828
http = "0.2"
2929
smallvec = "1.6.1"
30-
simd-json = "0.4.6"
30+
simd-json = "0.4.13"
3131
simd-json-derive = "0.2.2"
3232
serde = { version = "1.0", features = ["derive"] }
33+
serde_json = "1.0"
3334
log = { version = "0.4", features = ["release_max_level_off"] }
3435
tokio = { version = "1", default-features = false }
3536
tokio-postgres = { git="https://github.com/fafhrd91/postgres.git" }

frameworks/Rust/ntex/src/db.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use nanorand::{WyRand, RNG};
55
use ntex::util::{Bytes, BytesMut};
66
use smallvec::SmallVec;
77
use tokio_postgres::types::ToSql;
8-
use tokio_postgres::{connect, Client, NoTls, Statement};
8+
use tokio_postgres::{connect, Client, Statement};
99
use yarte::{ywrite_html, Serialize};
1010

11+
#[cfg(target_os = "macos")]
12+
use serde_json as simd_json;
13+
1114
use crate::utils::Writer;
1215

1316
#[derive(Copy, Clone, Serialize, Debug, serde::Serialize)]
@@ -33,7 +36,7 @@ pub struct PgConnection {
3336

3437
impl PgConnection {
3538
pub async fn connect(db_url: &str) -> PgConnection {
36-
let (cl, conn) = connect(db_url, NoTls)
39+
let (cl, conn) = connect(db_url)
3740
.await
3841
.expect("can not connect to postgresql");
3942
ntex::rt::spawn(conn.map(|_| ()));

frameworks/Rust/ntex/src/main.rs

Lines changed: 5 additions & 4 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::{HeaderValue, CONTENT_TYPE, SERVER};
5-
use ntex::{http, util::Bytes, web, time::Seconds};
5+
use ntex::{http, time::Seconds, util::Bytes, util::PoolId, web};
66
use yarte::Serialize;
77

88
mod utils;
@@ -42,17 +42,18 @@ async fn main() -> std::io::Result<()> {
4242
ntex::server::build()
4343
.backlog(1024)
4444
.bind("techempower", "0.0.0.0:8080", || {
45+
PoolId::P1.set_read_params(65535, 1024);
46+
PoolId::P1.set_write_params(65535, 1024);
47+
4548
http::HttpService::build()
4649
.keep_alive(http::KeepAlive::Os)
4750
.client_timeout(Seconds(0))
48-
.disconnect_timeout(Seconds(0))
49-
.buffer_params(65535, 65535, 1024)
5051
.h1(web::App::new()
5152
.service(json)
5253
.service(plaintext)
5354
.with_config(web::dev::AppConfig::default()))
54-
.tcp()
5555
})?
56+
.memory_pool("techempower", PoolId::P1)
5657
.start()
5758
.await
5859
}

frameworks/Rust/ntex/src/main_db.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ use futures::future::{ok, Future, FutureExt};
77
use ntex::http::header::{HeaderValue, CONTENT_TYPE, SERVER};
88
use ntex::http::{HttpService, KeepAlive, Request, Response};
99
use ntex::service::{Service, ServiceFactory};
10-
use ntex::{util::BytesMut, time::Seconds};
1110
use ntex::web::{Error, HttpResponse};
11+
use ntex::{time::Seconds, util::BytesMut, util::PoolId};
12+
13+
#[cfg(target_os = "macos")]
14+
use serde_json as simd_json;
1215

1316
mod db;
1417
mod utils;
@@ -98,14 +101,15 @@ async fn main() -> std::io::Result<()> {
98101
ntex::server::build()
99102
.backlog(1024)
100103
.bind("techempower", "0.0.0.0:8080", || {
104+
PoolId::P1.set_read_params(65535, 1024);
105+
PoolId::P1.set_write_params(65535, 1024);
106+
101107
HttpService::build()
102108
.keep_alive(KeepAlive::Os)
103109
.client_timeout(Seconds(0))
104-
.disconnect_timeout(Seconds(0))
105-
.buffer_params(65535, 65535, 1024)
106110
.h1(AppFactory)
107-
.tcp()
108111
})?
112+
.memory_pool("techempower", PoolId::P1)
109113
.start()
110114
.await
111115
}

frameworks/Rust/ntex/src/main_raw.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#[global_allocator]
22
static GLOBAL: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;
33

4-
use std::{cell::RefCell, future::Future, io, pin::Pin, rc::Rc, task::Context, task::Poll};
4+
use std::{future::Future, io, pin::Pin, task::Context, task::Poll};
55

6-
use ntex::{fn_service, time::Seconds, http::h1, rt::net::TcpStream};
7-
use ntex::framed::{ReadTask, State, WriteTask};
6+
use ntex::{fn_service, http::h1, io::Io, util::BufMut, util::PoolId};
87

98
mod utils;
109

10+
#[cfg(target_os = "macos")]
11+
use serde_json as simd_json;
12+
1113
const JSON: &[u8] =
1214
b"HTTP/1.1 200 OK\r\nServer: N\r\nContent-Type: application/json\r\nContent-Length: 27\r\n";
1315
const PLAIN: &[u8] =
@@ -22,7 +24,7 @@ pub struct Message {
2224
}
2325

2426
struct App {
25-
state: State,
27+
io: Io,
2628
codec: h1::Codec,
2729
}
2830

@@ -31,19 +33,18 @@ impl Future for App {
3133

3234
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
3335
let this = self.as_mut().get_mut();
34-
if !this.state.is_open() {
35-
this.state.close();
36+
if this.io.is_closed() {
3637
return Poll::Ready(Ok(()));
3738
}
3839

39-
let read = this.state.read();
40-
let write = this.state.write();
40+
let read = this.io.read();
41+
let write = this.io.write();
4142
loop {
4243
match read.decode(&this.codec) {
4344
Ok(Some((req, _))) => {
44-
write.with_buf(|buf| {
45+
let _ = write.with_buf(|buf| {
4546
// make sure we've got room
46-
let remaining = buf.capacity() - buf.len();
47+
let remaining = buf.remaining_mut();
4748
if remaining < 1024 {
4849
buf.reserve(65535 - remaining);
4950
}
@@ -73,16 +74,12 @@ impl Future for App {
7374
}
7475
Ok(None) => break,
7576
_ => {
76-
this.state.close();
77+
this.io.close();
7778
return Poll::Ready(Err(()));
7879
}
7980
}
8081
}
81-
if read.is_ready() {
82-
this.state.register_dispatcher(cx.waker());
83-
} else {
84-
read.wake(cx.waker())
85-
}
82+
let _ = read.poll_read_ready(cx);
8683
Poll::Pending
8784
}
8885
}
@@ -95,18 +92,15 @@ async fn main() -> io::Result<()> {
9592
ntex::server::build()
9693
.backlog(1024)
9794
.bind("techempower", "0.0.0.0:8080", || {
98-
fn_service(|io: TcpStream| {
99-
let state = State::with_params(65535, 65535, 1024, Seconds(0));
100-
let io = Rc::new(RefCell::new(io));
101-
ntex::rt::spawn(ReadTask::new(io.clone(), state.clone()));
102-
ntex::rt::spawn(WriteTask::new(io, state.clone()));
95+
PoolId::P1.set_read_params(65535, 8192);
96+
PoolId::P1.set_write_params(65535, 8192);
10397

104-
App {
105-
state,
106-
codec: h1::Codec::default(),
107-
}
98+
fn_service(|io| App {
99+
io,
100+
codec: h1::Codec::default(),
108101
})
109102
})?
103+
.memory_pool("techempower", PoolId::P1)
110104
.start()
111105
.await
112106
}

0 commit comments

Comments
 (0)