pub struct SseParser { /* private fields */ }Expand description
Stateful SSE byte-stream parser.
Feed bytes with SseParser::feed and poll complete frames with
SseParser::next_frame.
The parser buffers bytes internally until a complete line is available, then processes each line according to the SSE spec.
§Memory limits
The parser enforces a configurable maximum event size (default 16 MiB) to
prevent unbounded memory growth from malicious or malformed streams. When
the limit is exceeded, the current event is discarded and an error is
queued. A single line is refused the same way as soon as it grows past
the longest line an in-limit event can hold (the limit plus retry: ),
so a peer that never sends a newline gets an error rather than silence,
and the parser holds no more than that of it. Use
SseParser::with_max_event_size to configure the limit.
The internal frame queue is also bounded (default 4096 frames) to prevent OOM from streams that produce many oversized-event errors without the consumer draining them.
Implementations§
Source§impl SseParser
impl SseParser
Sourcepub fn new() -> SseParser
pub fn new() -> SseParser
Creates a new, empty SseParser with default limits (4 MiB max event size).
Sourcepub fn with_max_event_size(max_event_size: usize) -> SseParser
pub fn with_max_event_size(max_event_size: usize) -> SseParser
Creates a new SseParser with a custom maximum event size.
Events exceeding this limit will be discarded and an error queued.
Sourcepub const fn with_max_queued_frames(self, max: usize) -> SseParser
pub const fn with_max_queued_frames(self, max: usize) -> SseParser
Sets the maximum number of frames that can be buffered before the oldest frame is dropped. Prevents unbounded memory growth if the consumer is slower than the producer.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Returns the number of complete frames waiting to be consumed.
Sourcepub fn feed(&mut self, bytes: &[u8])
pub fn feed(&mut self, bytes: &[u8])
Feeds raw bytes from the SSE stream into the parser.
After calling feed, call SseParser::next_frame repeatedly until
it returns None to consume all complete frames.
Sourcepub fn incomplete_event_len(&self) -> Option<usize>
pub fn incomplete_event_len(&self) -> Option<usize>
Bytes of an event that has started but not been dispatched: data:
lines still waiting for their blank line, plus an unterminated line
that is not a comment. None when there is none.
What is left here when the byte stream ends is an event the server
did not finish writing. The SSE specification discards it at end of
stream, and so does the parser; this is how a caller finds out that
it did, instead of the loss passing unseen. An unterminated comment
(: keep-alive cut off before its newline) carries no event and is
not counted.
Sourcepub fn next_frame(&mut self) -> Option<Result<SseFrame, SseParseError>>
pub fn next_frame(&mut self) -> Option<Result<SseFrame, SseParseError>>
Returns the next complete SseFrame, or None if none are ready.
Returns Err if an event exceeded the maximum size limit.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SseParser
impl RefUnwindSafe for SseParser
impl Send for SseParser
impl Sync for SseParser
impl Unpin for SseParser
impl UnsafeUnpin for SseParser
impl UnwindSafe for SseParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request