mirror of
https://github.com/alibaba/higress.git
synced 2026-05-11 22:37:32 +08:00
[feat] Support redis call with wasm-rust (#1417)
This commit is contained in:
@@ -28,29 +28,15 @@
|
||||
/// However, in the rules of event and field, there is an ambiguous grammar in the judgment of eol,
|
||||
/// and it will bring ambiguity (whether the field ends). In order to eliminate this ambiguity,
|
||||
/// we believe that CRLF as CR+LF belongs to event and field respectively.
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EventStream {
|
||||
buffer: Vec<u8>,
|
||||
processed_offset: usize,
|
||||
}
|
||||
|
||||
impl EventStream {
|
||||
pub fn new() -> Self {
|
||||
EventStream {
|
||||
buffer: Vec::new(),
|
||||
processed_offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Update the event stream by adding new data to the buffer and resetting processed offset if needed.
|
||||
pub fn update(&mut self, data: Vec<u8>) {
|
||||
if self.processed_offset > 0 {
|
||||
self.buffer.drain(0..self.processed_offset);
|
||||
self.processed_offset = 0;
|
||||
}
|
||||
|
||||
self.buffer.extend(data);
|
||||
}
|
||||
|
||||
impl Iterator for EventStream {
|
||||
type Item = Vec<u8>;
|
||||
/// Get the next event from the event stream. Return the event data if available, otherwise return None.
|
||||
/// Next will consume all the data in the current buffer. However, if there is a valid event at the end of the buffer,
|
||||
/// it will return the event directly even if the data after the next `update` could be considered part of the same event
|
||||
@@ -71,7 +57,8 @@ impl EventStream {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn next(&mut self) -> Option<Vec<u8>> {
|
||||
///
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let mut i = self.processed_offset;
|
||||
|
||||
while i < self.buffer.len() {
|
||||
@@ -86,6 +73,18 @@ impl EventStream {
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl EventStream {
|
||||
/// Update the event stream by adding new data to the buffer and resetting processed offset if needed.
|
||||
pub fn update(&mut self, data: Vec<u8>) {
|
||||
if self.processed_offset > 0 {
|
||||
self.buffer.drain(0..self.processed_offset);
|
||||
self.processed_offset = 0;
|
||||
}
|
||||
|
||||
self.buffer.extend(data);
|
||||
}
|
||||
|
||||
/// Flush the event stream and return any remaining unprocessed event data. Return None if there is none.
|
||||
pub fn flush(&mut self) -> Option<Vec<u8>> {
|
||||
@@ -138,7 +137,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_crlf_events() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"event1\n\nevent2\n\n".to_vec());
|
||||
|
||||
assert_eq!(parser.next(), Some(b"event1".to_vec()));
|
||||
@@ -147,7 +146,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_lf_events() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"event3\n\r\nevent4\r\n".to_vec());
|
||||
|
||||
assert_eq!(parser.next(), Some(b"event3".to_vec()));
|
||||
@@ -156,7 +155,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_partial_event() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"partial_event1".to_vec());
|
||||
|
||||
assert_eq!(parser.next(), None);
|
||||
@@ -167,7 +166,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_mixed_eol_events() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"event5\r\nevent6\r\n\r\nevent7\r\n".to_vec());
|
||||
|
||||
assert_eq!(parser.next(), Some(b"event5".to_vec()));
|
||||
@@ -177,7 +176,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_mixed2_eol_events() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"event5\r\nevent6\r\n".to_vec());
|
||||
assert_eq!(parser.next(), Some(b"event5".to_vec()));
|
||||
assert_eq!(parser.next(), Some(b"event6".to_vec()));
|
||||
@@ -188,7 +187,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_no_event() {
|
||||
let mut parser = EventStream::new();
|
||||
let mut parser = EventStream::default();
|
||||
parser.update(b"no_eol_in_this_string".to_vec());
|
||||
|
||||
assert_eq!(parser.next(), None);
|
||||
|
||||
Reference in New Issue
Block a user