Skip to content

Commit 04379e2

Browse files
committed
fix: replace bincode with postcard
1 parent e2f272b commit 04379e2

9 files changed

Lines changed: 432 additions & 416 deletions

File tree

Cargo.lock

Lines changed: 413 additions & 352 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ async-speed-limit = { version = "0.4", features = ["tokio"] }
1515
async-trait = "0.1"
1616
axum = "0.8"
1717
base64 = "0.22"
18-
bincode = "2"
1918
bytes = "1"
2019
clap = "4"
2120
derive_more = "2"
@@ -36,6 +35,7 @@ monitor_table = { git = "https://github.com/Banyc/monitor_table.git", tag = "v0.
3635
mptcp = { git = "https://github.com/Banyc/mptcp.git", tag = "v0.0.2" }
3736
mux = { git = "https://github.com/Banyc/mux.git", tag = "v0.0.10" }
3837
pin-project-lite = "0.2"
38+
postcard = "1"
3939
primitive = { git = "https://github.com/Banyc/primitive.git", tag = "v0.0.59" }
4040
rand = "0.9"
4141
regex = "1"

common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ ahash = { workspace = true }
1111
async-speed-limit = { workspace = true }
1212
async-trait = { workspace = true }
1313
base64 = { workspace = true }
14-
bincode = { workspace = true }
1514
bytes = { workspace = true }
1615
derive_more = { workspace = true, features = ["full"] }
1716
duplicate = { workspace = true }
@@ -22,6 +21,7 @@ hdv_derive = { workspace = true }
2221
metrics = { workspace = true }
2322
monitor_table = { workspace = true, features = ["hdv"] }
2423
pin-project-lite = { workspace = true }
24+
postcard = { workspace = true }
2525
primitive = { workspace = true }
2626
rand = { workspace = true }
2727
regex = { workspace = true }

common/src/addr.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ pub fn any_addr(ip_version: &IpAddr) -> SocketAddr {
4242
SocketAddr::new(any_ip, 0)
4343
}
4444

45-
#[derive(
46-
Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, bincode::Encode, bincode::Decode,
47-
)]
45+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
4846
pub struct InternetAddr(InternetAddrKind);
4947
impl Deref for InternetAddr {
5048
type Target = InternetAddrKind;
@@ -86,9 +84,7 @@ impl FromStr for InternetAddr {
8684
}
8785
}
8886

