Skip to content

Commit

Permalink
perf(serverless): small perf wins (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored Jun 18, 2023
1 parent 32a2c08 commit a232492
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-pears-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/serverless': patch
---

Add a fast path when handling assets, inline dashmap
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/runtime_utils/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::{
};

pub fn find_asset<'a>(url: &'a str, assets: &'a HashSet<String>) -> Option<&'a String> {
// Fast path to return early if there are no assets
if assets.len() == 0 {
return None;
}

// Remove the leading '/' from the url
let url = &url[1..];

Expand Down
3 changes: 2 additions & 1 deletion crates/serverless/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ metrics-exporter-prometheus = { version = "0.12.1", default-features = false, fe
log = { version = "0.4.19", features = ["std", "kv_unstable", "kv_unstable_serde"] }
anyhow = "1.0.71"
tokio-cron-scheduler = "0.9.4"
dashmap = "5.4.0"
# TODO: use a specific version of dashmap when the inline feature is released
dashmap = { git = "https://github.com/xacrimon/dashmap.git", rev = "7f9522c5286cfbbb78df15f00f87b8331cb875d6", features = ["inline"] }
futures = "0.3.28"
clickhouse = "0.11.5"
bytes = "1.4.0"
Expand Down
14 changes: 4 additions & 10 deletions crates/serverless/src/serverless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use futures::lock::Mutex;
use hyper::{
header::HOST,
http::response::Builder,
server::conn::AddrStream,
service::{make_service_fn, service_fn},
Body, Request, Response, Server,
};
Expand Down Expand Up @@ -98,7 +97,6 @@ async fn handle_error(

async fn handle_request(
req: Request<Body>,
ip: String,
deployments: Deployments,
last_requests: Arc<DashMap<String, Instant>>,
workers: Workers,
Expand All @@ -117,7 +115,7 @@ async fn handle_request(
"lagon_ignored_requests",
"reason" => "No hostname",
);
warn!(req = as_debug!(req), ip = ip, request = request_id; "No Host header found in request");
warn!(req = as_debug!(req), request = request_id; "No Host header found in request");

return Ok(Builder::new().status(404).body(PAGE_404.into())?);
}
Expand All @@ -131,7 +129,7 @@ async fn handle_request(
"reason" => "No deployment",
"hostname" => hostname.clone(),
);
warn!(req = as_debug!(req), ip = ip, hostname = hostname, request = request_id; "No deployment found for hostname");
warn!(req = as_debug!(req), hostname = hostname, request = request_id; "No deployment found for hostname");

return Ok(Response::builder().status(404).body(PAGE_404.into())?);
}
Expand All @@ -143,7 +141,7 @@ async fn handle_request(
"reason" => "Cron",
"hostname" => hostname.clone(),
);
warn!(req = as_debug!(req), ip = ip, hostname = hostname, request = request_id; "Cron deployment cannot be called directly");
warn!(req = as_debug!(req), hostname = hostname, request = request_id; "Cron deployment cannot be called directly");

return Ok(Response::builder().status(403).body(PAGE_403.into())?);
}
Expand Down Expand Up @@ -435,21 +433,17 @@ where
}
});

let server = Server::bind(&addr).serve(make_service_fn(move |conn: &AddrStream| {
let server = Server::bind(&addr).serve(make_service_fn(move |_| {
let deployments = Arc::clone(&deployments);
let last_requests = Arc::clone(&last_requests);
let workers = Arc::clone(&workers);
let inserters = Arc::clone(&inserters);
let log_sender = log_sender.clone();

let addr = conn.remote_addr();
let ip = addr.ip().to_string();

async move {
Ok::<_, Infallible>(service_fn(move |req| {
handle_request(
req,
ip.clone(),
Arc::clone(&deployments),
Arc::clone(&last_requests),
Arc::clone(&workers),
Expand Down

0 comments on commit a232492

Please sign in to comment.