Skip to content

Commit e676146

Browse files
committed
fix(middleware): address response relay review feedback
Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
1 parent 3f8a40e commit e676146

2 files changed

Lines changed: 328 additions & 58 deletions

File tree

  • crates

‎crates/openshell-supervisor-middleware/src/response.rs‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub struct HttpResponseInvocation {
7272
pub failed: bool,
7373
pub stage_disabled: bool,
7474
pub reason_code: Option<String>,
75+
pub failure_category: Option<String>,
7576
}
7677

7778
pub struct HttpResponsePreflightOutcome {
@@ -211,12 +212,18 @@ impl HttpResponseSession {
211212
})?;
212213
let deadline = Instant::now() + MAX_MIDDLEWARE_CHAIN_TIMEOUT;
213214
let output = self.process_units_from(0, vec![data], deadline).await?;
214-
if self.defer_output_until_finish {
215+
if !self.defer_output_until_finish {
216+
return Ok(output);
217+
}
218+
if self.requires_whole_body() {
215219
self.deferred_output.extend(output);
216-
Ok(Vec::new())
217-
} else {
218-
Ok(output)
220+
return Ok(Vec::new());
219221
}
222+
223+
self.defer_output_until_finish = false;
224+
let mut released = std::mem::take(&mut self.deferred_output);
225+
released.extend(output);
226+
Ok(released)
220227
}
221228

222229
/// Finalize every body stage, process normalized trailers, and end streams.
@@ -595,6 +602,7 @@ impl HttpResponseSession {
595602
failed: true,
596603
stage_disabled: true,
597604
reason_code: None,
605+
failure_category: Some(response_failure_category(reason).into()),
598606
});
599607
stage
600608
.end(HttpResponseSessionEndReason::MiddlewareFailure)
@@ -795,6 +803,7 @@ impl ChainRunner {
795803
failed: false,
796804
stage_disabled: false,
797805
reason_code: (!skip.reason_code.is_empty()).then_some(skip.reason_code),
806+
failure_category: None,
798807
});
799808
let mut skipped = HttpResponseStage {
800809
entry,
@@ -898,6 +907,7 @@ impl ChainRunner {
898907
failed: false,
899908
stage_disabled: false,
900909
reason_code: None,
910+
failure_category: None,
901911
});
902912
stages.push(HttpResponseStage {
903913
entry,
@@ -1338,6 +1348,7 @@ fn body_invocation(
13381348
failed: false,
13391349
stage_disabled: false,
13401350
reason_code: None,
1351+
failure_category: None,
13411352
}
13421353
}
13431354

@@ -1412,10 +1423,37 @@ fn collect_preflight_failure(
14121423
failed: true,
14131424
stage_disabled: true,
14141425
reason_code: None,
1426+
failure_category: Some(response_failure_category(reason).into()),
14151427
});
14161428
fail_closed.then(|| format!("middleware_failed: {reason}"))
14171429
}
14181430

1431+
fn response_failure_category(reason: &str) -> &'static str {
1432+
if reason == "middleware_session_capacity_exhausted" {
1433+
"session_capacity"
1434+
} else if reason.contains("over_capacity") {
1435+
"payload_capacity"
1436+
} else if reason.contains("timeout") {
1437+
"timeout"
1438+
} else if reason.contains("stream_closed")
1439+
|| reason.contains("stream closed")
1440+
|| reason.contains("transport")
1441+
|| reason.contains("unavailable")
1442+
{
1443+
"transport"
1444+
} else if matches!(
1445+
reason,
1446+
"bodyless_response"
1447+
| "partial_response"
1448+
| "content_coding_not_identity"
1449+
| "cache_control_no_transform"
1450+
) {
1451+
"response_not_inspectable"
1452+
} else {
1453+
"invalid_result"
1454+
}
1455+
}
1456+
14191457
fn empty_preflight_outcome(headers: Vec<HttpHeader>) -> HttpResponsePreflightOutcome {
14201458
HttpResponsePreflightOutcome {
14211459
allowed: true,
@@ -2057,9 +2095,20 @@ mod tests {
20572095
let pushed = session.push_body(original.clone()).await;
20582096
assert_eq!(pushed.is_ok(), allowed);
20592097
if allowed {
2060-
assert!(pushed.unwrap().is_empty());
2098+
assert_eq!(pushed.unwrap(), vec![original]);
2099+
assert!(!session.requires_whole_body());
2100+
for fill in [b'b', b'c'] {
2101+
let unit = vec![fill; MAX_HTTP_RESPONSE_STREAM_UNIT_BYTES];
2102+
assert_eq!(
2103+
session
2104+
.push_body(unit.clone())
2105+
.await
2106+
.expect("fail-open stage must release later units"),
2107+
vec![unit]
2108+
);
2109+
}
20612110
let finish = session.finish(Vec::new()).await.expect("fail-open finish");
2062-
assert_eq!(finish.body_units, vec![original]);
2111+
assert!(finish.body_units.is_empty());
20632112
}
20642113
}
20652114
}

0 commit comments

Comments
 (0)