Skip to content
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 44 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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 Apr 4, 2023
b746d6c
Make types that need to be public public
jjant Apr 4, 2023
5153fef
Use `CheckHealthLayer` in example
jjant Apr 4, 2023
ceeb99b
Simplify `CheckHealthService`
jjant Apr 4, 2023
7fb5c48
Add `CheckHealthLayer::with_default_handler`
jjant Apr 4, 2023
bdbed1b
Defer `service.clone()` to when needed and use `oneshot()`
jjant Apr 12, 2023
d830a0f
Always return `Poll::Ready` in the middleware
jjant Apr 12, 2023
ffacb4c
Make health check uri configurable
jjant Apr 12, 2023
ef888e5
Implement `CheckHealthPlugin`
jjant Apr 12, 2023
764f389
Add `Debug` impls
jjant Apr 12, 2023
7fbd3da
Use extension trait method
jjant Apr 13, 2023
687c897
Add `PluginPipeline::http_layer` method
jjant Apr 13, 2023
3983a0d
Use `http_layer` method on pokemon service example
jjant Apr 13, 2023
b27db9b
Remove `CheckHealthPlugin` and `CheckHealthExt`
jjant Apr 13, 2023
8e2945a
Test that the pokemon service responds to health check requests
jjant Apr 13, 2023
e513685
Remove default ping handler
jjant Apr 13, 2023
8030949
Make check health handler infallible
jjant Apr 13, 2023
7e55d0c
Undo breaking change in `HttpLayer`
jjant Apr 13, 2023
8f6ec3d
Simplify `MappedHandlerFuture`
jjant Apr 13, 2023
e4d22cf
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 13, 2023
d496eb7
Fix docs
jjant Apr 13, 2023
6772f5a
Use `async` block instead of explicit `std::future::ready` call
jjant Apr 14, 2023
6735a32
Newtype future and add module docs
jjant Apr 17, 2023
4e442ca
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 17, 2023
ae48b9d
Fix clippy complaints
jjant Apr 17, 2023
9fd2088
Inline `MappedHandlerFuture` into `CheckHealthFuture`
jjant Apr 17, 2023
bc385f2
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 17, 2023
5bed399
Add module level example for `check_health`
jjant Apr 18, 2023
32007cd
Fix minor grammar mistake in comment
jjant Apr 20, 2023
887fbdc
Don't run example
jjant Apr 20, 2023
7fad221
Allow using non-static `&str`s for health check URIs
jjant Apr 20, 2023
3d15ad8
Use healthcheck-based naming instead of "ping"-based
jjant Apr 20, 2023
9bd96ce
Use "health check" instead of "check health" everywhere
jjant Apr 20, 2023
05af5e7
Prefix public items with `Alb`
jjant Apr 20, 2023
67493f2
Add changelog entry
jjant Apr 20, 2023
b822c53
Mention `PluginPipeline::http_layer` in changelog
jjant Apr 20, 2023
a2719d4
Document `AlbHealthCheckLayer::new`
jjant Apr 20, 2023
8c04213
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 20, 2023
f22e012
Make layer take in a `Service`
jjant Apr 20, 2023
2f4d3bd
Use `Cow<'a, str>` for uri
jjant Apr 21, 2023
a961ecb
Rename `new_fn` -> `from_handler`
jjant Apr 21, 2023
d56d5cd
Use `'static` for `Cow` lifetime
jjant Apr 21, 2023
3e98163
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 21, 2023
8a4eede
Merge branch 'main' into jjant/add-ping-layer
jjant Apr 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@
# meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client | server | all"}
# author = "rcoh"

[[smithy-rs]]
message = """
Implement layer for servers to handle [ALB health checks](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html).
Take a look at `aws_smithy_http_server::plugin::alb_health_check` to learn about it.
"""
references = ["smithy-rs#2540"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "server" }
author = "jjant"

[[smithy-rs]]
message = "Implement `PluginPipeline::http_layer` which allows you to apply a `tower::Layer` to all operations."
references = ["smithy-rs#2540"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "server" }
author = "jjant"

[[aws-sdk-rust]]
message = "Implement std::error::Error#source() properly for the service meta Error enum"
message = "Implement std::error::Error#source() properly for the service meta Error enum."
references = ["aws-sdk-rust#784"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "abusch"

[[smithy-rs]]
message = "Implement std::error::Error#source() properly for the service meta Error enum"
message = "Implement std::error::Error#source() properly for the service meta Error enum."
references = ["aws-sdk-rust#784"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client"}
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "client" }
author = "abusch"

[[aws-sdk-rust]]
Expand All @@ -32,7 +47,7 @@ author = "jdisanti"
[[smithy-rs]]
message = "The outputs for event stream operations now implement the `Sync` auto-trait."
references = ["smithy-rs#2496"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "all"}
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "all" }
author = "jdisanti"

