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 4 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. 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() -> Self
pub fn new() -> Self
Creates a new, empty SseParser with default limits (4 MiB max event size).
Sourcepub fn with_max_event_size(max_event_size: usize) -> Self
pub fn with_max_event_size(max_event_size: usize) -> Self
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) -> Self
pub const fn with_max_queued_frames(self, max: usize) -> Self
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 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
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> 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