-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a body channel implementation that knows when buf is written #2858
Comments
At work, we run into this issue, where we wanted to get callback on body chunks and then on end of streaming. We implemented custom Either way, I'd be more than happy to tackle this. One thing I'd like to sort out first, is do we go with alerting via channel (if so, can you elaborate a bit more how you see it), or providing a callback the way we did. |
I think there's 2 parts to figure out, the easier one is the struct Alert<B: Buf> {
inner: B,
signal: SignalThingy,
}
impl<B: Buf> Buf for Alert {
fn advance(&mut self, cnt: usize) {
self.inner.advance(cnt);
if !self.inner.has_remaining() {
self.signal.thingy();
}
}
} The harder part is what is the signal thingy? Is it like a |
Alright, there is a fundamental flaw in the There is no method that will guarantee that the buffer was actually consumed. One is allowed to call Relying on a call to let buf = Bytes::from_static(b"abc");
loop {
let chunk_size = buf.chunk().len();
send(buf.chunk());
if chunk_size == buf.remaining() {
break;
}
} Notice that I could write such snippets for each of We would require something like: trait ConsumingBuf {
fn next_chunk(&mut self) -> &[u8] {
let chunk = self.inner.chunk();
self.inner.advance(chunk.len());
if self.inner.remaining() == 0 {
self.signal.thingy()
}
}
} (where |
It's true there's no guarantee, but hyper does make sure to always advance to the end. The thing with readiness-based IO is that we need to peek, because there might not be enough room in the socket's write buffer to send it all. |
It is very common to want a body type that allows for a push-based API, and knowledge of when those bytes have been written to the socket. We can provide a channel type that wraps the user's
B: Buf
in one that alerts the channel whenBuf::advance
has reached the end.The text was updated successfully, but these errors were encountered: