Skip to content

Commit 9d56630

Browse files
committed
Merge branch 'main' into sveltekit-ssr-zip-example
2 parents a879e6a + 1e8c2cc commit 9d56630

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/lib.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
mod readiness;
5+
46
use http::{
57
header::{HeaderName, HeaderValue},
68
Method, StatusCode,
@@ -13,6 +15,7 @@ use lambda_http::request::RequestContext;
1315
use lambda_http::Body;
1416
pub use lambda_http::Error;
1517
use lambda_http::{Request, RequestExt, Response};
18+
use readiness::Checkpoint;
1619
use std::fmt::Debug;
1720
use std::{
1821
env,
@@ -24,8 +27,7 @@ use std::{
2427
},
2528
time::Duration,
2629
};
27-
use tokio::net::TcpStream;
28-
use tokio::time::timeout;
30+
use tokio::{net::TcpStream, time::timeout};
2931
use tokio_retry::{strategy::FixedInterval, Retry};
3032
use tower::{Service, ServiceBuilder};
3133
use tower_http::compression::CompressionLayer;
@@ -266,7 +268,12 @@ impl Adapter<HttpConnector, Body> {
266268
}
267269

268270
async fn is_web_ready(&self, url: &Url, protocol: &Protocol) -> bool {
271+
let mut checkpoint = Checkpoint::new();
269272
Retry::spawn(FixedInterval::from_millis(10), || {
273+
if checkpoint.lapsed() {
274+
tracing::info!(url = %url.to_string(), "app is not ready after {}ms", checkpoint.next_ms());
275+
checkpoint.increment();
276+
}
270277
self.check_web_readiness(url, protocol)
271278
})
272279
.await
@@ -282,10 +289,11 @@ impl Adapter<HttpConnector, Body> {
282289
&& response.status().as_u16() >= 100
283290
} =>
284291
{
292+
tracing::debug!("app is ready");
285293
Ok(())
286294
}
287295
_ => {
288-
tracing::debug!("app is not ready");
296+
tracing::trace!("app is not ready");
289297
Err(-1)
290298
}
291299
},

src/readiness.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::time::Instant;
2+
3+
pub(crate) struct Checkpoint {
4+
start: Instant,
5+
interval_ms: u128,
6+
next_ms: u128,
7+
}
8+
9+
impl Checkpoint {
10+
pub fn new() -> Checkpoint {
11+
// The default function timeout is 3 seconds. This will alert the users. See #520
12+
let interval_ms = 2000;
13+
14+
let start = Instant::now();
15+
Checkpoint {
16+
start,
17+
interval_ms,
18+
next_ms: start.elapsed().as_millis() + interval_ms,
19+
}
20+
}
21+
22+
pub const fn next_ms(&self) -> u128 {
23+
self.next_ms
24+
}
25+
26+
pub const fn increment(&mut self) {
27+
self.next_ms += self.interval_ms;
28+
}
29+
30+
pub fn lapsed(&self) -> bool {
31+
self.start.elapsed().as_millis() >= self.next_ms
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
use super::*;
38+
39+
#[test]
40+
fn test_checkpoint_new() {
41+
let checkpoint = Checkpoint::new();
42+
assert_eq!(checkpoint.next_ms(), 2000);
43+
assert!(!checkpoint.lapsed());
44+
}
45+
46+
#[test]
47+
fn test_checkpoint_increment() {
48+
let mut checkpoint = Checkpoint::new();
49+
checkpoint.increment();
50+
assert_eq!(checkpoint.next_ms(), 4000);
51+
assert!(!checkpoint.lapsed());
52+
}
53+
54+
#[test]
55+
fn test_checkpoint_lapsed() {
56+
let checkpoint = Checkpoint {
57+
start: Instant::now(),
58+
interval_ms: 0,
59+
next_ms: 0,
60+
};
61+
assert!(checkpoint.lapsed());
62+
}
63+
}

0 commit comments

Comments
 (0)