Giter Site home page Giter Site logo

Comments (4)

dariusc93 avatar dariusc93 commented on June 27, 2024

I do like the idea of this being documented but I do wonder if we should make sizes configurable, either as a const or adding fields to Codec for the size

from rust-libp2p.

caementarius avatar caementarius commented on June 27, 2024

I do like the idea of this being documented but I do wonder if we should make sizes configurable, either as a const or adding fields to Codec for the size

I like the idea too, it would require more work but it might be usefull

from rust-libp2p.

dariusc93 avatar dariusc93 commented on June 27, 2024

I like the idea too, it would require more work but it might be usefull

Though outside the scope of this issue, I would imagine it being something along the lines of the following (give or take):

diff --git a/protocols/request-response/src/cbor.rs b/protocols/request-response/src/cbor.rs
index 44d82be2..aaad251c 100644
--- a/protocols/request-response/src/cbor.rs
+++ b/protocols/request-response/src/cbor.rs
@@ -56,21 +56,30 @@ mod codec {
     /// Max response size in bytes
     const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;
 
+    #[derive(Clone)]
     pub struct Codec<Req, Resp> {
+        max_request_size: u64,
+        max_response_size: u64,
         phantom: PhantomData<(Req, Resp)>,
     }
 
     impl<Req, Resp> Default for Codec<Req, Resp> {
         fn default() -> Self {
             Codec {
+                max_response_size: RESPONSE_SIZE_MAXIMUM,
+                max_request_size: REQUEST_SIZE_MAXIMUM,
                 phantom: PhantomData,
             }
         }
     }
 
-    impl<Req, Resp> Clone for Codec<Req, Resp> {
-        fn clone(&self) -> Self {
-            Self::default()
+    impl<Req, Resp> Codec<Req, Resp> {
+        pub fn new(max_request: u64, max_response: u64) -> Self {
+            Self {
+                max_request_size: max_request,
+                max_response_size: max_response,
+                phantom: PhantomData
+            }
         }
     }
 
@@ -90,7 +99,7 @@ mod codec {
         {
             let mut vec = Vec::new();
 
-            io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
+            io.take(self.max_request_size).read_to_end(&mut vec).await?;
 
             cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error)
         }
@@ -101,7 +110,7 @@ mod codec {
         {
             let mut vec = Vec::new();
 
-            io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
+            io.take(self.max_response_size).read_to_end(&mut vec).await?;
 
             cbor4ii::serde::from_slice(vec.as_slice()).map_err(decode_into_io_error)
         }
diff --git a/protocols/request-response/src/json.rs b/protocols/request-response/src/json.rs
index 85e78e7d..09c1f45f 100644
--- a/protocols/request-response/src/json.rs
+++ b/protocols/request-response/src/json.rs
@@ -54,21 +54,30 @@ mod codec {
     /// Max response size in bytes
     const RESPONSE_SIZE_MAXIMUM: u64 = 10 * 1024 * 1024;
 
+    #[derive(Clone)]
     pub struct Codec<Req, Resp> {
+        max_request_size: u64,
+        max_response_size: u64,
         phantom: PhantomData<(Req, Resp)>,
     }
 
     impl<Req, Resp> Default for Codec<Req, Resp> {
         fn default() -> Self {
             Codec {
+                max_response_size: RESPONSE_SIZE_MAXIMUM,
+                max_request_size: REQUEST_SIZE_MAXIMUM,
                 phantom: PhantomData,
             }
         }
     }
 
-    impl<Req, Resp> Clone for Codec<Req, Resp> {
-        fn clone(&self) -> Self {
-            Self::default()
+    impl<Req, Resp> Codec<Req, Resp> {
+        pub fn new(max_request: u64, max_response: u64) -> Self {
+            Self {
+                max_request_size: max_request,
+                max_response_size: max_response,
+                phantom: PhantomData
+            }
         }
     }
 
@@ -88,7 +97,7 @@ mod codec {
         {
             let mut vec = Vec::new();
 
-            io.take(REQUEST_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
+            io.take(self.max_request_size).read_to_end(&mut vec).await?;
 
             Ok(serde_json::from_slice(vec.as_slice())?)
         }
@@ -99,7 +108,7 @@ mod codec {
         {
             let mut vec = Vec::new();
 
-            io.take(RESPONSE_SIZE_MAXIMUM).read_to_end(&mut vec).await?;
+            io.take(self.max_response_size).read_to_end(&mut vec).await?;
 
             Ok(serde_json::from_slice(vec.as_slice())?)
         }

from rust-libp2p.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.