-
-
Notifications
You must be signed in to change notification settings - Fork 325
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
async stream for watch events #83
Comments
Tried to do this manually, by following seanmonstar/reqwest#682 roughly, but could not make the signature allow both an early failure and return a stream. I did manage to make one helper function with the correct signature, but it didn't get me to the end: pub fn watch_unfold<T>(res: reqwest::Response) -> impl Stream<Item = Result<T>>
where
T: DeserializeOwned + Unpin
{
futures::stream::unfold(res, |mut resp| async move {
match resp.chunk().await {
Ok(Some(l)) => {
trace!("Chunk: {:?}", l);
return match serde_json::from_slice(&l) {
Ok(t) => Some((Ok(t), resp)),
Err(e) => {
warn!("{} {:?}", String::from_utf8_lossy(&l), e);
Some((Err(Error::from(ErrorKind::SerdeParse)), resp))
},
}
},
Ok(None) => return None,
Err(err) => return Some((Err(Error::from(ErrorKind::RequestSend)), resp)),
}
})
}
} +futures-util = "0.3.0"
+futures = "0.3.0" +use futures::future::TryFutureExt; // producer
+use futures::{future, stream, Future, Stream};
+use futures_util::TryStreamExt; // consumer |
Just to clarify, I suppose this is the reason I'm seeing this when trying to upgrade to 0.18.0 (after updating all my async/awaits)?
|
Yeah, the API is not yet to await each individual element. You currently need to await the |
Looking at your example above using I also went ahead and updated the rest of the code that needed updating with this change and the jobs example. If this is the sort of implementation you're looking for I'd be happy to make a PR and then iterate on it, though I might be slow over the next few days due to travel. |
Oh, nice! I was missing how to get the function that returned Happy to take a pr and help it get to a final state. I am also travelling a bit, but will try to at least respond ASAP. Interesting that watch needs to return a boxed mut stream. Do you know the reason for that? |
Sounds good to me, PR on the way! As for the mut boxed stream, the |
Oh, that makes sense! I've seen people require |
Closed in #92 and kube 0.21.0 |
One of the things that would be the biggest usability/performance improvements would be the being able to return watch events as they occur (without buffering the entire timeout call internally) and instead return some kind of
stream
fromrequest_events
.Goofed a little bit around this using https://github.com/tokio-rs/async-stream in #82 but I might not include this for the intial async/await rewrite as it seems rather hairy.
Here is the subset of the diff I was trying to get working on top of #82 👍
ultimately, got stopped short of weird macro-origin lifetime issues that I struggled to debug. if anyone have any ideas or suggestions for moving forward with a stream interface here, that would be really helpful.
The text was updated successfully, but these errors were encountered: