@@ -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 ) ]
9049pub struct ClientApplication {
@@ -148,25 +107,16 @@ pub struct EventContext {
148107 pub identity : Option < CognitoIdentity > ,
149108}
150109
151- /// The client SDK for Lambda's Runtime APIs that implements the `RuntimeClient` trait.
152- /// This object is used by the `RustRuntime` to communicate with the internal endpoint.
153- pub struct HttpRuntimeClient {
110+ /// Used by the Runtime to communicate with the internal endpoint.
111+ pub struct RuntimeClient {
154112 _runtime : Runtime ,
155113 http_client : Client < HttpConnector , Body > ,
156114 endpoint : String ,
157115}
158116
159- impl HttpRuntimeClient {
117+ impl RuntimeClient {
160118 /// Creates a new instance of the Runtime APIclient SDK. The http client has timeouts disabled and
161- /// always send the `Connection: keep-alive` header.
162- ///
163- /// # Arguments
164- ///
165- /// * `http_endpoint` The http endpoint for the Runtime APIs. This value comes from the AWS_LAMBDA_RUNTIME_API
166- /// environment variable.
167- ///
168- /// # Return
169- /// An instance of the Runtime APIs client SDK.
119+ /// will always send a `Connection: keep-alive` header.
170120 pub fn new ( endpoint : String , runtime : Option < Runtime > ) -> Result < Self , ApiError > {
171121 debug ! ( "Starting new HttpRuntimeClient for {}" , endpoint) ;
172122 // start a tokio core main event loop for hyper
@@ -177,20 +127,17 @@ impl HttpRuntimeClient {
177127
178128 let http_client = Client :: builder ( ) . executor ( runtime. executor ( ) ) . build_http ( ) ;
179129
180- Ok ( HttpRuntimeClient {
130+ Ok ( RuntimeClient {
181131 _runtime : runtime,
182132 http_client,
183133 endpoint,
184134 } )
185135 }
186136}
187137
188- impl RuntimeClient for HttpRuntimeClient {
189- /// Makes the HTTP call to poll for new events to the Runtime APIs.
190- ///
191- /// # Return
192- /// 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 > {
138+ impl RuntimeClient {
139+ /// Polls for new events to the Runtime APIs.
140+ pub fn next_event ( & self ) -> Result < ( Vec < u8 > , EventContext ) , ApiError > {
194141 let uri = format ! (
195142 "http://{}/{}/runtime/invocation/next" ,
196143 self . endpoint, RUNTIME_API_VERSION
@@ -252,7 +199,7 @@ impl RuntimeClient for HttpRuntimeClient {
252199 ///
253200 /// # Returns
254201 /// 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 > {
202+ pub fn event_response ( & self , request_id : & str , output : Vec < u8 > ) -> Result < ( ) , ApiError > {
256203 let uri: Uri = format ! (
257204 "http://{}/{}/runtime/invocation/{}/response" ,
258205 self . endpoint, RUNTIME_API_VERSION , request_id
@@ -299,7 +246,7 @@ impl RuntimeClient for HttpRuntimeClient {
299246 ///
300247 /// # Returns
301248 /// 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 > {
249+ pub fn event_error ( & self , request_id : & str , e : & RuntimeApiError ) -> Result < ( ) , ApiError > {
303250 let uri: Uri = format ! (
304251 "http://{}/{}/runtime/invocation/{}/error" ,
305252 self . endpoint, RUNTIME_API_VERSION , request_id
@@ -346,30 +293,33 @@ impl RuntimeClient for HttpRuntimeClient {
346293 /// # Panics
347294 /// If it cannot send the init error. In this case we panic to force the runtime
348295 /// to restart.
349- fn fail_init ( & self , e : & RuntimeApiError ) {
296+ pub fn fail_init ( & self , e : & RuntimeApiError ) {
350297 let uri: Uri = format ! ( "http://{}/{}/runtime/init/error" , self . endpoint, RUNTIME_API_VERSION )
351298 . parse ( )
352- . expect ( "Could not generate Runtime API URI" ) ;
299+ . expect ( "Could not generate Runtime URI" ) ;
353300 error ! ( "Calling fail_init Runtime API: {}" , e. to_response( ) . error_message) ;
354301 let err_body = serde_json:: to_vec ( & e. to_response ( ) ) . expect ( "Could not serialize error object" ) ;
355302 let req = self . get_runtime_post_request ( & uri, err_body) ;
356303
357- match self . http_client . request ( req) . wait ( ) {
358- Ok ( _) => { }
359- Err ( e) => {
304+ let resp = self
305+ . http_client
306+ . request ( req)
307+ . map_err ( |e| {
360308 error ! ( "Error while sending init failed message: {}" , e) ;
361309 panic ! ( "Error while sending init failed message: {}" , e) ;
362- }
363- }
310+ } ) . map ( |resp| {
311+ info ! ( "Successfully sent error response to the runtime API: {:?}" , resp) ;
312+ } ) ;
313+ tokio:: spawn ( resp) ;
364314 }
365315
366316 /// Returns the endpoint configured for this HTTP Runtime client.
367- fn get_endpoint ( & self ) -> String {
317+ pub fn get_endpoint ( & self ) -> String {
368318 self . endpoint . clone ( )
369319 }
370320}
371321
372- impl HttpRuntimeClient {
322+ impl RuntimeClient {
373323 /// Creates a Hyper `Request` object for the given `Uri` and `Body`. Sets the
374324 /// HTTP method to `POST` and the `Content-Type` header value to `application/json`.
375325 ///
0 commit comments