-
Notifications
You must be signed in to change notification settings - Fork 330
Tidy up tower::load #445
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
Merged
Merged
Tidy up tower::load #445
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //! Application-specific request completion semantics. | ||
|
|
||
| use futures_core::ready; | ||
| use pin_project::pin_project; | ||
| use std::{ | ||
| future::Future, | ||
| pin::Pin, | ||
| task::{Context, Poll}, | ||
| }; | ||
|
|
||
| /// Attaches `H`-typed completion tracker to `V` typed values. | ||
| /// | ||
| /// Handles (of type `H`) are intended to be RAII guards that primarily implement `Drop` and update | ||
| /// load metric state as they are dropped. This trait allows implementors to "forward" the handle | ||
| /// to later parts of the request-handling pipeline, so that the handle is only dropped when the | ||
| /// request has truly completed. | ||
| /// | ||
| /// This utility allows load metrics to have a protocol-agnostic means to track streams past their | ||
| /// initial response future. For example, if `V` represents an HTTP response type, an | ||
| /// implementation could add `H`-typed handles to each response's extensions to detect when all the | ||
| /// response's extensions have been dropped. | ||
| /// | ||
| /// A base `impl<H, V> TrackCompletion<H, V> for CompleteOnResponse` is provided to drop the handle | ||
| /// once the response future is resolved. This is appropriate when a response is discrete and | ||
| /// cannot comprise multiple messages. | ||
| /// | ||
| /// In many cases, the `Output` type is simply `V`. However, `TrackCompletion` may alter the type | ||
| /// in order to instrument it appropriately. For example, an HTTP `TrackCompletion` may modify the | ||
| /// body type: so a `TrackCompletion` that takes values of type `http::Response<A>` may output | ||
| /// values of type `http::Response<B>`. | ||
| pub trait TrackCompletion<H, V>: Clone { | ||
| /// The instrumented value type. | ||
| type Output; | ||
|
|
||
| /// Attaches a `H`-typed handle to a `V`-typed value. | ||
| fn track_completion(&self, handle: H, value: V) -> Self::Output; | ||
| } | ||
|
|
||
| /// A `TrackCompletion` implementation that considers the request completed when the response | ||
| /// future is resolved. | ||
| #[derive(Clone, Copy, Debug, Default)] | ||
| #[non_exhaustive] | ||
| pub struct CompleteOnResponse; | ||
|
|
||
| /// Attaches a `C`-typed completion tracker to the result of an `F`-typed `Future`. | ||
| #[pin_project] | ||
| #[derive(Debug)] | ||
| pub struct TrackCompletionFuture<F, C, H> { | ||
| #[pin] | ||
| future: F, | ||
| handle: Option<H>, | ||
| completion: C, | ||
| } | ||
|
|
||
| // ===== impl InstrumentFuture ===== | ||
|
|
||
| impl<F, C, H> TrackCompletionFuture<F, C, H> { | ||
| /// Wraps a future, propagating the tracker into its value if successful. | ||
| pub fn new(completion: C, handle: H, future: F) -> Self { | ||
| TrackCompletionFuture { | ||
| future, | ||
| completion, | ||
| handle: Some(handle), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<F, C, H, T, E> Future for TrackCompletionFuture<F, C, H> | ||
| where | ||
| F: Future<Output = Result<T, E>>, | ||
| C: TrackCompletion<H, T>, | ||
| { | ||
| type Output = Result<C::Output, E>; | ||
|
|
||
| fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
| let this = self.project(); | ||
| let rsp = ready!(this.future.poll(cx))?; | ||
| let h = this.handle.take().expect("handle"); | ||
| Poll::Ready(Ok(this.completion.track_completion(h, rsp))) | ||
| } | ||
| } | ||
|
|
||
| // ===== CompleteOnResponse ===== | ||
|
|
||
| impl<H, V> TrackCompletion<H, V> for CompleteOnResponse { | ||
| type Output = V; | ||
|
|
||
| fn track_completion(&self, handle: H, value: V) -> V { | ||
| drop(handle); | ||
| value | ||
| } | ||
| } | ||
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.