Skip to content

Commit

Permalink
Merge pull request #374 from weibocom/dev_kv_fixbuf_20231017
Browse files Browse the repository at this point in the history
update handshake response
  • Loading branch information
viciousstar authored Nov 6, 2023
2 parents 0cb1ed3 + 9d5da8f commit 5ec1ba7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 10 additions & 0 deletions ds/src/mem/ring_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@ impl RingSlice {
mask,
}
}
/// 注意:本方法用vec的capacity作为cap,方便ManuallyDrop结构的恢复及正确drop
#[inline(always)]
pub fn from_vec(data: &Vec<u8>) -> Self {
let mut mem: RingSlice = data.as_slice().into();
// 这里面的cap是真实的cap
mem.cap = data.capacity() as u32;
mem
}

/// 注意:对于Vec请使用from_vec,本方法直接用slice的长度作为cap,对ManuallyDrop结构无法友好支持
#[inline(always)]
pub fn from_slice(data: &[u8]) -> Self {
let mut mem: RingSlice = data.into();
// 这里面的cap是真实的cap
mem.cap = data.len() as u32;
mem
}
#[inline(always)]
pub fn slice(&self, offset: usize, len: usize) -> RingSlice {
self.sub_slice(offset, len)
Expand Down
6 changes: 3 additions & 3 deletions protocol/src/kv/common/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ impl HandshakeResponse {
db_name: Option<RingSlice>,
auth_plugin: Option<AuthPlugin>,
mut capabilities: CapabilityFlags,
connect_attributes: Option<HashMap<String, String>>,
connect_attributes: Option<&HashMap<String, String>>,
) -> Self {
let scramble_buf =
if capabilities.contains(CapabilityFlags::CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA) {
Expand Down Expand Up @@ -1839,8 +1839,8 @@ impl HandshakeResponse {
// .map(|(k, v)| (RawBytes::new(k.into_bytes()), RawBytes::new(v.into_bytes())))
.map(|(k, v)| {
(
RawBytes::new(RingSlice::from_vec(&k.into_bytes())),
RawBytes::new(RingSlice::from_vec(&v.into_bytes())),
RawBytes::new(RingSlice::from_slice(k.as_bytes())),
RawBytes::new(RingSlice::from_slice(v.as_bytes())),
)
})
.collect()
Expand Down
3 changes: 2 additions & 1 deletion protocol/src/kv/rsppacket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ impl<'a, S: crate::Stream> ResponsePacket<'a, S> {
let client = self.client.as_ref().unwrap();
let user = client.get_user().unwrap_or_default().as_bytes().to_vec();
let db_name = client.get_db_name().unwrap_or_default().as_bytes().to_vec();
let conn_attrs = client.connect_attrs();

let handshake_response = HandshakeResponse::new(
scramble_buf,
Expand All @@ -393,7 +394,7 @@ impl<'a, S: crate::Stream> ResponsePacket<'a, S> {
Some(RingSlice::from_vec(&db_name)),
Some(auth_plugin.clone()),
client.capability_flags,
Some(client.connect_attrs()),
Some(&conn_attrs),
);
log::debug!("+++ kv handshake rsp: {}", handshake_response);
let mut buf: Vec<u8> = Vec::with_capacity(256);
Expand Down

0 comments on commit 5ec1ba7

Please sign in to comment.