Skip to content

Commit

Permalink
[salvo] Update to version 0.68 (#9110)
Browse files Browse the repository at this point in the history
* [salvo] Update to version 0.68

* cargo fmt

* ok
  • Loading branch information
chrislearn committed Jun 11, 2024
1 parent 31ef34b commit 48316c2
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 63 deletions.
9 changes: 3 additions & 6 deletions frameworks/Rust/salvo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ anyhow = "1"
async-trait = "0.1"
bytes = "1"
diesel = { version = "2", features = ["postgres", "r2d2"] }
deadpool = { version = "0.11", features = ["rt_tokio_1", "serde", "managed"] }
deadpool-postgres = "0.13"
deadpool = { version = "0.12", features = ["rt_tokio_1", "serde", "managed"] }
deadpool-postgres = "0.14"
futures-util = "0.3"
lru = "0.12.0"
markup = "0.15"
# mimalloc = { version = "0.1", default-features = false }
mongodb = { version = "2.4.0", features = ["zstd-compression", "snappy-compression", "zlib-compression"] }
once_cell = "1"
rand = { version = "0.8", features = ["min_const_gen", "small_rng"] }
salvo = { version = "0.67", default-features = false, features = ["anyhow", "server", "http1", "affix"] }
salvo = { version = "0.68", default-features = false, features = ["anyhow", "server", "http1", "affix"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
# smallvec = "1"
Expand All @@ -64,7 +64,4 @@ dotenv = "0.15.0"

[profile.release]
lto = true
opt-level = 3
codegen-units = 1
panic = "abort"

2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-diesel.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_POSTGRES_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world
ENV TECHEMPOWER_MAX_POOL_SIZE=28
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-lru.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_POSTGRES_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world

Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-mongo-raw.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_MONGODB_URL=mongodb://tfb-database:27017
ENV TECHEMPOWER_MAX_POOL_SIZE=28
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-mongo.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_MONGODB_URL=mongodb://tfb-database:27017
ENV TECHEMPOWER_MAX_POOL_SIZE=28
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-pg-pool.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_POSTGRES_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world
ENV TECHEMPOWER_MAX_POOL_SIZE=28
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-pg.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_POSTGRES_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world

Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo-sqlx.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ENV TECHEMPOWER_POSTGRES_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world
ENV TECHEMPOWER_MAX_POOL_SIZE=56
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/salvo.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.77
FROM rust:1.78-buster

ADD ./ /salvo
WORKDIR /salvo
Expand Down
3 changes: 1 addition & 2 deletions frameworks/Rust/salvo/src/db_pg_pool.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use deadpool_postgres::{Client, Manager, ManagerConfig, RecyclingMethod};
use tokio_pg_mapper::FromTokioPostgresRow;
use tokio_postgres::{NoTls, Row, Error, Statement};
use tokio_postgres::{Error, NoTls, Row, Statement};

use crate::{Fortune, World};


pub async fn create_pool(database_url: String, max_pool_size: u32) -> deadpool_postgres::Pool {
let pg_config: tokio_postgres::Config = database_url.parse().expect("invalid database url");

Expand Down
2 changes: 1 addition & 1 deletion frameworks/Rust/salvo/src/db_sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sqlx::{
pool::PoolConnection,
postgres::{PgArguments, PgPoolOptions},
Arguments, PgPool, Postgres, Error
Arguments, Error, PgPool, Postgres,
};

use crate::{Fortune, World};
Expand Down
25 changes: 21 additions & 4 deletions frameworks/Rust/salvo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use std::sync::Arc;
use std::thread::available_parallelism;

use bytes::Bytes;
use salvo::conn::tcp::TcpAcceptor;
use salvo::http::body::ResBody;
use salvo::http::header::{self, HeaderValue};
use salvo::prelude::*;
use serde::Serialize;

mod utils;


#[derive(Serialize)]
pub struct Message {
pub message: &'static str,
Expand All @@ -35,22 +38,36 @@ fn plaintext(res: &mut Response) {
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
res.body(ResBody::Once(Bytes::from_static(b"Hello, world!")));
}
#[tokio::main]
async

fn main() {
let router = Arc::new(
Router::new()
.push(Router::with_path("plaintext").get(plaintext))
.push(Router::with_path("json").get(json)),
);

let thread_count = available_parallelism().map(|n| n.get()).unwrap_or(16);
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
for _ in 1..thread_count {
let router = router.clone();
std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.block_on(serve(router));
});
}
println!("Started http server: 127.0.0.1:8080");
serve(router).await;
rt.block_on(serve(router));
}

async fn serve(router: Arc<Router>) {
// let acceptor: TcpAcceptor = utils::reuse_listener().unwrap().try_into().unwrap();
let acceptor = TcpListener::new("0.0.0.0:8080").bind().await;
let acceptor: TcpAcceptor = utils::reuse_listener().unwrap().try_into().unwrap();
let mut server = Server::new(acceptor);
let http1 = server.http1_mut();
http1.pipeline_flush(true);
Expand Down
14 changes: 7 additions & 7 deletions frameworks/Rust/salvo/src/main_diesel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use std::thread::available_parallelism;
use anyhow::Error;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool, PoolError, PooledConnection};
use dotenv::dotenv;
use once_cell::sync::OnceCell;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use salvo::conn::tcp::TcpAcceptor;
use salvo::http::header::{self, HeaderValue};
use salvo::http::ResBody;
use salvo::prelude::*;
use dotenv::dotenv;

mod models_diesel;
mod schema;
Expand Down Expand Up @@ -135,7 +135,10 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {

let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res.body(ResBody::Once(Bytes::from(data)));
Ok(())
}
Expand Down Expand Up @@ -164,14 +167,11 @@ markup::define! {

fn main() {
dotenv().ok();

let db_url: String = utils::get_env_var("TECHEMPOWER_POSTGRES_URL");
let max_pool_size: u32 = utils::get_env_var("TECHEMPOWER_MAX_POOL_SIZE");
DB_POOL
.set(
create_pool(&db_url, max_pool_size)
.unwrap_or_else(|_| panic!("Error connecting to {}", &db_url)),
)
.set(create_pool(&db_url, max_pool_size).unwrap_or_else(|_| panic!("Error connecting to {}", &db_url)))
.ok();

let router = Arc::new(
Expand Down
5 changes: 4 additions & 1 deletion frameworks/Rust/salvo/src/main_mongo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ async fn fortunes(res: &mut Response, depot: &mut Depot) -> Result<(), Error> {

let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res.body(ResBody::Once(Bytes::from(data)));
Ok(())
}
Expand Down
37 changes: 8 additions & 29 deletions frameworks/Rust/salvo/src/main_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ impl WorldHandler {
}
#[async_trait]
impl Handler for WorldHandler {
async fn handle(
&self,
_req: &mut Request,
_depot: &mut Depot,
res: &mut Response,
_ctrl: &mut FlowCtrl,
) {
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let world = self.conn.get_world().await.unwrap();
let data = serde_json::to_vec(&world).unwrap();
let headers = res.headers_mut();
Expand All @@ -66,13 +60,7 @@ impl WorldsHandler {
}
#[async_trait]
impl Handler for WorldsHandler {
async fn handle(
&self,
req: &mut Request,
_depot: &mut Depot,
res: &mut Response,
_ctrl: &mut FlowCtrl,
) {
async fn handle(&self, req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let count = req.query::<u16>("q").unwrap_or(1);
let count = cmp::min(500, cmp::max(1, count));
let worlds = self.conn.get_worlds(count).await.unwrap();
Expand All @@ -99,13 +87,7 @@ impl UpdatesHandler {
}
#[async_trait]
impl Handler for UpdatesHandler {
async fn handle(
&self,
req: &mut Request,
_depot: &mut Depot,
res: &mut Response,
_ctrl: &mut FlowCtrl,
) {
async fn handle(&self, req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let count = req.query::<u16>("q").unwrap_or(1);
let count = cmp::min(500, cmp::max(1, count));
let worlds = self.conn.update(count).await.unwrap();
Expand All @@ -132,19 +114,16 @@ impl FortunesHandler {
}
#[async_trait]
impl Handler for FortunesHandler {
async fn handle(
&self,
_req: &mut Request,
_depot: &mut Depot,
res: &mut Response,
_ctrl: &mut FlowCtrl,
) {
async fn handle(&self, _req: &mut Request, _depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let mut data = String::new();
write!(&mut data, "{}", self.conn.tell_fortune().await.unwrap()).unwrap();

let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res.body(ResBody::Once(Bytes::from(data)));
}
}
Expand Down
9 changes: 6 additions & 3 deletions frameworks/Rust/salvo/src/main_pg_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::thread::available_parallelism;
use anyhow::Error;
use bytes::Bytes;
use deadpool_postgres::Pool;
use dotenv::dotenv;
use futures_util::{stream::FuturesUnordered, TryStreamExt};
use once_cell::sync::OnceCell;
use rand::rngs::SmallRng;
Expand All @@ -19,7 +20,6 @@ use salvo::conn::tcp::TcpAcceptor;
use salvo::http::header::{self, HeaderValue};
use salvo::http::ResBody;
use salvo::prelude::*;
use dotenv::dotenv;

mod db_pg_pool;
mod models_pg_pool;
Expand Down Expand Up @@ -129,7 +129,10 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {

let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res.body(ResBody::Once(Bytes::from(data)));
Ok(())
}
Expand Down Expand Up @@ -158,7 +161,7 @@ markup::define! {

fn main() {
dotenv().ok();

let db_url: String = utils::get_env_var("TECHEMPOWER_POSTGRES_URL");
let max_pool_size: u32 = utils::get_env_var("TECHEMPOWER_MAX_POOL_SIZE");
let rt = tokio::runtime::Builder::new_current_thread()
Expand Down
7 changes: 5 additions & 2 deletions frameworks/Rust/salvo/src/main_sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::thread::available_parallelism;

use anyhow::Error;
use bytes::Bytes;
use dotenv::dotenv;
use once_cell::sync::OnceCell;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
Expand All @@ -17,7 +18,6 @@ use salvo::http::header::{self, HeaderValue};
use salvo::http::ResBody;
use salvo::prelude::*;
use sqlx::PgPool;
use dotenv::dotenv;

mod db_sqlx;
mod models_sqlx;
Expand Down Expand Up @@ -62,7 +62,10 @@ async fn fortunes(res: &mut Response) -> Result<(), Error> {

let headers = res.headers_mut();
headers.insert(header::SERVER, HeaderValue::from_static("salvo"));
headers.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"));
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
);
res.body(ResBody::Once(Bytes::from(data)));
Ok(())
}
Expand Down

0 comments on commit 48316c2

Please sign in to comment.