Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions frameworks/Rust/ntex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex"
version = "0.3.0"
version = "0.4.0"
edition = "2018"

[[bin]]
Expand Down Expand Up @@ -37,21 +37,22 @@ tokio = ["ntex/tokio"]
async-std = ["ntex/async-std"]

[dependencies]
ntex = "0.5.30"
ntex = "0.6.1"
ntex-bytes = { version = "0.1.19", 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"] }
buf-min = { version = "0.7", features = ["ntex-bytes"] }
env_logger = "0.10"
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand"] }
nanorand = { version = "0.7", default-features = false, features = ["std", "wyrand", "tls"] }
atoi = "2.0"
num_cpus = "1.13"
smallvec = "1.6.1"
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-0.6" }

[profile.release]
opt-level = 3
Expand Down
3 changes: 1 addition & 2 deletions frameworks/Rust/ntex/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(clippy::uninit_vec)]
use std::{borrow::Cow, cell::RefCell, fmt::Write as FmtWrite};

use nanorand::{Rng, WyRand};
Expand Down Expand Up @@ -119,7 +118,7 @@ impl PgConnection {
}

pub async fn update(&self, num: usize) -> Bytes {
let mut rng = self.rng.clone();
let mut rng = nanorand::tls_rng();
let mut queries = SmallVec::<[_; 32]>::new();
(0..num).for_each(|_| {
let w_id = (rng.generate::<u32>() % 10_000 + 1) as i32;
Expand Down
31 changes: 11 additions & 20 deletions frameworks/Rust/ntex/src/main_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,43 @@
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::{future::Future, pin::Pin, rc::Rc, task::Context, task::Poll};

use ntex::http::header::{CONTENT_TYPE, SERVER};
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
use ntex::service::{Service, ServiceFactory};
use ntex::web::{Error, HttpResponse};
use ntex::{time::Seconds, util::PoolId};
use ntex::{time::Seconds, util::PoolId, util::BoxFuture};

mod db;
mod utils;

struct App(Rc<db::PgConnection>);
struct App(db::PgConnection);

impl Service<Request> for App {
type Response = Response;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Response, Error>>>>;

#[inline]
fn poll_ready(&self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&self, req: Request) -> Self::Future {
let db = self.0.clone();
type Future<'f> = BoxFuture<'f, Result<Response, Error>> where Self: 'f;

fn call(&self, req: Request) -> Self::Future<'_> {
Box::pin(async move {
match req.path() {
"/db" => {
let body = db.get_world().await;
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 = db.tell_fortune().await;
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 = db
let worlds = self.0
.get_worlds(utils::get_query_param(req.uri().query()))
.await;
let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
Expand All @@ -57,7 +48,7 @@ impl Service<Request> for App {
Ok(res)
}
"/update" => {
let worlds = db.update(utils::get_query_param(req.uri().query())).await;
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()
Expand All @@ -77,13 +68,13 @@ impl ServiceFactory<Request> for AppFactory {
type Error = Error;
type Service = App;
type InitError = ();
type Future = Pin<Box<dyn Future<Output = Result<Self::Service, Self::InitError>>>>;
type Future<'f> = BoxFuture<'f, Result<Self::Service, Self::InitError>>;

fn new_service(&self, _: ()) -> Self::Future {
fn create(&self, _: ()) -> Self::Future<'_> {
const DB_URL: &str =
"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";

Box::pin(async move { Ok(App(Rc::new(db::PgConnection::connect(DB_URL).await))) })
Box::pin(async move { Ok(App(db::PgConnection::connect(DB_URL).await)) })
}
}

Expand Down
6 changes: 4 additions & 2 deletions frameworks/Rust/ntex/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub const HDR_HTML_CONTENT_TYPE: HeaderValue =
HeaderValue::from_static("text/html; charset=utf-8");
pub const BODY_PLAIN_TEXT: Bytes = Bytes::from_static(b"Hello, World!");

const LW: usize = 1024;
const HW: usize = 128 * 1024;
pub const SIZE: usize = 27;

pub fn get_query_param(query: Option<&str>) -> usize {
Expand All @@ -25,7 +27,7 @@ pub fn get_query_param(query: Option<&str>) -> usize {

pub fn reserve(buf: &mut BytesMut) {
let remaining = buf.remaining_mut();
if remaining < 1024 {
buf.reserve(65535 - remaining);
if remaining < LW {
buf.reserve(HW);
}
}