-
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 2 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,34 @@ | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum BooleanMode { | ||
| Integer, | ||
| SimpleString, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum DoubleMode { | ||
| BulkString, | ||
| IntegerIfWhole, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub enum MapMode { | ||
| FlatArray, | ||
| ArrayOfPairs, | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| pub struct DownlevelPolicy { | ||
| pub boolean_mode: BooleanMode, | ||
| pub double_mode: DoubleMode, | ||
| pub map_mode: MapMode, | ||
| } | ||
|
|
||
| impl Default for DownlevelPolicy { | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -370,6 +370,7 @@ impl RespEncode for RespEncoder { | |
| } | ||
| self.append_crlf() | ||
| } | ||
| _ => self, | ||
| } | ||
| } | ||
| } | ||
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,46 @@ | ||
| // 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. | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| use crate::{ | ||
| compat::DownlevelPolicy, | ||
| traits::{Decoder, Encoder}, | ||
| types::RespVersion, | ||
| }; | ||
|
|
||
| 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()), | ||
| } | ||
| } | ||
|
|
||
| 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), | ||
| } | ||
| } | ||
|
|
||
| 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), | ||
| } | ||
| } | ||
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,41 @@ | ||
| // 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. | ||
|
|
||
| use bytes::{Bytes, BytesMut}; | ||
|
|
||
| use crate::{ | ||
| error::RespResult, | ||
| traits::{Decoder, Encoder}, | ||
| types::RespData, | ||
| }; | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| pub fn encode_many(encoder: &mut dyn Encoder, values: &[RespData]) -> RespResult<Bytes> { | ||
| let mut buf = BytesMut::new(); | ||
| 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
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,52 @@ | ||
| use std::collections::VecDeque; | ||
|
|
||
| use bytes::Bytes; | ||
|
|
||
| use crate::{ | ||
| error::RespResult, | ||
| parse::{Parse, RespParse, RespParseResult}, | ||
| traits::Decoder, | ||
| types::{RespData, RespVersion}, | ||
| }; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct Resp1Decoder { | ||
| inner: RespParse, | ||
| out: VecDeque<RespResult<RespData>>, | ||
| } | ||
|
|
||
| impl Resp1Decoder { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| inner: RespParse::new(RespVersion::RESP1), | ||
| out: VecDeque::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Decoder for Resp1Decoder { | ||
| fn push(&mut self, data: Bytes) { | ||
| let mut res = self.inner.parse(data); | ||
| loop { | ||
| match res { | ||
| RespParseResult::Complete(d) => self.out.push_back(Ok(d)), | ||
| RespParseResult::Error(e) => self.out.push_back(Err(e)), | ||
| RespParseResult::Incomplete => break, | ||
| } | ||
| res = self.inner.parse(Bytes::new()); | ||
| } | ||
| } | ||
|
|
||
| fn next(&mut self) -> Option<RespResult<RespData>> { | ||
| self.out.pop_front() | ||
| } | ||
|
|
||
| fn reset(&mut self) { | ||
| self.inner.reset(); | ||
| self.out.clear(); | ||
| } | ||
|
|
||
| fn version(&self) -> RespVersion { | ||
| RespVersion::RESP1 | ||
| } | ||
| } |
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,123 @@ | ||
| use bytes::{Bytes, BytesMut}; | ||
|
|
||
| use crate::{ | ||
| compat::{BooleanMode, DoubleMode, DownlevelPolicy, MapMode}, | ||
| encode::{RespEncode, RespEncoder}, | ||
| error::RespResult, | ||
| traits::Encoder, | ||
| types::{RespData, RespVersion}, | ||
| }; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct Resp1Encoder { | ||
| inner: RespEncoder, | ||
| policy: DownlevelPolicy, | ||
| } | ||
|
|
||
| impl Resp1Encoder { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| inner: RespEncoder::new(RespVersion::RESP1), | ||
| policy: DownlevelPolicy::default(), | ||
| } | ||
| } | ||
|
|
||
| pub fn with_policy(policy: DownlevelPolicy) -> Self { | ||
| Self { | ||
| inner: RespEncoder::new(RespVersion::RESP1), | ||
| policy, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Encoder for Resp1Encoder { | ||
| fn encode_one(&mut self, data: &RespData) -> RespResult<Bytes> { | ||
| self.inner.clear(); | ||
| self.encode_downleveled(data); | ||
| Ok(self.inner.get_response()) | ||
| } | ||
|
|
||
| fn encode_into(&mut self, data: &RespData, out: &mut BytesMut) -> RespResult<()> { | ||
| let bytes = self.encode_one(data)?; | ||
| out.extend_from_slice(&bytes); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn version(&self) -> RespVersion { | ||
| RespVersion::RESP1 | ||
| } | ||
| } | ||
|
|
||
| impl Resp1Encoder { | ||
| fn encode_downleveled(&mut self, data: &RespData) { | ||
| match data { | ||
| // RESP1 uses inline/simple/integer/bulk/array semantics basically consistent | ||
| RespData::SimpleString(_) | ||
| | RespData::Error(_) | ||
| | RespData::Integer(_) | ||
| | RespData::BulkString(_) | ||
| | RespData::Array(_) | ||
| | RespData::Inline(_) => { | ||
| self.inner.encode_resp_data(data); | ||
| } | ||
| // Downlevel mapping strategy consistent with RESP2 | ||
| RespData::Null => { | ||
| self.inner.set_line_string("$-1"); | ||
| } | ||
| RespData::Boolean(b) => { | ||
| match self.policy.boolean_mode { | ||
| BooleanMode::Integer => self.inner.append_integer(if *b { 1 } else { 0 }), | ||
| BooleanMode::SimpleString => { | ||
| if *b { | ||
| self.inner.append_simple_string("OK") | ||
| } else { | ||
| self.inner.append_simple_string("ERR") | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| RespData::Double(v) => { | ||
| if let DoubleMode::IntegerIfWhole = self.policy.double_mode { | ||
| if v.fract() == 0.0 && v.is_finite() { | ||
| self.inner.append_integer(*v as i64); | ||
| return; | ||
| } | ||
| } | ||
| self.inner.append_string(&format!("{}", v)); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| crate::types::RespData::BulkError(msg) => { | ||
| self.inner | ||
| .append_string_raw(&format!("-{}\r\n", String::from_utf8_lossy(msg))); | ||
| } | ||
| crate::types::RespData::VerbatimString { data, .. } => { | ||
| self.inner.append_bulk_string(data); | ||
| } | ||
| crate::types::RespData::BigNumber(s) => { | ||
| self.inner.append_string(s); | ||
| } | ||
| crate::types::RespData::Map(entries) => match self.policy.map_mode { | ||
| MapMode::FlatArray => { | ||
| self.inner.append_array_len((entries.len() * 2) as i64); | ||
| for (k, v) in entries { | ||
| self.encode_downleveled(k); | ||
| self.encode_downleveled(v); | ||
| } | ||
| } | ||
| MapMode::ArrayOfPairs => { | ||
| self.inner.append_array_len(entries.len() as i64); | ||
| for (k, v) in entries { | ||
| self.inner.append_array_len(2); | ||
| self.encode_downleveled(k); | ||
| self.encode_downleveled(v); | ||
| } | ||
| } | ||
| }, | ||
| crate::types::RespData::Set(items) | crate::types::RespData::Push(items) => { | ||
| self.inner.append_array_len(items.len() as i64); | ||
| for it in items { | ||
| self.encode_downleveled(it); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,2 @@ | ||
| pub mod decoder; | ||
| pub mod encoder; |
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.