Skip to content

Commit ae92c6b

Browse files
author
David Barsky
committed
Removed RuntimeClient trait.
1 parent 54fd3e7 commit ae92c6b

File tree

3 files changed

+34
-69
lines changed

3 files changed

+34
-69
lines changed

lambda-runtime-client/src/client.rs

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,6 @@ impl fmt::Display for LambdaHeaders {
4444
}
4545
}
4646

47-
/// Trait that defines the methods that should be implemented by a Lambda Runtime API client.
48-
/// Events are returned as a `Vec<u8>` and output for the `event_response()` method should
49-
/// also be passed as a `Vec<u8>`. Serialization and deserialization are up to the runtime
50-
/// library. When returning an error to the `event_error()` or `fail_init()` methods the objects
51-
/// should implement the `error::RuntimeApiError` triat.
52-
pub trait RuntimeClient {
53-
/// Should fetch the next event from the Runtime APIs.
54-
///
55-
/// # Return
56-
/// A tuple of the event raw bytes and the `EventContext` object extracted from the
57-
/// Lambda Runtime API headers - see the `LambdaHeaders` enum.
58-
fn next_event(&self) -> Result<(Vec<u8>, EventContext), ApiError>;
59-
/// Should return the handler response to the Runtime APIs.
60-
///
61-
/// # Arguments
62-
///
63-
/// * `request_id` The original AWS request id received from the `poll_events()` method.
64-
/// * `output` The object returned by the handler serialized to its raw bytes representation
65-
/// as a `Vec<u8>`.
66-
///
67-
/// # Return
68-
/// A `Result<(), RuntimeError>` signifying the API call's success or failure.
69-
fn event_response(&self, request_id: &str, output: Vec<u8>) -> Result<(), ApiError>;
70-
/// Should send an error response to the Runtime APIs.
71-
///
72-
/// # Arguments
73-
///
74-
/// * `request_id` The original AWS request id received from the `poll_events()` method.
75-
/// * `e` The `errors::HandlerError` returned by the handler function.
76-
///
77-
/// # Return
78-
/// A `Result<(), RuntimeError>` signifying the API call's success or failure. A unit struct
79-
/// signifies a lack of an error.
80-
fn event_error(&self, request_id: &str, e: &RuntimeApiError) -> Result<(), ApiError>;
81-
/// Should tell the Runtime APIs that the custom runtime has failed to initialize and the
82-
/// execution environment should be terminated.
83-
fn fail_init(&self, e: &RuntimeApiError);
84-
/// Returns the endpoint this client is communicating with. Primarily used for debugging.
85-
fn get_endpoint(&self) -> String;
86-
}
87-
8847
/// AWS Moble SDK client properties
8948
#[derive(Deserialize, Clone)]
9049
pub struct ClientApplication {
@@ -150,13 +109,13 @@ pub struct EventContext {
150109

151110
/// The client SDK for Lambda's Runtime APIs that implements the `RuntimeClient` trait.
152111
/// This object is used by the `RustRuntime` to communicate with the internal endpoint.
153-
pub struct HttpRuntimeClient {
112+
pub struct RuntimeClient {
154113
_runtime: Runtime,
155114
http_client: Client<HttpConnector, Body>,
156115
endpoint: String,
157116
}
158117

159-
impl HttpRuntimeClient {
118+
impl RuntimeClient {
160119
/// Creates a new instance of the Runtime APIclient SDK. The http client has timeouts disabled and
161120
/// always send the `Connection: keep-alive` header.
162121
///
@@ -177,20 +136,20 @@ impl HttpRuntimeClient {
177136

178137
let http_client = Client::builder().executor(runtime.executor()).build_http();
179138

180-
Ok(HttpRuntimeClient {
139+
Ok(RuntimeClient {
181140
_runtime: runtime,
182141
http_client,
183142
endpoint,
184143
})
185144
}
186145
}
187146

188-
impl RuntimeClient for HttpRuntimeClient {
147+
impl RuntimeClient {
189148
/// Makes the HTTP call to poll for new events to the Runtime APIs.
190149
///
191150
/// # Return
192151
/// A `Result` containing a tuple of the new event of type `E` and its `EventContext`.
193-
fn next_event(&self) -> Result<(Vec<u8>, EventContext), ApiError> {
152+
pub fn next_event(&self) -> Result<(Vec<u8>, EventContext), ApiError> {
194153
let uri = format!(
195154
"http://{}/{}/runtime/invocation/next",
196155
self.endpoint, RUNTIME_API_VERSION
@@ -252,7 +211,7 @@ impl RuntimeClient for HttpRuntimeClient {
252211
///
253212
/// # Returns
254213
/// A `Result` object containing a bool return value for the call or an `error::ApiError` instance.
255-
fn event_response(&self, request_id: &str, output: Vec<u8>) -> Result<(), ApiError> {
214+
pub fn event_response(&self, request_id: &str, output: Vec<u8>) -> Result<(), ApiError> {
256215
let uri: Uri = format!(
257216
"http://{}/{}/runtime/invocation/{}/response",
258217
self.endpoint, RUNTIME_API_VERSION, request_id
@@ -299,7 +258,7 @@ impl RuntimeClient for HttpRuntimeClient {
299258
///
300259
/// # Returns
301260
/// A `Result` object containing a bool return value for the call or an `error::ApiError` instance.
302-
fn event_error(&self, request_id: &str, e: &RuntimeApiError) -> Result<(), ApiError> {
261+
pub fn event_error(&self, request_id: &str, e: &RuntimeApiError) -> Result<(), ApiError> {
303262
let uri: Uri = format!(
304263
"http://{}/{}/runtime/invocation/{}/error",
305264
self.endpoint, RUNTIME_API_VERSION, request_id
@@ -346,7 +305,7 @@ impl RuntimeClient for HttpRuntimeClient {
346305
/// # Panics
347306
/// If it cannot send the init error. In this case we panic to force the runtime
348307
/// to restart.
349-
fn fail_init(&self, e: &RuntimeApiError) {
308+
pub fn fail_init(&self, e: &RuntimeApiError) {
350309
let uri: Uri = format!("http://{}/{}/runtime/init/error", self.endpoint, RUNTIME_API_VERSION)
351310
.parse()
352311
.expect("Could not generate Runtime API URI");
@@ -364,12 +323,12 @@ impl RuntimeClient for HttpRuntimeClient {
364323
}
365324

366325
/// Returns the endpoint configured for this HTTP Runtime client.
367-
fn get_endpoint(&self) -> String {
326+
pub fn get_endpoint(&self) -> String {
368327
self.endpoint.clone()
369328
}
370329
}
371330

372-
impl HttpRuntimeClient {
331+
impl RuntimeClient {
373332
/// Creates a Hyper `Request` object for the given `Uri` and `Body`. Sets the
374333
/// HTTP method to `POST` and the `Content-Type` header value to `application/json`.
375334
///

lambda-runtime/src/env.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ pub trait ConfigProvider {
3939
/// used by the `start()` method of this module.
4040
pub struct EnvConfigProvider;
4141

42+
impl EnvConfigProvider {
43+
pub fn new() -> Self {
44+
EnvConfigProvider { }
45+
}
46+
}
47+
4248
impl ConfigProvider for EnvConfigProvider {
4349
/// Loads the function settings from the Lambda environment variables:
4450
/// https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html

lambda-runtime/src/runtime.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{error::Error, result};
22

3-
use lambda_runtime_client;
43
use serde;
54
use serde_json;
65

6+
use lambda_runtime_client::RuntimeClient;
77
use context::Context;
88
use env::{ConfigProvider, EnvConfigProvider, FunctionSettings};
99
use error::{HandlerError, RuntimeError};
@@ -22,12 +22,12 @@ pub type Handler<E, O> = fn(E, Context) -> Result<O, HandlerError>;
2222
///
2323
/// # Panics
2424
/// The function panics if the Lambda environment variables are not set.
25-
pub fn start<E: 'static, O: 'static>(f: Handler<E, O>, runtime: Option<TokioRuntime>)
25+
pub fn start<E, O>(f: Handler<E, O>, runtime: Option<TokioRuntime>)
2626
where
2727
for<'invocation> E: serde::Deserialize<'invocation>,
2828
O: serde::Serialize,
2929
{
30-
start_with_config(f, &EnvConfigProvider {}, runtime)
30+
start_with_config(f, EnvConfigProvider::new(), runtime)
3131
}
3232

3333
#[macro_export]
@@ -53,13 +53,14 @@ macro_rules! lambda {
5353
/// The function panics if the `ConfigProvider` returns an error from the `get_runtime_api_endpoint()`
5454
/// or `get_function_settings()` methods. The panic forces AWS Lambda to terminate the environment
5555
/// and spin up a new one for the next invocation.
56-
pub(crate) fn start_with_config<E: 'static, O: 'static>(
56+
pub(crate) fn start_with_config<E, O, C>(
5757
f: Handler<E, O>,
58-
config: &'static ConfigProvider,
58+
config: C,
5959
runtime: Option<TokioRuntime>,
6060
) where
6161
for<'invocation> E: serde::Deserialize<'invocation>,
6262
O: serde::Serialize,
63+
C: ConfigProvider,
6364
{
6465
// if we cannot find the endpoint we panic, nothing else we can do.
6566
let endpoint: String;
@@ -81,10 +82,9 @@ pub(crate) fn start_with_config<E: 'static, O: 'static>(
8182
}
8283
}
8384

84-
match lambda_runtime_client::HttpRuntimeClient::new(endpoint, runtime) {
85+
match RuntimeClient::new(endpoint, runtime) {
8586
Ok(client) => {
86-
let trait_client: &lambda_runtime_client::RuntimeClient = &client;
87-
start_with_runtime_client(f, function_config, trait_client);
87+
start_with_runtime_client(f, function_config, client);
8888
}
8989
Err(e) => {
9090
panic!("Could not create runtime client SDK: {}", e);
@@ -103,10 +103,10 @@ pub(crate) fn start_with_config<E: 'static, O: 'static>(
103103
///
104104
/// # Panics
105105
/// The function panics if we cannot instantiate a new `RustRuntime` object.
106-
pub(crate) fn start_with_runtime_client<'env, E: 'static, O: 'static>(
106+
pub(crate) fn start_with_runtime_client<E, O>(
107107
f: Handler<E, O>,
108108
func_settings: FunctionSettings,
109-
client: &'env lambda_runtime_client::RuntimeClient,
109+
client: RuntimeClient,
110110
) where
111111
for<'invocation> E: serde::Deserialize<'invocation>,
112112
O: serde::Serialize,
@@ -125,15 +125,15 @@ pub(crate) fn start_with_runtime_client<'env, E: 'static, O: 'static>(
125125

126126
/// Internal representation of the runtime object that polls for events and communicates
127127
/// with the Runtime APIs
128-
pub(super) struct Runtime<'env, E: 'static, O: 'static> {
129-
runtime_client: &'env lambda_runtime_client::RuntimeClient,
128+
pub(super) struct Runtime<E, O> {
129+
runtime_client: RuntimeClient,
130130
handler: Handler<E, O>,
131131
max_retries: i8,
132132
settings: FunctionSettings,
133133
}
134134

135135
// generic methods implementation
136-
impl<'env, E, O> Runtime<'env, E, O> {
136+
impl<E, O> Runtime<E, O> {
137137
/// Creates a new instance of the `Runtime` object populated with the environment
138138
/// settings.
139139
///
@@ -151,8 +151,8 @@ impl<'env, E, O> Runtime<'env, E, O> {
151151
f: Handler<E, O>,
152152
config: FunctionSettings,
153153
retries: i8,
154-
client: &'env lambda_runtime_client::RuntimeClient,
155-
) -> result::Result<Runtime<'env, E, O>, RuntimeError> {
154+
client: RuntimeClient,
155+
) -> result::Result<Runtime<E, O>, RuntimeError> {
156156
debug!(
157157
"Creating new runtime with {} max retries for endpoint {}",
158158
retries,
@@ -169,7 +169,7 @@ impl<'env, E, O> Runtime<'env, E, O> {
169169

170170
// implementation of methods that require the Event and Output types
171171
// to be compatible with `serde`'s Deserialize/Serialize.
172-
impl<'env, E, O> Runtime<'env, E, O>
172+
impl<'env, E, O> Runtime<E, O>
173173
where
174174
for<'de> E: serde::Deserialize<'de>,
175175
O: serde::Serialize,
@@ -300,12 +300,12 @@ pub(crate) mod tests {
300300
use super::*;
301301
use context;
302302
use env;
303-
use lambda_runtime_client as cli;
303+
use lambda_runtime_client::RuntimeClient;
304304

305305
#[test]
306306
fn runtime_invokes_handler() {
307307
let config: &env::ConfigProvider = &env::tests::MockConfigProvider { error: false };
308-
let client: &lambda_runtime_client::RuntimeClient = &cli::HttpRuntimeClient::new(
308+
let client = RuntimeClient::new(
309309
config
310310
.get_runtime_api_endpoint()
311311
.expect("Could not get runtime endpoint"),

0 commit comments

Comments
 (0)