-
Notifications
You must be signed in to change notification settings - Fork 33
feat(resp): implement full RESP3 support with unified architecture #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6bae406
feat(resp): implement full RESP3 support with unified architecture
Dayuxiaoshui 717bbb4
style(resp): fix code formatting in resp3_scaffold.rs
Dayuxiaoshui 24062a1
fix(resp): add Apache 2.0 license headers to all new files
Dayuxiaoshui 448006f
docs(resp): add comprehensive docstrings to improve coverage
Dayuxiaoshui 807e52f
style(resp): fix code formatting and linting issues
Dayuxiaoshui 65962bf
test(resp): improve test assertions and error messages
Dayuxiaoshui 7435bfc
fix(resp): prevent integer overflow in double casting
Dayuxiaoshui 6dc13eb
feat(resp): complete RESP3 implementation with full RESP type support
Dayuxiaoshui b890744
feat(resp): optimize encode_many performance with buffer pre-allocation
Dayuxiaoshui 431f94b
fix(resp): replace write! macro with extend_from_slice in RESP3 encoder
Dayuxiaoshui ec0c26c
fix(resp): refactor RESP3 decoder to support all RESP types in Map/Se…
Dayuxiaoshui c68e6c5
fix(resp): fix clippy warnings in RESP3 decoder
Dayuxiaoshui 4b40ed7
fix(resp): fix critical buffer corruption in RESP3 decoder
Dayuxiaoshui 8a6c004
fix(resp): implement stateful parsing to prevent data loss in collect…
Dayuxiaoshui 404ee40
fix(resp): remove unused import in incremental_parsing test
Dayuxiaoshui e2c8196
fix(resp): address CodeRabbit security and code quality issues
Dayuxiaoshui 2f81a4f
fix(resp): implement stack-based nested collection parsing
Dayuxiaoshui 9d54ad3
feat(resp): enhance DoS protection and fix CodeRabbit issues
Dayuxiaoshui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) 2024-present, arana-db Community. All rights reserved. | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| /// Defines how Boolean values should be converted when encoding to older RESP versions. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum BooleanMode { | ||
| /// Convert Boolean to Integer: true → :1, false → :0 | ||
| Integer, | ||
| /// Convert Boolean to Simple String: true → +OK, false → +ERR | ||
| SimpleString, | ||
| } | ||
|
|
||
| /// Defines how Double values should be converted when encoding to older RESP versions. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum DoubleMode { | ||
| /// Convert Double to Bulk String: 3.14 → $4\r\n3.14\r\n | ||
| BulkString, | ||
| /// Convert Double to Integer if whole number: 2.0 → :2, 2.5 → BulkString | ||
| IntegerIfWhole, | ||
| } | ||
|
|
||
| /// Defines how Map values should be converted when encoding to older RESP versions. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum MapMode { | ||
| /// Convert Map to flat Array: Map{k1:v1,k2:v2} → *4\r\nk1\r\nv1\r\nk2\r\nv2\r\n | ||
| FlatArray, | ||
| /// Convert Map to Array of pairs: Map{k1:v1,k2:v2} → *2\r\n*2\r\nk1\r\nv1\r\n*2\r\nk2\r\nv2\r\n | ||
| ArrayOfPairs, | ||
| } | ||
|
|
||
| /// Configuration for converting RESP3 types to older RESP versions. | ||
| /// | ||
| /// This policy defines how RESP3-specific types (Boolean, Double, Map, etc.) | ||
| /// should be represented when encoding to RESP1 or RESP2 protocols. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub struct DownlevelPolicy { | ||
| /// How to convert Boolean values | ||
| pub boolean_mode: BooleanMode, | ||
| /// How to convert Double values | ||
| pub double_mode: DoubleMode, | ||
| /// How to convert Map values | ||
| pub map_mode: MapMode, | ||
| } | ||
|
|
||
| impl Default for DownlevelPolicy { | ||
| /// Creates a default downlevel policy with conservative conversion settings. | ||
| /// | ||
| /// Default settings: | ||
| /// - Boolean → Integer (true → :1, false → :0) | ||
| /// - Double → BulkString (3.14 → $4\r\n3.14\r\n) | ||
| /// - Map → FlatArray (Map{k:v} → *2\r\nk\r\nv\r\n) | ||
| fn default() -> Self { | ||
| Self { | ||
| boolean_mode: BooleanMode::Integer, | ||
| double_mode: DoubleMode::BulkString, | ||
| map_mode: MapMode::FlatArray, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright (c) 2024-present, arana-db Community. All rights reserved. | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Factory functions for creating RESP protocol encoders and decoders. | ||
| //! | ||
| //! This module provides convenient factory functions to create version-specific | ||
| //! encoder and decoder instances based on the desired RESP protocol version. | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| use crate::{ | ||
| compat::DownlevelPolicy, | ||
| traits::{Decoder, Encoder}, | ||
| types::RespVersion, | ||
| }; | ||
|
|
||
| /// Create a new decoder for the specified RESP version. | ||
| /// | ||
| /// # Arguments | ||
| /// * `version` - The RESP protocol version to create a decoder for | ||
| /// | ||
| /// # Returns | ||
| /// A boxed decoder instance that implements the `Decoder` trait | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use resp::{RespVersion, new_decoder}; | ||
| /// | ||
| /// let mut decoder = new_decoder(RespVersion::RESP3); | ||
| /// decoder.push("+OK\r\n".into()); | ||
| /// ``` | ||
| pub fn new_decoder(version: RespVersion) -> Box<dyn Decoder> { | ||
| match version { | ||
| RespVersion::RESP1 => Box::new(crate::resp1::decoder::Resp1Decoder::default()), | ||
| RespVersion::RESP2 => Box::new(crate::resp2::decoder::Resp2Decoder::default()), | ||
| RespVersion::RESP3 => Box::new(crate::resp3::decoder::Resp3Decoder::default()), | ||
| } | ||
| } | ||
|
|
||
| /// Create a new encoder for the specified RESP version. | ||
| /// | ||
| /// # Arguments | ||
| /// * `version` - The RESP protocol version to create an encoder for | ||
| /// | ||
| /// # Returns | ||
| /// A boxed encoder instance that implements the `Encoder` trait | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use resp::{RespData, RespVersion, new_encoder}; | ||
| /// | ||
| /// let mut encoder = new_encoder(RespVersion::RESP3); | ||
| /// let bytes = encoder.encode_one(&RespData::Boolean(true)).unwrap(); | ||
| /// assert_eq!(bytes.as_ref(), b"#t\r\n"); | ||
| /// ``` | ||
| pub fn new_encoder(version: RespVersion) -> Box<dyn Encoder> { | ||
| match version { | ||
| RespVersion::RESP1 => Box::new(crate::resp1::encoder::Resp1Encoder::default()), | ||
| RespVersion::RESP2 => Box::new(crate::resp2::encoder::Resp2Encoder::default()), | ||
| RespVersion::RESP3 => Box::new(crate::resp3::encoder::Resp3Encoder::default()), | ||
| } | ||
| } | ||
|
|
||
| /// Create a new encoder for the specified RESP version with custom downlevel policy. | ||
| /// | ||
| /// # Arguments | ||
| /// * `version` - The RESP protocol version to create an encoder for | ||
| /// * `policy` - The downlevel compatibility policy for RESP3 types | ||
| /// | ||
| /// # Returns | ||
| /// A boxed encoder instance that implements the `Encoder` trait | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use resp::{BooleanMode, DownlevelPolicy, RespData, RespVersion, new_encoder_with_policy}; | ||
| /// | ||
| /// let policy = DownlevelPolicy { | ||
| /// boolean_mode: BooleanMode::SimpleString, | ||
| /// ..Default::default() | ||
| /// }; | ||
| /// let mut encoder = new_encoder_with_policy(RespVersion::RESP2, policy); | ||
| /// let bytes = encoder.encode_one(&RespData::Boolean(true)).unwrap(); // "+OK\r\n" | ||
| /// assert_eq!(bytes.as_ref(), b"+OK\r\n"); | ||
| /// ``` | ||
| pub fn new_encoder_with_policy(version: RespVersion, policy: DownlevelPolicy) -> Box<dyn Encoder> { | ||
| match version { | ||
| RespVersion::RESP1 => Box::new(crate::resp1::encoder::Resp1Encoder::with_policy(policy)), | ||
| RespVersion::RESP2 => Box::new(crate::resp2::encoder::Resp2Encoder::with_policy(policy)), | ||
| RespVersion::RESP3 => Box::new(crate::resp3::encoder::Resp3Encoder::default()), | ||
| } | ||
| } | ||
Dayuxiaoshui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright (c) 2024-present, arana-db Community. All rights reserved. | ||
| // | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Utilities for batch processing of RESP messages. | ||
| //! | ||
| //! This module provides functions for encoding and decoding multiple RESP messages | ||
| //! in a single operation, which is useful for pipelining and batch operations. | ||
|
|
||
| use bytes::{Bytes, BytesMut}; | ||
|
|
||
| use crate::{ | ||
| error::RespResult, | ||
| traits::{Decoder, Encoder}, | ||
| types::RespData, | ||
| }; | ||
|
|
||
| /// Decode multiple RESP messages from a single byte chunk. | ||
| /// | ||
| /// This function pushes the entire chunk into the decoder and attempts to parse | ||
| /// all complete messages available. Useful for processing pipelined commands. | ||
| /// | ||
| /// # Arguments | ||
| /// * `decoder` - The decoder instance to use for parsing | ||
| /// * `chunk` - The byte chunk containing one or more RESP messages | ||
| /// | ||
| /// # Returns | ||
| /// A vector of parsing results. Each element is either `Ok(RespData)` for a | ||
| /// successfully parsed message, or `Err(RespError)` for parsing errors. | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use resp::{RespVersion, decode_many, new_decoder}; | ||
| /// | ||
| /// let mut decoder = new_decoder(RespVersion::RESP2); | ||
| /// let results = decode_many(&mut *decoder, "+OK\r\n:42\r\n".into()); | ||
| /// assert_eq!(results.len(), 2); | ||
| /// ``` | ||
| pub fn decode_many(decoder: &mut dyn Decoder, chunk: Bytes) -> Vec<RespResult<RespData>> { | ||
| decoder.push(chunk); | ||
| let mut out = Vec::new(); | ||
| while let Some(frame) = decoder.next() { | ||
| out.push(frame); | ||
| } | ||
| out | ||
| } | ||
|
|
||
| /// Encode multiple RESP messages into a single byte buffer. | ||
| /// | ||
| /// This function encodes each `RespData` value in sequence and concatenates | ||
| /// the results into a single `Bytes` buffer. Useful for building pipelined commands. | ||
| /// | ||
| /// # Arguments | ||
| /// * `encoder` - The encoder instance to use for encoding | ||
| /// * `values` - A slice of `RespData` values to encode | ||
| /// | ||
| /// # Returns | ||
| /// A `Result` containing the concatenated encoded bytes, or an error if encoding fails. | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use resp::{RespData, RespVersion, encode_many, new_encoder}; | ||
| /// | ||
| /// let mut encoder = new_encoder(RespVersion::RESP2); | ||
| /// let values = vec![RespData::SimpleString("OK".into()), RespData::Integer(42)]; | ||
| /// let bytes = encode_many(&mut *encoder, &values).unwrap(); | ||
| /// // bytes contains "+OK\r\n:42\r\n" | ||
| /// assert_eq!(bytes.as_ref(), b"+OK\r\n:42\r\n"); | ||
| /// ``` | ||
| pub fn encode_many(encoder: &mut dyn Encoder, values: &[RespData]) -> RespResult<Bytes> { | ||
| let mut buf = BytesMut::with_capacity(values.len() * 32); // Rough estimate | ||
| for v in values { | ||
| encoder.encode_into(v, &mut buf)?; | ||
| } | ||
| Ok(buf.freeze()) | ||
| } | ||
Dayuxiaoshui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.