-
Notifications
You must be signed in to change notification settings - Fork 202
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
Add AlbHealthCheckLayer
#2540
Merged
Merged
Add AlbHealthCheckLayer
#2540
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
8374c38
Add Amazon IP header
jjant b746d6c
Make types that need to be public public
jjant 5153fef
Use `CheckHealthLayer` in example
jjant ceeb99b
Simplify `CheckHealthService`
jjant 7fb5c48
Add `CheckHealthLayer::with_default_handler`
jjant bdbed1b
Defer `service.clone()` to when needed and use `oneshot()`
jjant d830a0f
Always return `Poll::Ready` in the middleware
jjant ffacb4c
Make health check uri configurable
jjant ef888e5
Implement `CheckHealthPlugin`
jjant 764f389
Add `Debug` impls
jjant 7fbd3da
Use extension trait method
jjant 687c897
Add `PluginPipeline::http_layer` method
jjant 3983a0d
Use `http_layer` method on pokemon service example
jjant b27db9b
Remove `CheckHealthPlugin` and `CheckHealthExt`
jjant 8e2945a
Test that the pokemon service responds to health check requests
jjant e513685
Remove default ping handler
jjant 8030949
Make check health handler infallible
jjant 7e55d0c
Undo breaking change in `HttpLayer`
jjant 8f6ec3d
Simplify `MappedHandlerFuture`
jjant e4d22cf
Merge branch 'main' into jjant/add-ping-layer
jjant d496eb7
Fix docs
jjant 6772f5a
Use `async` block instead of explicit `std::future::ready` call
jjant 6735a32
Newtype future and add module docs
jjant 4e442ca
Merge branch 'main' into jjant/add-ping-layer
jjant ae48b9d
Fix clippy complaints
jjant 9fd2088
Inline `MappedHandlerFuture` into `CheckHealthFuture`
jjant bc385f2
Merge branch 'main' into jjant/add-ping-layer
jjant 5bed399
Add module level example for `check_health`
jjant 32007cd
Fix minor grammar mistake in comment
jjant 887fbdc
Don't run example
jjant 7fad221
Allow using non-static `&str`s for health check URIs
jjant 3d15ad8
Use healthcheck-based naming instead of "ping"-based
jjant 9bd96ce
Use "health check" instead of "check health" everywhere
jjant 05af5e7
Prefix public items with `Alb`
jjant 67493f2
Add changelog entry
jjant b822c53
Mention `PluginPipeline::http_layer` in changelog
jjant a2719d4
Document `AlbHealthCheckLayer::new`
jjant 8c04213
Merge branch 'main' into jjant/add-ping-layer
jjant f22e012
Make layer take in a `Service`
jjant 2f4d3bd
Use `Cow<'a, str>` for uri
jjant a961ecb
Rename `new_fn` -> `from_handler`
jjant d56d5cd
Use `'static` for `Cow` lifetime
jjant 3e98163
Merge branch 'main' into jjant/add-ping-layer
jjant 8a4eede
Merge branch 'main' into jjant/add-ping-layer
jjant 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 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
176 changes: 176 additions & 0 deletions
176
rust-runtime/aws-smithy-http-server/src/plugin/alb_health_check.rs
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,176 @@ | ||||||
/* | ||||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||
* SPDX-License-Identifier: Apache-2.0 | ||||||
*/ | ||||||
|
||||||
//! Middleware for handling [ALB health | ||||||
//! checks](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html). | ||||||
//! | ||||||
//! # Example | ||||||
//! | ||||||
//! ```no_run | ||||||
//! # use aws_smithy_http_server::{body, plugin::{PluginPipeline, alb_health_check::AlbHealthCheckLayer}}; | ||||||
//! # use hyper::{Body, Response, StatusCode}; | ||||||
//! let plugins = PluginPipeline::new() | ||||||
//! // Handle all `/ping` health check requests by returning a `200 OK`. | ||||||
//! .http_layer(AlbHealthCheckLayer::from_handler("/ping", |_req| async { | ||||||
//! StatusCode::OK | ||||||
//! })); | ||||||
//! | ||||||
//! ``` | ||||||
|
||||||
use std::borrow::Cow; | ||||||
use std::convert::Infallible; | ||||||
use std::task::{Context, Poll}; | ||||||
|
||||||
use futures_util::{Future, FutureExt}; | ||||||
use http::StatusCode; | ||||||
use hyper::{Body, Request, Response}; | ||||||
use pin_project_lite::pin_project; | ||||||
use tower::{service_fn, util::Oneshot, Layer, Service, ServiceExt}; | ||||||
|
||||||
use crate::body::BoxBody; | ||||||
|
||||||
use super::either::EitherProj; | ||||||
use super::Either; | ||||||
|
||||||
/// A [`tower::Layer`] used to apply [`AlbHealthCheckService`]. | ||||||
#[derive(Clone, Debug)] | ||||||
pub struct AlbHealthCheckLayer<HealthCheckHandler> { | ||||||
health_check_uri: Cow<'static, str>, | ||||||
health_check_handler: HealthCheckHandler, | ||||||
} | ||||||
|
||||||
impl AlbHealthCheckLayer<()> { | ||||||
/// Handle health check requests at `health_check_uri` with the specified handler. | ||||||
pub fn from_handler<HandlerFuture: Future<Output = StatusCode>, H: Fn(Request<Body>) -> HandlerFuture + Clone>( | ||||||
health_check_uri: impl Into<Cow<'static, str>>, | ||||||
health_check_handler: H, | ||||||
) -> AlbHealthCheckLayer< | ||||||
impl Service< | ||||||
Request<Body>, | ||||||
Response = StatusCode, | ||||||
Error = Infallible, | ||||||
Future = impl Future<Output = Result<StatusCode, Infallible>>, | ||||||
> + Clone, | ||||||
> { | ||||||
let service = service_fn(move |req| health_check_handler(req).map(Ok)); | ||||||
|
||||||
AlbHealthCheckLayer::new(health_check_uri, service) | ||||||
} | ||||||
|
||||||
/// Handle health check requests at `health_check_uri` with the specified service. | ||||||
pub fn new<H: Service<Request<Body>, Response = StatusCode>>( | ||||||
health_check_uri: impl Into<Cow<'static, str>>, | ||||||
health_check_handler: H, | ||||||
) -> AlbHealthCheckLayer<H> { | ||||||
AlbHealthCheckLayer { | ||||||
health_check_uri: health_check_uri.into(), | ||||||
health_check_handler, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl<S, H: Clone> Layer<S> for AlbHealthCheckLayer<H> { | ||||||
type Service = AlbHealthCheckService<H, S>; | ||||||
|
||||||
fn layer(&self, inner: S) -> Self::Service { | ||||||
AlbHealthCheckService { | ||||||
inner, | ||||||
layer: self.clone(), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
/// A middleware [`Service`] responsible for handling health check requests. | ||||||
#[derive(Clone, Debug)] | ||||||
pub struct AlbHealthCheckService<H, S> { | ||||||
inner: S, | ||||||
layer: AlbHealthCheckLayer<H>, | ||||||
} | ||||||
|
||||||
impl<H, S> Service<Request<Body>> for AlbHealthCheckService<H, S> | ||||||
where | ||||||
S: Service<Request<Body>, Response = Response<BoxBody>> + Clone, | ||||||
S::Future: std::marker::Send + 'static, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
H: Service<Request<Body>, Response = StatusCode, Error = Infallible> + Clone, | ||||||
{ | ||||||
type Response = S::Response; | ||||||
|
||||||
type Error = S::Error; | ||||||
|
||||||
type Future = AlbHealthCheckFuture<H, S>; | ||||||
|
||||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||||||
// The check that the service is ready is done by `Oneshot` below. | ||||||
Poll::Ready(Ok(())) | ||||||
} | ||||||
|
||||||
fn call(&mut self, req: Request<Body>) -> Self::Future { | ||||||
if req.uri() == self.layer.health_check_uri.as_ref() { | ||||||
let clone = self.layer.health_check_handler.clone(); | ||||||
let service = std::mem::replace(&mut self.layer.health_check_handler, clone); | ||||||
let handler_future = service.oneshot(req); | ||||||
|
||||||
AlbHealthCheckFuture::handler_future(handler_future) | ||||||
} else { | ||||||
let clone = self.inner.clone(); | ||||||
let service = std::mem::replace(&mut self.inner, clone); | ||||||
let service_future = service.oneshot(req); | ||||||
|
||||||
AlbHealthCheckFuture::service_future(service_future) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
type HealthCheckFutureInner<H, S> = Either<Oneshot<H, Request<Body>>, Oneshot<S, Request<Body>>>; | ||||||
|
||||||
pin_project! { | ||||||
/// Future for [`AlbHealthCheckService`]. | ||||||
pub struct AlbHealthCheckFuture<H: Service<Request<Body>, Response = StatusCode>, S: Service<Request<Body>>> { | ||||||
#[pin] | ||||||
inner: HealthCheckFutureInner<H, S> | ||||||
} | ||||||
} | ||||||
|
||||||
impl<H: Service<Request<Body>, Response = StatusCode>, S: Service<Request<Body>>> AlbHealthCheckFuture<H, S> { | ||||||
fn handler_future(handler_future: Oneshot<H, Request<Body>>) -> Self { | ||||||
Self { | ||||||
inner: Either::Left { value: handler_future }, | ||||||
} | ||||||
} | ||||||
|
||||||
fn service_future(service_future: Oneshot<S, Request<Body>>) -> Self { | ||||||
Self { | ||||||
inner: Either::Right { value: service_future }, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
impl< | ||||||
H: Service<Request<Body>, Response = StatusCode, Error = Infallible>, | ||||||
S: Service<Request<Body>, Response = Response<BoxBody>>, | ||||||
> Future for AlbHealthCheckFuture<H, S> | ||||||
{ | ||||||
type Output = Result<S::Response, S::Error>; | ||||||
|
||||||
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||||||
let either_proj = self.project().inner.project(); | ||||||
|
||||||
match either_proj { | ||||||
EitherProj::Left { value } => { | ||||||
let polled: Poll<Self::Output> = value.poll(cx).map(|res| { | ||||||
res.map(|status_code| { | ||||||
Response::builder() | ||||||
.status(status_code) | ||||||
.body(crate::body::empty()) | ||||||
.unwrap() | ||||||
}) | ||||||
.map_err(|never| match never {}) | ||||||
}); | ||||||
polled | ||||||
} | ||||||
EitherProj::Right { value } => value.poll(cx), | ||||||
} | ||||||
} | ||||||
} |
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 |
---|---|---|
|
@@ -118,6 +118,7 @@ | |
//! ``` | ||
//! | ||
|
||
pub mod alb_health_check; | ||
mod closure; | ||
mod either; | ||
mod filter; | ||
|
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
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.
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.
nit: these bounds would read nicer in a where clause.