Skip to content

Commit c36f127

Browse files
authored
rewrite extension client and upgrade tokio (awslabs#120)
1 parent f5ace23 commit c36f127

File tree

6 files changed

+34
-63
lines changed

6 files changed

+34
-63
lines changed

.rustfmt.toml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
max_width=120
2-
unstable_features = true
3-
imports_granularity = "Crate"
42
reorder_imports = true
53
edition = "2021"

Cargo.lock

+5-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ exclude = ["examples"]
1919
[dependencies]
2020
http = "0.2"
2121
hyper = { version = "0.14", features = ["client"] }
22-
lambda-extension = "0.7.0"
2322
lambda_http = "0.7.2"
24-
tokio = { version = "1.20.0", features = [
23+
tokio = { version = "1.24", features = [
2524
"macros",
2625
"io-util",
2726
"sync",

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ build-LambdaAdapterLayerArm64:
5252
DOCKER_BUILDKIT=1 docker build --build-arg TARGET_PLATFORM=linux/arm64 --build-arg ARCH=aarch64 -o $(ARTIFACTS_DIR)/extensions .
5353

5454
fmt:
55-
cargo +nightly fmt --all
55+
cargo fmt --all

src/lib.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use std::{
1717
use encoding_rs::{Encoding, UTF_8};
1818
use http::{
1919
header::{HeaderName, HeaderValue},
20-
Uri,
20+
Method, StatusCode, Uri,
2121
};
2222
use hyper::{
2323
body::HttpBody,
2424
client::{Client, HttpConnector},
25+
Body,
2526
};
26-
use lambda_extension::Extension;
2727
use lambda_http::aws_lambda_events::serde_json;
2828
pub use lambda_http::Error;
2929
use lambda_http::{Body as LambdaBody, Request, RequestExt, Response};
@@ -137,13 +137,31 @@ impl Adapter {
137137
pub fn register_default_extension(&self) {
138138
// register as an external extension
139139
tokio::task::spawn(async move {
140-
match Extension::new().with_events(&[]).run().await {
141-
Ok(_) => {}
142-
Err(err) => {
143-
tracing::error!(err = err, "extension terminated unexpectedly");
144-
panic!("extension thread execution");
145-
}
140+
let aws_lambda_runtime_api: String =
141+
env::var("AWS_LAMBDA_RUNTIME_API").unwrap_or_else(|_| "127.0.0.1:9001".to_string());
142+
let client = hyper::Client::new();
143+
let register_req = hyper::Request::builder()
144+
.method(Method::POST)
145+
.uri(format!("http://{aws_lambda_runtime_api}/2020-01-01/extension/register"))
146+
.header("Lambda-Extension-Name", "lambda-adapter")
147+
.body(Body::from("{ \"events\": [] }"))
148+
.unwrap();
149+
let register_res = client.request(register_req).await.unwrap();
150+
if register_res.status() != StatusCode::OK {
151+
panic!("extension registration failure");
146152
}
153+
let next_req = hyper::Request::builder()
154+
.method(Method::GET)
155+
.uri(format!(
156+
"http://{aws_lambda_runtime_api}/2020-01-01/extension/event/next"
157+
))
158+
.header(
159+
"Lambda-Extension-Identifier",
160+
register_res.headers().get("Lambda-Extension-Identifier").unwrap(),
161+
)
162+
.body(Body::empty())
163+
.unwrap();
164+
client.request(next_req).await.unwrap();
147165
});
148166
}
149167

tests/integ_tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use httpmock::{
88
Method::{DELETE, GET, POST, PUT},
99
MockServer,
1010
};
11-
use lambda_extension::Service;
12-
1311
use lambda_http::Body;
1412
use lambda_web_adapter::{Adapter, AdapterOptions, Protocol};
13+
use tower::Service;
1514

1615
#[test]
1716
fn test_adapter_options_from_env() {

0 commit comments

Comments
 (0)