|
| 1 | +// #![deny(clippy::all, clippy::cargo)] |
| 2 | +// #![warn(missing_docs,? nonstandard_style, rust_2018_idioms)] |
| 3 | + |
| 4 | +use async_trait::async_trait; |
| 5 | +use hyper::client::{connect::Connection, HttpConnector}; |
| 6 | +use lambda_runtime_client::Client; |
| 7 | +use serde::Deserialize; |
| 8 | +use tokio::io::{AsyncRead, AsyncWrite}; |
| 9 | +use tokio_stream::{StreamExt}; |
| 10 | +use tower_service::Service; |
| 11 | +use tracing::trace; |
| 12 | + |
| 13 | +pub mod requests; |
| 14 | + |
| 15 | +pub type Error = lambda_runtime_client::Error; |
| 16 | + |
| 17 | +#[derive(Debug, Deserialize)] |
| 18 | +#[serde(rename_all = "camelCase")] |
| 19 | +pub struct Tracing { |
| 20 | + pub r#type: String, |
| 21 | + pub value: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Debug, Deserialize)] |
| 25 | +#[serde(rename_all = "camelCase")] |
| 26 | +pub struct InvokeEvent { |
| 27 | + deadline_ms: u64, |
| 28 | + request_id: String, |
| 29 | + invoked_function_arn: String, |
| 30 | + tracing: Tracing, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, Deserialize)] |
| 34 | +#[serde(rename_all = "camelCase")] |
| 35 | +pub struct ShutdownEvent { |
| 36 | + shutdown_reason: String, |
| 37 | + deadline_ms: u64, |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug, Deserialize)] |
| 41 | +#[serde(rename_all = "UPPERCASE", tag = "eventType")] |
| 42 | +pub enum NextEvent { |
| 43 | + Invoke(InvokeEvent), |
| 44 | + Shutdown(ShutdownEvent), |
| 45 | +} |
| 46 | + |
| 47 | +/// A trait describing an asynchronous extension. |
| 48 | +#[async_trait] |
| 49 | +pub trait Extension { |
| 50 | + async fn on_invoke(&self, extension_id: &str, event: InvokeEvent) -> Result<(), Error>; |
| 51 | + async fn on_shutdown(&self, extension_id: &str, event: ShutdownEvent) -> Result<(), Error>; |
| 52 | +} |
| 53 | + |
| 54 | +struct Runtime<'a, C: Service<http::Uri> = HttpConnector> { |
| 55 | + extension_id: &'a str, |
| 56 | + client: Client<C>, |
| 57 | +} |
| 58 | + |
| 59 | +impl<'a, C> Runtime<'a, C> |
| 60 | +where |
| 61 | + C: Service<http::Uri> + Clone + Send + Sync + Unpin + 'static, |
| 62 | + <C as Service<http::Uri>>::Future: Unpin + Send, |
| 63 | + <C as Service<http::Uri>>::Error: Into<Box<dyn std::error::Error + Send + Sync>>, |
| 64 | + <C as Service<http::Uri>>::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static, |
| 65 | +{ |
| 66 | + pub async fn run(&self, extension: impl Extension) -> Result<(), Error> { |
| 67 | + let client = &self.client; |
| 68 | + let extension_id = self.extension_id; |
| 69 | + |
| 70 | + let incoming = async_stream::stream! { |
| 71 | + loop { |
| 72 | + trace!("Waiting for next event (incoming loop)"); |
| 73 | + let req = requests::next_event_request(extension_id)?; |
| 74 | + let res = client.call(req).await; |
| 75 | + yield res; |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + tokio::pin!(incoming); |
| 80 | + while let Some(event) = incoming.next().await { |
| 81 | + trace!("New event arrived (run loop)"); |
| 82 | + let event = event?; |
| 83 | + let (_parts, body) = event.into_parts(); |
| 84 | + |
| 85 | + let body = hyper::body::to_bytes(body).await?; |
| 86 | + trace!("{}", std::str::from_utf8(&body)?); // this may be very verbose |
| 87 | + let event: NextEvent = serde_json::from_slice(&body)?; |
| 88 | + |
| 89 | + match event { |
| 90 | + NextEvent::Invoke(event) => { |
| 91 | + extension.on_invoke(extension_id, event).await?; |
| 92 | + } |
| 93 | + NextEvent::Shutdown(event) => { |
| 94 | + extension.on_shutdown(extension_id, event).await?; |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +async fn register<C>(client: &Client<C>, extension_name: &str) -> Result<String, Error> |
| 104 | +where |
| 105 | + C: Service<http::Uri> + Clone + Send + Sync + Unpin + 'static, |
| 106 | + <C as Service<http::Uri>>::Future: Unpin + Send, |
| 107 | + <C as Service<http::Uri>>::Error: Into<Box<dyn std::error::Error + Send + Sync>>, |
| 108 | + <C as Service<http::Uri>>::Response: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static, |
| 109 | +{ |
| 110 | + let req = requests::register_request(extension_name)?; |
| 111 | + let res = client.call(req).await?; |
| 112 | + // ensure!(res.status() == http::StatusCode::OK, "Unable to register extension",); |
| 113 | + |
| 114 | + let ext_id = res.headers().get(requests::EXTENSION_ID_HEADER).unwrap().to_str()?; |
| 115 | + Ok(ext_id.into()) |
| 116 | +} |
| 117 | + |
| 118 | +pub async fn run(extension: impl Extension) -> Result<(), Error> { |
| 119 | + let args: Vec<String> = std::env::args().collect(); |
| 120 | + |
| 121 | + let client = Client::builder().build().expect("Unable to create a runtime client"); |
| 122 | + let extension_id = register(&client, &args[0]).await?; |
| 123 | + let runtime = Runtime { |
| 124 | + extension_id: &extension_id, |
| 125 | + client, |
| 126 | + }; |
| 127 | + |
| 128 | + runtime.run(extension).await |
| 129 | +} |
0 commit comments