feat(client): make body reusable in some cases#1197
feat(client): make body reusable in some cases#1197
Conversation
ce162f5 to
398f667
Compare
|
Personally I would prefer a middleware solution for this. There are mainly two benefits for that approach in this case: simple design without touching too much in the core logic for better modularity. For this case I imagine a middleware solution could be like this snipet: // this is the core issue of a reusable stream that help to determine if a body can be possibly reused
pub(crate) trait LendingStream {
// the body clone happens inside the trait and depend on the implementor type
fn poll_next_lending() -> _;
fn poll_next() -> _;
}
pub struct BoxBody(Box<dyn LendingStream>)
struct AutoBodyMiddleware<S> {
// config for how large the body we want to keep in memory for possible reuse
max_size: usize,
http_service: S
}
impl Service<_> for AutoBody<_> {
async fn call(&self, _) -> _ {
let (low, up)= req.body().size_hint();
if up <= self.max_size {
// the middleware only help deciding if a lending iterator should be triggered.
body.set_can_be_lend();
}
}
}This is mostly a concept still as there is no mature and well accepted design for lending iterators in Rust. So for now it remains as a possible solution. |
|
Will try to look at this when a have time. And also maybe we could, in this middleware, add an |
|
I guess that could be an separate issue. For preserving body we can start by introducing more side effects to the dispatchers and hold onto |
This is a first attempt at having a clonable body.
This is mainly done by cloning
Byteswhich in most cases does not clone the underlying data, but only a pointer associated to itMaking it a draft for the moment as i think there may be a better way for the service request to handle all of this, i'm wondering if maybe we could return the request associated with response, and cloning parts there and body if reusable.
I also want to make a RequestBody::Stream into a RequestBody::Reusable (so we could create a middleware that ensure this everytime so we can reuse body safely in all cases)