-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ext): add
ext::on_informational()
callback extension (#3818)
This new function allows attaching a callback to a request, such that when it is sent through a hyper client connection, and any 1xx informational responses are received, they are passed to the callback. This takes the unstable client informational feature (introduced in #2594, tracking issue in #2565), and promotes it to a stable API. Closes #2565
- Loading branch information
1 parent
de28b0e
commit 8ce1fcf
Showing
8 changed files
with
181 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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,86 @@ | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct OnInformational(Arc<dyn OnInformationalCallback + Send + Sync>); | ||
|
||
/// Add a callback for 1xx informational responses. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # let some_body = (); | ||
/// let mut req = hyper::Request::new(some_body); | ||
/// | ||
/// hyper::ext::on_informational(&mut req, |res| { | ||
/// println!("informational: {:?}", res.status()); | ||
/// }); | ||
/// | ||
/// // send request on a client connection... | ||
/// ``` | ||
pub fn on_informational<B, F>(req: &mut http::Request<B>, callback: F) | ||
where | ||
F: Fn(Response<'_>) + Send + Sync + 'static, | ||
{ | ||
on_informational_raw(req, OnInformationalClosure(callback)); | ||
} | ||
|
||
pub(crate) fn on_informational_raw<B, C>(req: &mut http::Request<B>, callback: C) | ||
where | ||
C: OnInformationalCallback + Send + Sync + 'static, | ||
{ | ||
req.extensions_mut() | ||
.insert(OnInformational(Arc::new(callback))); | ||
} | ||
|
||
// Sealed, not actually nameable bounds | ||
pub(crate) trait OnInformationalCallback { | ||
fn on_informational(&self, res: http::Response<()>); | ||
} | ||
|
||
impl OnInformational { | ||
pub(crate) fn call(&self, res: http::Response<()>) { | ||
self.0.on_informational(res); | ||
} | ||
} | ||
|
||
struct OnInformationalClosure<F>(F); | ||
|
||
impl<F> OnInformationalCallback for OnInformationalClosure<F> | ||
where | ||
F: Fn(Response<'_>) + Send + Sync + 'static, | ||
{ | ||
fn on_informational(&self, res: http::Response<()>) { | ||
let res = Response(&res); | ||
(self.0)(res); | ||
} | ||
} | ||
|
||
// A facade over http::Response. | ||
// | ||
// It purposefully hides being able to move the response out of the closure, | ||
// while also not being able to expect it to be a reference `&Response`. | ||
// (Otherwise, a closure can be written as `|res: &_|`, and then be broken if | ||
// we make the closure take ownership.) | ||
// | ||
// With the type not being nameable, we could change from being a facade to | ||
// being either a real reference, or moving the http::Response into the closure, | ||
// in a backwards-compatible change in the future. | ||
#[derive(Debug)] | ||
pub struct Response<'a>(&'a http::Response<()>); | ||
|
||
impl Response<'_> { | ||
#[inline] | ||
pub fn status(&self) -> http::StatusCode { | ||
self.0.status() | ||
} | ||
|
||
#[inline] | ||
pub fn version(&self) -> http::Version { | ||
self.0.version() | ||
} | ||
|
||
#[inline] | ||
pub fn headers(&self) -> &http::HeaderMap { | ||
self.0.headers() | ||
} | ||
} |
This file contains 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 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 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 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 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.
8ce1fcf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark 'end_to_end'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.http2_parallel_x10_req_10kb_100_chunks_adaptive_window
32200239
ns/iter (± 16633719.10
)7951094
ns/iter (± 16404385.64
)4.05
This comment was automatically generated by workflow using github-action-benchmark.