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 1 commit
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
10 changes: 3 additions & 7 deletions examples/pokemon-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod plugin;
use std::{net::SocketAddr, sync::Arc};

use aws_smithy_http_server::{
body,
extension::OperationExtensionExt,
instrumentation::InstrumentExt,
plugin::{alb_health_check::AlbHealthCheckLayer, PluginPipeline},
Expand All @@ -17,7 +16,7 @@ use aws_smithy_http_server::{
};
use clap::Parser;

use hyper::{Body, Response, StatusCode};
use hyper::StatusCode;
use plugin::PrintExt;

use pokemon_service::{
Expand Down Expand Up @@ -54,11 +53,8 @@ pub async fn main() {
// Adds `tracing` spans and events to the request lifecycle.
.instrument()
// Handle `/ping` health check requests.
.http_layer(AlbHealthCheckLayer::new("/ping", |_req| async {
Response::builder()
.status(StatusCode::OK)
.body(body::boxed(Body::empty()))
.expect("Couldn't construct response")
.http_layer(AlbHealthCheckLayer::new_fn("/ping", |_req| async {
StatusCode::OK
}));

let app = PokemonService::builder_with_plugins(plugins)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
//!
//! ```

use std::convert::Infallible;
use std::task::{Context, Poll};

use futures_util::Future;
use futures_util::{Future, FutureExt};
use http::StatusCode;
use hyper::{Body, Request, Response};
use pin_project_lite::pin_project;
use tower::{util::Oneshot, Layer, Service, ServiceExt};
use tower::{service_fn, util::Oneshot, Layer, Service, ServiceExt};

use crate::body::BoxBody;

Expand All @@ -43,7 +45,24 @@ pub struct AlbHealthCheckLayer<'a, HealthCheckHandler> {

impl<'a> AlbHealthCheckLayer<'a, ()> {
/// Handle health check requests at `health_check_uri` with the specified handler.
pub fn new<HandlerFuture: Future<Output = Response<BoxBody>>, H: Fn(Request<Body>) -> HandlerFuture>(
pub fn new_fn<HandlerFuture: Future<Output = StatusCode>, H: Fn(Request<Body>) -> HandlerFuture + Clone>(
hlbarber marked this conversation as resolved.
Show resolved Hide resolved
health_check_uri: &'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 handler.
pub fn new<H: Service<Request<Body>, Response = StatusCode>>(
health_check_uri: &'static str,
health_check_handler: H,
) -> AlbHealthCheckLayer<H> {
Expand Down Expand Up @@ -72,18 +91,17 @@ pub struct AlbHealthCheckService<'a, H, S> {
layer: AlbHealthCheckLayer<'a, H>,
}

impl<'a, H, HandlerFuture, S> Service<Request<Body>> for AlbHealthCheckService<'a, H, S>
impl<'a, H, S> Service<Request<Body>> for AlbHealthCheckService<'a, 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.

HandlerFuture: Future<Output = Response<BoxBody>>,
H: Fn(Request<Body>) -> HandlerFuture,
H: Service<Request<Body>, Response = StatusCode, Error = Infallible> + Clone,
{
type Response = S::Response;

type Error = S::Error;

type Future = AlbHealthCheckFuture<S, HandlerFuture>;
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.
Expand All @@ -92,7 +110,9 @@ where

fn call(&mut self, req: Request<Body>) -> Self::Future {
if req.uri() == self.layer.health_check_uri {
let handler_future = (self.layer.health_check_handler)(req);
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 {
Expand All @@ -105,18 +125,18 @@ where
}
}

type HealthCheckFutureInner<S, HandlerFuture> = Either<HandlerFuture, Oneshot<S, Request<Body>>>;
type HealthCheckFutureInner<H, S> = Either<Oneshot<H, Request<Body>>, Oneshot<S, Request<Body>>>;

pin_project! {
/// Future for [`AlbHealthCheckService`].
pub struct AlbHealthCheckFuture<S: Service<Request<Body>>, HandlerFuture: Future<Output = S::Response>> {
pub struct AlbHealthCheckFuture<H: Service<Request<Body>, Response = StatusCode>, S: Service<Request<Body>>> {
#[pin]
inner: HealthCheckFutureInner<S, HandlerFuture>
inner: HealthCheckFutureInner<H, S>
}
}

impl<S: Service<Request<Body>>, HandlerFuture: Future<Output = S::Response>> AlbHealthCheckFuture<S, HandlerFuture> {
fn handler_future(handler_future: HandlerFuture) -> Self {
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 },
}
Expand All @@ -129,16 +149,29 @@ impl<S: Service<Request<Body>>, HandlerFuture: Future<Output = S::Response>> Alb
}
}

impl<S: Service<Request<Body>>, HandlerFuture: Future<Output = S::Response>> Future
for AlbHealthCheckFuture<S, HandlerFuture>
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 } => value.poll(cx).map(Ok),
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),
}
}
Expand Down