- Notifications
You must be signed in to change notification settings - Fork746
Add response wrapper type toTensorzeroHttpClient#5253
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Aaron1011 wants to merge2 commits intomainChoose a base branch fromaaron/response-wrapper
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+121 −13
Open
Changes from1 commit
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
Add response wrapper type to
TensorzeroHttpClientPreviously, calling the `send` method would return a`reqwest::Response` and drop our `LimitedClientTicket`.This will incorrectly signal that we now have additionalspace in the pool (even though the HTTP connection is stillneeded to read the response body). It also will interfere withstoring a `Span` to track the total HTTP duration (for the upcoming'tensorzero_overhead' metric)I've introduced a new wrapper type. It's similar to`TensorzeroRequestBuilder` - we hold on to our `LimitedClientTicket`,and expose accessor methods that forward to the underlying type.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit14b9fb66eb0b2f6b1e72796c984196105f5d3b01
There are no files selected for viewing
5 changes: 3 additions & 2 deletionstensorzero-core/src/client/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
110 changes: 102 additions & 8 deletionstensorzero-core/src/http.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| use chrono::Duration; | ||
| use http_body::{Frame, SizeHint}; | ||
| use once_cell::sync::OnceCell; | ||
| use opentelemetry_http::HeaderInjector; | ||
| use std::{ | ||
| @@ -13,7 +14,7 @@ use tracing::Span; | ||
| use tracing_futures::Instrument; | ||
| use futures::Stream; | ||
| use http::{HeaderMap, HeaderName, HeaderValue, StatusCode}; | ||
| use pin_project::pin_project; | ||
| use reqwest::{Body, Response}; | ||
| use reqwest::{Client, IntoUrl, NoProxy, Proxy, RequestBuilder}; | ||
| @@ -318,6 +319,94 @@ impl Stream for TensorZeroEventSource { | ||
| } | ||
| } | ||
| /// A wrapper type around `reqwest::Response` | ||
| /// We use this to extend the lifetime of a `Span`, | ||
| /// and drop it when the response is fully consumed | ||
| /// (e.g. after `text`) is called. | ||
| /// | ||
| // At the moment, we don't actually store a Span - this will | ||
| // be added in a future PR | ||
| pub struct TensorzeroResponseWrapper { | ||
| /// IMPORTANT - do *not* directly expose this field. | ||
| /// Instead, add accessor methods to `TensorzeroResponseWrapper`, | ||
| /// so that the caller is forced to hold on to the entire `TensorzeroResponseWrapper` | ||
| /// until it gets 'consumed' (e.g. calling `text`) | ||
| response: Response, | ||
| /// We hold onto a ticket, since holding a `Response` still uses a logical HTTP connection | ||
| /// (since the body will not be read until `text` is called) | ||
| ticket: LimitedClientTicket<'static>, | ||
| } | ||
| #[pin_project] | ||
| /// A wrapper over a `reqwest::Body` that holds on to a `LimitedClientTicket` | ||
| /// We use this to extend the lifetime of our ticket until the body is fully consumed | ||
| /// (since the underlying HTTP connection is still in use as long as we're reading data from the body) | ||
| pub struct TensorzeroBodyWrapper { | ||
| #[pin] | ||
| body: reqwest::Body, | ||
| ticket: LimitedClientTicket<'static>, | ||
| } | ||
| #[deny(clippy::missing_trait_methods)] | ||
| impl http_body::Body for TensorzeroBodyWrapper { | ||
| type Data = <reqwest::Body as http_body::Body>::Data; | ||
| type Error = <reqwest::Body as http_body::Body>::Error; | ||
| fn poll_frame( | ||
| self: Pin<&mut Self>, | ||
| cx: &mut Context<'_>, | ||
| ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> { | ||
| self.project().body.poll_frame(cx) | ||
| } | ||
| fn is_end_stream(&self) -> bool { | ||
| self.body.is_end_stream() | ||
| } | ||
| fn size_hint(&self) -> SizeHint { | ||
| self.body.size_hint() | ||
| } | ||
| } | ||
| impl TensorzeroResponseWrapper { | ||
| pub fn status(&self) -> StatusCode { | ||
| self.response.status() | ||
| } | ||
| pub fn headers(&self) -> &HeaderMap { | ||
| self.response.headers() | ||
| } | ||
| pub fn error_for_status_ref(&self) -> Result<&Self, reqwest::Error> { | ||
| self.response.error_for_status_ref()?; | ||
| Ok(self) | ||
| } | ||
| // These methods consume the `TensorzeroResponseWrapper`, | ||
| // and drop the ticket. They do *not* give the caller ownership of `self.response` | ||
| pub async fn text(self) -> Result<String, reqwest::Error> { | ||
| self.response.text().await | ||
| } | ||
| pub async fn json<T: DeserializeOwned>(self) -> Result<T, reqwest::Error> { | ||
| self.response.json().await | ||
| } | ||
| pub async fn bytes(self) -> Result<bytes::Bytes, reqwest::Error> { | ||
| self.response.bytes().await | ||
| } | ||
| /// Converts this `TensorzeroResponseWrapper` into an `http::Response<TensorzeroBodyWrapper>`. | ||
| /// preserving our `LimitedClientTicket` until the body is fully consumed | ||
Aaron1011 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| pub fn into_http_response(self) -> http::Response<TensorzeroBodyWrapper> { | ||
| let resp: http::Response<reqwest::Body> = self.response.into(); | ||
| resp.map(|body| TensorzeroBodyWrapper { | ||
| body, | ||
| ticket: self.ticket, | ||
| }) | ||
| } | ||
| } | ||
| // Workaround for https://github.com/hyperium/h2/issues/763 | ||
| // The 'h2' crate creates a long-lived span for outgoing HTTP connections. | ||
| // Due to connection pooling, these spans can live for a long time - | ||
| @@ -432,14 +521,19 @@ impl<'a> TensorzeroRequestBuilder<'a> { | ||
| }) | ||
| } | ||
| // This method preserves our ticket (by storing it in the `TensorzeroResponseWrapper`), | ||
| // since holding a `Reponse` still requires an active connection (since the | ||
Aaron1011 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // body will not be read until `text()` is called) | ||
| pub async fn send(mut self) -> Result<TensorzeroResponseWrapper, reqwest::Error> { | ||
| self = self.with_otlp_headers(); | ||
| Ok(TensorzeroResponseWrapper { | ||
| response: self | ||
| .builder | ||
| .send() | ||
| .instrument(tensorzero_h2_workaround_span()) | ||
| .await?, | ||
| ticket: self.ticket.into_owned(), | ||
| }) | ||
| } | ||
| pub async fn send_and_parse_json<T: DeserializeOwned>( | ||
2 changes: 1 addition & 1 deletiontensorzero-core/src/providers/aws_http_client.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionstensorzero-core/src/providers/helpers.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.