Files
leleka/control-plane/src/native_events.rs
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

50 lines
1.9 KiB
Rust

//! Bounded reader for SSE emitted by our own native Responses pipeline.
use axum::body::Body;
use http_body::Body as _;
use serde_json::Value;
use std::{future::poll_fn, pin::Pin};
/// Transport comments keep silent prefill/buffered output connections active.
/// They carry no model data, usage, sequence number, or completion semantics.
pub(crate) fn keep_alive() -> axum::response::sse::KeepAlive {
axum::response::sse::KeepAlive::new()
.interval(std::time::Duration::from_secs(15))
.text("keep-alive")
}
pub(crate) const MAX_EVENT: usize = 64 * 1024 * 1024;
pub(crate) struct NativeEvents<'a> {
body: &'a mut Body,
buffer: Vec<u8>,
}
impl<'a> NativeEvents<'a> {
pub(crate) fn new(body: &'a mut Body) -> Self {
Self {
body,
buffer: Vec::new(),
}
}
pub(crate) async fn next(&mut self) -> Result<Value, &'static str> {
loop {
while let Some(end) = self.buffer.windows(2).position(|w| w == b"\n\n") {
let frame: Vec<_> = self.buffer.drain(..end + 2).collect();
let text = std::str::from_utf8(&frame).map_err(|_| "native stream is not UTF-8")?;
if let Some(data) = text.lines().find_map(|line| line.strip_prefix("data: ")) {
return serde_json::from_str(data).map_err(|_| "invalid native stream event");
}
}
let frame = poll_fn(|cx| Pin::new(&mut *self.body).poll_frame(cx))
.await
.ok_or("native stream ended without a terminal event")?
.map_err(|_| "native stream read failed")?;
let Ok(data) = frame.into_data() else {
continue;
};
if data.len() > MAX_EVENT - self.buffer.len() {
return Err("native stream event exceeds 64 MiB");
}
self.buffer.extend_from_slice(&data);
}
}
}