89-
#[derive(
90-
Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, bincode::Encode, bincode::Decode,
91-
)]
87+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
9288
#[serde(deny_unknown_fields)]
9389
pub enum InternetAddrKind {
9490
SocketAddr(SocketAddr),

common/src/header/codec.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ use std::{
55

66
use ae::anti_replay::ValidatorRef;
77
use duplicate::duplicate_item;
8+
use serde::{Serialize, de::DeserializeOwned};
89
use thiserror::Error;
910
use tokio::io::{AsyncRead, AsyncWrite};
1011
use tracing::{instrument, trace};
1112

1213
pub const MAX_HEADER_LEN: usize = 1024;
13-
const BINCODE_CONFIG: bincode::config::Configuration<
14-
bincode::config::LittleEndian,
15-
bincode::config::Varint,
16-
bincode::config::NoLimit,
17-
> = bincode::config::standard();
1814

1915
pub trait AsHeader {}
2016

@@ -31,7 +27,7 @@ pub async fn read_header<Reader, Header>(
3127
) -> Result<Header, CodecError>
3228
where
3329
Reader: reader_bounds,
34-
Header: std::fmt::Debug + AsHeader + bincode::Decode<()>,
30+
Header: std::fmt::Debug + AsHeader + DeserializeOwned,
3531
{
3632
let mut buf = [0; MAX_HEADER_LEN * 2];
3733
let mut start_pos = 0;
@@ -61,8 +57,7 @@ where
6157
},
6258
};
6359
let hdr_buf = &buf[start_pos..end_pos];
64-
let mut rdr = io::Cursor::new(hdr_buf);
65-
let header = bincode::decode_from_std_read(&mut rdr, BINCODE_CONFIG)?;
60+
let header = postcard::from_bytes(hdr_buf)?;
6661
trace!(?header, "Read header");
6762

6863
Ok(header)
@@ -81,13 +76,10 @@ pub async fn write_header<Writer, Header>(
8176
) -> Result<(), CodecError>
8277
where
8378
Writer: writer_bounds,
84-
Header: std::fmt::Debug + AsHeader + bincode::Encode,
79+
Header: std::fmt::Debug + AsHeader + Serialize,
8580
{
8681
let mut hdr_buf = [0; MAX_HEADER_LEN];
87-
let mut hdr_wtr = io::Cursor::new(&mut hdr_buf[..]);
88-
bincode::encode_into_std_write(header, &mut hdr_wtr, BINCODE_CONFIG)?;
89-
let len = hdr_wtr.position();
90-
let hdr_buf = &hdr_buf[..len as usize];
82+
let hdr_buf = postcard::to_slice(header, &mut hdr_buf)?;
9183

9284
let timestamped = true;
9385
let mut buf = [0; MAX_HEADER_LEN * 2];
@@ -121,7 +113,7 @@ pub async fn timed_read_header_async<Reader, Header>(
121113
) -> Result<Header, CodecError>
122114
where
123115
Reader: AsyncRead + Unpin,
124-
Header: std::fmt::Debug + AsHeader + bincode::Decode<()>,
116+
Header: std::fmt::Debug + AsHeader + DeserializeOwned,
125117
{
126118
let res = tokio::time::timeout(timeout, read_header_async(reader, key, validator)).await;
127119
match res {
@@ -141,7 +133,7 @@ pub async fn timed_write_header_async<Writer, Header>(
141133
) -> Result<(), CodecError>
142134
where
143135
Writer: AsyncWrite + Unpin,
144-
Header: std::fmt::Debug + AsHeader + bincode::Encode,
136+
Header: std::fmt::Debug + AsHeader + Serialize,
145137
{
146138
let res = tokio::time::timeout(timeout, write_header_async(writer, header, key)).await;
147139
match res {
@@ -157,10 +149,8 @@ where
157149
pub enum CodecError {
158150
#[error("IO error: {0}")]
159151
Io(#[from] io::Error),
160-
#[error("Encode error: {0}")]
161-
Encode(#[from] bincode::error::EncodeError),
162-
#[error("Decode error: {0}")]
163-
Decode(#[from] bincode::error::DecodeError),
152+
#[error("Codec error: {0}")]
153+
Codec(#[from] postcard::Error),
164154
#[error("Data tempered")]
165155
Integrity,
166156
}

common/src/header/heartbeat.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ where
5656
Ok(())
5757
}
5858

59-
#[derive(
60-
Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, bincode::Encode, bincode::Decode,
61-
)]
59+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6260
pub enum HeartbeatRequest {
6361
Noop,
6462
Upgrade,

common/src/header/route.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,24 @@ use thiserror::Error;
33

44
use super::codec::AsHeader;
55

6-
#[derive(
7-
Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, bincode::Encode, bincode::Decode,
8-
)]
6+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
97
pub struct RouteRequest<Addr> {
108
pub upstream: Option<Addr>,
119
}
1210
impl<Addr> AsHeader for RouteRequest<Addr> {}
1311

14-
#[derive(
15-
Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, bincode::Encode, bincode::Decode,
16-
)]
12+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
1713
pub struct RouteResponse {
1814
pub result: Result<(), RouteError>,
1915
}
2016
impl AsHeader for RouteResponse {}
2117

22-
#[derive(
23-
Debug,
24-
Error,
25-
Clone,
26-
PartialEq,
27-
Eq,
28-
Hash,
29-
Deserialize,
30-
Serialize,
31-
bincode::Encode,
32-
bincode::Decode,
33-
)]
18+
#[derive(Debug, Error, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
3419
#[error("{kind}")]
3520
pub struct RouteError {
3621
pub kind: RouteErrorKind,
3722
}
38-
#[derive(
39-
Debug,
40-
Error,
41-
Clone,
42-
PartialEq,
43-
Eq,
44-
Hash,
45-
Deserialize,
46-
Serialize,
47-
bincode::Encode,
48-
bincode::Decode,
49-
)]
23+
#[derive(Debug, Error, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
5024
pub enum RouteErrorKind {
5125
#[error("io error")]
5226
Io,

common/src/proto/addr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ impl StreamAddrBuilder {
2020
}
2121

2222
/// A stream address
23-
#[derive(
24-
Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize, bincode::Encode, bincode::Decode,
25-
)]
23+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
2624
pub struct StreamAddr {
2725
pub address: InternetAddr,
2826
pub stream_type: Arc<str>,

protocol/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ edition = "2024"
99
ae = { workspace = true }
1010
async-speed-limit = { workspace = true }
1111
async-trait = { workspace = true }
12-
bincode = { workspace = true }
1312
common = { path = "../common" }
1413
metrics = { workspace = true }
1514
mptcp = { workspace = true }

0 commit comments

Comments
 (0)