Skip to content

Commit

Permalink
feat: Allow configurable request/response sizes for json and cbor codec.
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Dec 2, 2023
1 parent 4f0013c commit 91c2604
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
2 changes: 2 additions & 0 deletions protocols/request-response/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.26.1

- Allow configurable request and response sizes for `json` and `cbor` codec.
See [PR XXXX](https://github.com/libp2p/rust-libp2p/pull/XXXX).
- Derive `PartialOrd` and `Ord` for `{Out,In}boundRequestId`.
See [PR 4956](https://github.com/libp2p/rust-libp2p/pull/4956).

Expand Down
35 changes: 27 additions & 8 deletions protocols/request-response/src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,41 @@ mod codec {
use serde::{de::DeserializeOwned, Serialize};
use std::{collections::TryReserveError, convert::Infallible, io, marker::PhantomData};

/// Max request size in bytes
const REQUEST_SIZE_MAXIMUM: u64 = 1024 * 1024;
/// Max response size in bytes
const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;

pub struct Codec<Req, Resp> {
/// Max request size in bytes
request_size_maximum: u64,
/// Max response size in bytes
response_size_maximum: u64,
phantom: PhantomData<(Req, Resp)>,
}

impl<Req, Resp> Default for Codec<Req, Resp> {
fn default() -> Self {
Codec {
request_size_maximum: 1024 * 1024,
response_size_maximum: 10 * 1024 * 1024,
phantom: PhantomData,
}
}
}

impl<Req, Resp> Clone for Codec<Req, Resp> {
fn clone(&self) -> Self {
Self::default()
Self {
request_size_maximum: self.request_size_maximum,
response_size_maximum: self.response_size_maximum,
phantom: PhantomData,
}
}
}

impl<Req, Resp> Codec<Req, Resp> {
pub fn new(request_size_maximum: u64, response_size_maximum: u64) -> Self {
Self {
request_size_maximum,
response_size_maximum,
..Default::default()
}
}
}

Expand All @@ -91,7 +106,9 @@ mod codec {
{
let mut vec = Vec::new();

io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
io.take(self.request_size_maximum)
.read_to_end(&mut vec)
.await?;

cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error)
}
Expand All @@ -102,7 +119,9 @@ mod codec {
{
let mut vec = Vec::new();

io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
io.take(self.response_size_maximum)
.read_to_end(&mut vec)
.await?;

cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error)
}
Expand Down
35 changes: 27 additions & 8 deletions protocols/request-response/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,41 @@ mod codec {
use serde::{de::DeserializeOwned, Serialize};
use std::{io, marker::PhantomData};

/// Max request size in bytes
const REQUEST_SIZE_MAXIMUM: u64 = 1024 * 1024;
/// Max response size in bytes
const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;

pub struct Codec<Req, Resp> {
/// Max request size in bytes
request_size_maximum: u64,
/// Max response size in bytes
response_size_maximum: u64,
phantom: PhantomData<(Req, Resp)>,
}

impl<Req, Resp> Default for Codec<Req, Resp> {
fn default() -> Self {
Codec {
request_size_maximum: 1024 * 1024,
response_size_maximum: 10 * 1024 * 1024,
phantom: PhantomData,
}
}
}

impl<Req, Resp> Clone for Codec<Req, Resp> {
fn clone(&self) -> Self {
Self::default()
Self {
request_size_maximum: self.request_size_maximum,
response_size_maximum: self.response_size_maximum,
phantom: self.phantom,
}
}
}

impl<Req, Resp> Codec<Req, Resp> {
pub fn new(request_size_maximum: u64, response_size_maximum: u64) -> Self {
Self {
request_size_maximum,
response_size_maximum,
..Default::default()
}
}
}

Expand All @@ -89,7 +104,9 @@ mod codec {
{
let mut vec = Vec::new();

io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
io.take(self.request_size_maximum)
.read_to_end(&mut vec)
.await?;

Ok(serde_json::from_slice(vec.as_slice())?)
}
Expand All @@ -100,7 +117,9 @@ mod codec {
{
let mut vec = Vec::new();

io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
io.take(self.response_size_maximum)
.read_to_end(&mut vec)
.await?;

Ok(serde_json::from_slice(vec.as_slice())?)
}
Expand Down

0 comments on commit 91c2604

Please sign in to comment.