[[aws-sdk-rust]]
Expand All @@ -44,7 +59,7 @@ author = "eduardomourar"
[[smithy-rs]]
message = "Clients now compile for the `wasm32-unknown-unknown` and `wasm32-wasi` targets when no default features are enabled. WebAssembly is not officially supported yet, but this is a great first step towards it!"
references = ["smithy-rs#2254"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "client"}
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "client" }
author = "eduardomourar"

[[smithy-rs]]
Expand All @@ -56,7 +71,7 @@ author = "jdisanti"
[[smithy-rs]]
message = "Streaming operations now emit the request ID at the `debug` log level like their non-streaming counterparts."
references = ["smithy-rs#2495"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client"}
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client" }
author = "jdisanti"

[[smithy-rs]]
Expand Down
3 changes: 3 additions & 0 deletions examples/pokemon-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ async-stream = "0.3"
rand = "0.8.5"
serial_test = "1.0.0"

# We use hyper client in tests
hyper = {version = "0.14.25", features = ["server", "client"] }

# This dependency is only required for testing the `pokemon-service-tls` program.
hyper-rustls = { version = "0.23.2", features = ["http2"] }

Expand Down
15 changes: 12 additions & 3 deletions examples/pokemon-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ mod plugin;
use std::{net::SocketAddr, sync::Arc};

use aws_smithy_http_server::{
extension::OperationExtensionExt, instrumentation::InstrumentExt, plugin::PluginPipeline,
request::request_id::ServerRequestIdProviderLayer, AddExtensionLayer,
extension::OperationExtensionExt,
instrumentation::InstrumentExt,
plugin::{alb_health_check::AlbHealthCheckLayer, PluginPipeline},
request::request_id::ServerRequestIdProviderLayer,
AddExtensionLayer,
};
use clap::Parser;

use hyper::StatusCode;
use plugin::PrintExt;

use pokemon_service::{
Expand Down Expand Up @@ -47,7 +51,12 @@ pub async fn main() {
// `Response::extensions`, or infer routing failure when it's missing.
.insert_operation_extension()
// Adds `tracing` spans and events to the request lifecycle.
.instrument();
.instrument()
// Handle `/ping` health check requests.
.http_layer(AlbHealthCheckLayer::from_handler("/ping", |_req| async {
StatusCode::OK
}));

let app = PokemonService::builder_with_plugins(plugins)
// Build a registry containing implementations to all the operations in the service. These
// are async functions or async closures that take as input the operation's input and
Expand Down
4 changes: 4 additions & 0 deletions examples/pokemon-service/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub async fn run_server() -> ChildDrop {
ChildDrop(child)
}

pub fn base_url() -> String {
format!("http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}")
}

pub fn client() -> Client<DynConnector, DynMiddleware<DynConnector>> {
let authority = Authority::from_str(&format!("{DEFAULT_ADDRESS}:{DEFAULT_PORT}"))
.expect("could not parse authority");
Expand Down
7 changes: 7 additions & 0 deletions examples/pokemon-service/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@ async fn simple_integration_test() {

let service_statistics_out = client.get_server_statistics().send().await.unwrap();
assert_eq!(2, service_statistics_out.calls_count.unwrap());

let hyper_client = hyper::Client::new();
let health_check_url = format!("{}/ping", common::base_url());
let health_check_url = hyper::Uri::try_from(health_check_url).unwrap();
let result = hyper_client.get(health_check_url).await.unwrap();

assert_eq!(result.status(), 200);
}
176 changes: 176 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/alb_health_check.rs
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>(
Copy link
Contributor

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.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
S::Future: std::marker::Send + 'static,
S::Future: Send + 'static,

Send is in the prelude.

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),
}
}
}
1 change: 1 addition & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
//! ```
//!

pub mod alb_health_check;
mod closure;
mod either;
mod filter;
Expand Down
7 changes: 7 additions & 0 deletions rust-runtime/aws-smithy-http-server/src/plugin/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use crate::operation::Operation;
use crate::plugin::{IdentityPlugin, Plugin, PluginStack};

use super::HttpLayer;

/// A wrapper struct for composing [`Plugin`]s.
/// It is used as input for the `builder_with_plugins` method on the generate service struct
/// (e.g. `PokemonService::builder_with_plugins`).
Expand Down Expand Up @@ -168,6 +170,11 @@ impl<P> PluginPipeline<P> {
pub fn push<NewPlugin>(self, new_plugin: NewPlugin) -> PluginPipeline<PluginStack<NewPlugin, P>> {
PluginPipeline(PluginStack::new(new_plugin, self.0))
}

/// Applies a single [`tower::Layer`] to all operations _before_ they are deserialized.
pub fn http_layer<L>(self, layer: L) -> PluginPipeline<PluginStack<HttpLayer<L>, P>> {
PluginPipeline(PluginStack::new(HttpLayer(layer), self.0))
}
}

impl<P, Op, S, L, InnerPlugin> Plugin<P, Op, S, L> for PluginPipeline<InnerPlugin>
Expand Down