Skip to content

Commit

Permalink
chore: optimized some performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zavakid committed Feb 8, 2025
1 parent 9ddf471 commit 9259f4a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ httparse = "1.8.0"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

tokio = {version = "1", features = ["rt-multi-thread", "net", "io-util", "macros", "sync", "signal", "test-util"] }
tokio = {version = "1", features = ["full", "tracing"] }
async-trait = "0.1.83"
futures = "0.3.31"
bytes = "1.8.0"
bytes = "1.10.0"
pin-project-lite = "0.2.15"

dynosaur = "0.1"
Expand Down Expand Up @@ -64,3 +64,8 @@ micro-http = { path = "crates/http" }
debug = true
[profile.release.build-override]
debug = true
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
debug = true
3 changes: 2 additions & 1 deletion crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tracing-subscriber.workspace = true

bytes.workspace = true
tokio.workspace = true
tokio-util = { version = "0.7.12", features = ["codec", "io"] }
tokio-util = { version = "0.7.13", features = ["codec", "io", "tracing"] }
futures.workspace = true
trait-variant.workspace = true

Expand All @@ -34,6 +34,7 @@ thiserror.workspace = true
indoc = "2.0.5"
criterion = { workspace = true, features = ["async_tokio", "html_reports"] }


[[bench]]
name = "http_bench"
harness = false
2 changes: 1 addition & 1 deletion crates/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tower-layer.workspace = true

pin-project-lite.workspace = true

tokio = { workspace = true, features = ["time"] }
tokio = { workspace = true}
futures.workspace = true
async-trait.workspace = true
trait-variant.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/web/examples/return_uid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use http::StatusCode;
use micro_web::router::{get, post, Router};
use micro_web::{handler_fn, PathParams, Server};
use micro_web::date::DateServiceDecorator;

async fn empty_body() -> &'static str {
""
Expand All @@ -16,12 +17,14 @@ async fn default_handler() -> (&'static str, StatusCode) {

#[tokio::main]
async fn main() {

// Build router with multiple routes and handlers
let router = Router::builder()
.route("/", get(handler_fn(empty_body)))
.route("/user", post(handler_fn(empty_body)))
.route("/user/{id}", get(handler_fn(echo_uid)))
.route("/{*p}", get(handler_fn(default_handler)))
.with_global_decorator(DateServiceDecorator)
.build();

// Configure and start the server
Expand Down
4 changes: 2 additions & 2 deletions crates/web/src/date/date_service_decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl<H: RequestHandler> RequestHandler for DateResponseHandler<H> {
) -> Response<ResponseBody> {
let mut resp = self.handler.invoke(req, req_body).await;

self.date_service.with_http_date(|date_str| {
resp.headers_mut().insert(http::header::DATE, HeaderValue::try_from(date_str).unwrap());
self.date_service.with_http_date(|date_header_value| {
resp.headers_mut().insert(http::header::DATE, HeaderValue::from(date_header_value));
});

resp
Expand Down
11 changes: 7 additions & 4 deletions crates/web/src/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use arc_swap::ArcSwap;
use httpdate::fmt_http_date;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use http::HeaderValue;

mod date_service_decorator;

Expand All @@ -19,7 +20,7 @@ pub use date_service_decorator::DateServiceDecorator;
/// providing an efficient way to access formatted HTTP date strings without
/// formatting them on every request.
pub struct DateService {
current: Arc<ArcSwap<(SystemTime, String)>>,
current: Arc<ArcSwap<(SystemTime, HeaderValue)>>,
handle: tokio::task::JoinHandle<()>,
}

Expand All @@ -34,16 +35,18 @@ impl DateService {
pub(crate) fn new() -> Self {
let system_time = SystemTime::now();
let http_date = fmt_http_date(system_time);
let date_value = HeaderValue::try_from(http_date).expect("http_date should not fail");

let current = Arc::new(ArcSwap::new(Arc::new((system_time, http_date))));
let current = Arc::new(ArcSwap::from_pointee((system_time, date_value)));
let current_arc = Arc::clone(&current);

let handle = tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_millis(700)).await;
let system_time = SystemTime::now();
let http_date = fmt_http_date(system_time);
current_arc.store(Arc::new((system_time, http_date)));
let date_value = HeaderValue::try_from(http_date).expect("http_date should not fail");
current_arc.store(Arc::new((system_time, date_value)));
}
});

Expand All @@ -56,7 +59,7 @@ impl DateService {
/// the internal synchronization mechanisms.
pub(crate) fn with_http_date<F>(&self, mut f: F)
where
F: FnMut(&str),
F: FnMut(&HeaderValue),
{
let date = &self.current.load().1;
f(date)
Expand Down
2 changes: 1 addition & 1 deletion crates/web/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Server {
}

pub async fn start(self) {
let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish();
let subscriber = FmtSubscriber::builder().with_max_level(Level::WARN).finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

info!("start listening at {:?}", self.address);
Expand Down

0 comments on commit 9259f4a

Please sign in to comment.