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

fix readiness check function #72

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Changes from all commits
Commits
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
revert readiness check function
bnusunny committed Sep 8, 2022
commit 33acf80cae9b65b805fbfd19bd63215c092b7a6c
19 changes: 6 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
use lambda_extension::Extension;
use lambda_http::{Body, Request, RequestExt, Response};
use reqwest::{redirect, Client, Url};
use std::{env, future::Future, mem, pin::Pin, time::Duration};
use std::{env, future, future::Future, mem, pin::Pin, time::Duration};
use tokio::{runtime::Handle, time::timeout};
use tokio_retry::{strategy::FixedInterval, Retry};
use tower::Service;
@@ -95,20 +95,13 @@ impl Adapter {

async fn check_readiness(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

this function doesn't need to be async if the request blocks the thread.

Retry::spawn(FixedInterval::from_millis(10), || {
let fut = self.check_health_url();
async move { fut.await }
match reqwest::blocking::get(&self.healthcheck_url) {
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason to use GET request and not HEAD? if the url you're checking is a homepage, the response could be really big. A HEAD request should work as well, without fetching the body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GET request allows the server to do additional actions, such as warm up cache and preload dependencies. HEAD request does not guarantee that. Servers might just return the headers and not do any extra actions.

I see the benefit of using HEAD request. We can make it an option for developers to choose. That should be another PR.

Ok(response) if { response.status().is_success() } => future::ready(Ok(())),
_ => future::ready(Err::<(), i32>(-1)),
}
})
.await
.unwrap()
}

async fn check_health_url(&self) -> Result<bool, std::convert::Infallible> {
self.client
.head(&self.healthcheck_url)
.send()
.await
.map(|r| r.status().is_success())
.or(Ok(false))
.is_ok()
}
}