-
Notifications
You must be signed in to change notification settings - Fork 101
/
lib.rs
311 lines (275 loc) · 9.61 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::{
env,
future::Future,
mem,
pin::Pin,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use http_body::Body as HttpBody;
use lambda_extension::Extension;
pub use lambda_http::Error;
use lambda_http::{Body, Request, RequestExt, Response};
use reqwest::{redirect, Client, Url};
use tokio::net::TcpStream;
use tokio::time::timeout;
use tokio_retry::{strategy::FixedInterval, Retry};
use tower::Service;
#[derive(Debug, Clone, Copy)]
pub enum Protocol {
Http,
Tcp,
}
impl Default for Protocol {
fn default() -> Self {
Protocol::Http
}
}
impl From<&str> for Protocol {
fn from(value: &str) -> Self {
match value.to_lowercase().as_str() {
"http" => Protocol::Http,
"tcp" => Protocol::Tcp,
_ => Protocol::Http,
}
}
}
#[derive(Default)]
pub struct AdapterOptions {
host: String,
port: String,
readiness_check_port: String,
readiness_check_path: String,
readiness_check_protocol: Protocol,
base_path: Option<String>,
async_init: bool,
}
impl AdapterOptions {
pub fn from_env() -> Self {
AdapterOptions {
host: env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string()),
port: env::var("PORT").unwrap_or_else(|_| "8080".to_string()),
readiness_check_port: env::var("READINESS_CHECK_PORT")
.unwrap_or_else(|_| env::var("PORT").unwrap_or_else(|_| "8080".to_string())),
readiness_check_path: env::var("READINESS_CHECK_PATH").unwrap_or_else(|_| "/".to_string()),
readiness_check_protocol: env::var("READINESS_CHECK_PROTOCOL")
.unwrap_or_else(|_| "HTTP".to_string())
.as_str()
.into(),
base_path: env::var("REMOVE_BASE_PATH").ok(),
async_init: env::var("ASYNC_INIT")
.unwrap_or_else(|_| "false".to_string())
.parse()
.unwrap_or(false),
}
}
}
pub struct Adapter {
client: Arc<Client>,
healthcheck_url: Url,
healthcheck_protocol: Protocol,
async_init: bool,
ready_at_init: Arc<AtomicBool>,
domain: Url,
base_path: Option<String>,
}
impl Adapter {
/// Create a new Adapter instance.
/// This function initializes a new HTTP client
/// to talk with the web server.
pub fn new(options: &AdapterOptions) -> Adapter {
let client = Client::builder()
.redirect(redirect::Policy::none())
.pool_idle_timeout(Duration::from_secs(4))
.build()
.unwrap();
let healthcheck_url = format!(
"http://{}:{}{}",
options.host, options.readiness_check_port, options.readiness_check_path
)
.parse()
.unwrap();
let domain = format!("http://{}:{}", options.host, options.port).parse().unwrap();
Adapter {
client: Arc::new(client),
healthcheck_url,
healthcheck_protocol: options.readiness_check_protocol,
domain,
base_path: options.base_path.clone(),
async_init: options.async_init,
ready_at_init: Arc::new(AtomicBool::new(false)),
}
}
/// Switch the default HTTP client with a different one.
pub fn with_client(self, client: Client) -> Self {
Adapter {
client: Arc::new(client),
..self
}
}
/// Register a Lambda Extension to ensure
/// that the adapter is loaded before any Lambda function
/// associated with it.
pub fn register_default_extension(&self) {
// register as an external extension
tokio::task::spawn(async move {
match Extension::new().with_events(&[]).run().await {
Ok(_) => {}
Err(err) => {
tracing::error!(err = err, "extension terminated unexpectedly");
panic!("extension thread execution");
}
}
});
}
/// Check if the web server has been initialized.
/// If `Adapter.async_init` is true, cancel this check before
/// Lambda's init 10s timeout, and let the server boot in the background.
pub async fn check_init_health(&mut self) {
let ready_at_init = if self.async_init {
timeout(Duration::from_secs_f32(9.8), self.check_readiness())
.await
.unwrap_or_default()
} else {
self.check_readiness().await
};
self.ready_at_init.store(ready_at_init, Ordering::SeqCst);
}
async fn check_readiness(&self) -> bool {
let url = self.healthcheck_url.clone();
let protocol = self.healthcheck_protocol;
is_web_ready(&url, &protocol).await
}
/// Run the adapter to take events from Lambda.
pub async fn run(self) -> Result<(), Error> {
lambda_http::run(self).await
}
}
/// Implement a `Tower.Service` that sends the requests
/// to the web server.
impl Service<Request> for Adapter {
type Response = Response<Body>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, _cx: &mut core::task::Context<'_>) -> core::task::Poll<Result<(), Self::Error>> {
core::task::Poll::Ready(Ok(()))
}
fn call(&mut self, event: Request) -> Self::Future {
let async_init = self.async_init;
let client = self.client.clone();
let ready_at_init = self.ready_at_init.clone();
let healthcheck_url = self.healthcheck_url.clone();
let healthcheck_protocol = self.healthcheck_protocol;
let domain = self.domain.clone();
let base_path = self.base_path.clone();
Box::pin(async move {
fetch_response(
async_init,
ready_at_init,
client,
base_path,
domain,
healthcheck_url,
healthcheck_protocol,
event,
)
.await
})
}
}
#[allow(clippy::too_many_arguments)]
async fn fetch_response(
async_init: bool,
ready_at_init: Arc<AtomicBool>,
client: Arc<Client>,
base_path: Option<String>,
domain: Url,
healthcheck_url: Url,
healthcheck_protocol: Protocol,
event: Request,
) -> Result<Response<Body>, Error> {
if async_init && !ready_at_init.load(Ordering::SeqCst) {
is_web_ready(&healthcheck_url, &healthcheck_protocol).await;
ready_at_init.store(true, Ordering::SeqCst);
}
let path = event.raw_http_path();
let mut path = path.as_str();
let (parts, body) = event.into_parts();
// strip away Base Path if environment variable REMOVE_BASE_PATH is set.
if let Some(base_path) = base_path.as_deref() {
path = path.trim_start_matches(base_path);
}
let req_headers = parts.headers;
let mut app_url = domain;
app_url.set_path(path);
app_url.set_query(parts.uri.query());
tracing::debug!(app_url = %app_url, req_headers = ?req_headers, "sending request to app server");
let app_response = client
.request(parts.method, app_url.to_string())
.headers(req_headers)
.body(body.to_vec())
.send()
.await?;
let app_headers = app_response.headers().clone();
let status = app_response.status();
let body = convert_body(app_response).await?;
tracing::debug!(status = %status, body_size = body.size_hint().lower(), app_headers = ?app_headers, "responding to lambda event");
let mut lambda_response = Response::builder();
if let Some(headers) = lambda_response.headers_mut() {
let _ = mem::replace(headers, app_headers);
}
let resp = lambda_response.status(status).body(body).map_err(Box::new)?;
Ok(resp)
}
async fn is_web_ready(url: &Url, protocol: &Protocol) -> bool {
Retry::spawn(FixedInterval::from_millis(10), || check_web_readiness(url, protocol))
.await
.is_ok()
}
async fn check_web_readiness(url: &Url, protocol: &Protocol) -> Result<(), i8> {
match protocol {
Protocol::Http => match reqwest::get(url.as_str()).await {
Ok(_) => Ok(()),
Err(_) => Err(-1),
},
Protocol::Tcp => match TcpStream::connect(format!("{}:{}", url.host().unwrap(), url.port().unwrap())).await {
Ok(_) => Ok(()),
Err(_) => Err(-1),
},
}
}
async fn convert_body(app_response: reqwest::Response) -> Result<Body, Error> {
if app_response.headers().get(http::header::CONTENT_ENCODING).is_some() {
let content = app_response.bytes().await?;
return if content.is_empty() {
Ok(Body::Empty)
} else {
Ok(Body::Binary(content.to_vec()))
};
}
match app_response.headers().get(http::header::CONTENT_TYPE) {
Some(value) => {
let content_type = value.to_str().unwrap_or_default();
if content_type.starts_with("text")
|| content_type.starts_with("application/json")
|| content_type.starts_with("application/javascript")
|| content_type.starts_with("application/xml")
{
Ok(Body::Text(app_response.text().await?))
} else {
let content = app_response.bytes().await?;
if content.is_empty() {
Ok(Body::Empty)
} else {
Ok(Body::Binary(content.to_vec()))
}
}
}
None => Ok(Body::Text(app_response.text().await?)),
}
}