Skip to content

Commit 2463dfb

Browse files
authored
Be nice to more standard http servers. (#448)
* Be nice to more standard http servers. Handle HTTP status codes coming from the runtime server in a way that plays nice with local development tools like the runtime interface emulator. Signed-off-by: David Calavera <[email protected]> * Enable http niciness only when binaries are compiled in debug mode. Signed-off-by: David Calavera <[email protected]>
1 parent 0bcc004 commit 2463dfb

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lambda-http/src/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
7272
.headers
7373
.get(http::header::HOST)
7474
.and_then(|s| s.to_str().ok())
75-
.or_else(|| ag.request_context.domain_name.as_deref())
75+
.or(ag.request_context.domain_name.as_deref())
7676
.unwrap_or_default();
7777

7878
let path = apigw_path_with_stage(&ag.request_context.stage, ag.raw_path.as_deref().unwrap_or_default());

lambda-runtime/src/lib.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,30 @@ where
9797
{
9898
let client = &self.client;
9999
tokio::pin!(incoming);
100-
while let Some(event) = incoming.next().await {
100+
while let Some(next_event_response) = incoming.next().await {
101101
trace!("New event arrived (run loop)");
102-
let event = event?;
102+
let event = next_event_response?;
103103
let (parts, body) = event.into_parts();
104104

105+
#[cfg(debug_assertions)]
106+
if parts.status == http::StatusCode::NO_CONTENT {
107+
// Ignore the event if the status code is 204.
108+
// This is a way to keep the runtime alive when
109+
// there are no events pending to be processed.
110+
continue;
111+
}
112+
113+
let body = hyper::body::to_bytes(body).await?;
114+
trace!("response body - {}", std::str::from_utf8(&body)?);
115+
116+
#[cfg(debug_assertions)]
117+
if parts.status.is_server_error() {
118+
error!("Lambda Runtime server returned an unexpected error");
119+
return Err(parts.status.to_string().into());
120+
}
121+
105122
let ctx: Context = Context::try_from(parts.headers)?;
106123
let ctx: Context = ctx.with_config(config);
107-
let body = hyper::body::to_bytes(body).await?;
108-
trace!("{}", std::str::from_utf8(&body)?); // this may be very verbose
109124
let body = serde_json::from_slice(&body)?;
110125

111126
let xray_trace_id = &ctx.xray_trace_id.clone();

0 commit comments

Comments
 (0)