diff --git a/Cargo.lock b/Cargo.lock index 86804f8e..4734f696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,10 +445,10 @@ name = "lambda_runtime" version = "0.1.0" dependencies = [ "backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.12 (registry+https://github.com/rust-lang/crates.io-index)", "hyper-tls 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "lambda_runtime_client 0.1.0", - "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", "rusoto_core 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", "rusoto_dynamodb 0.35.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/lambda-runtime-client/src/lib.rs b/lambda-runtime-client/src/lib.rs index eff48e2d..bbee313e 100644 --- a/lambda-runtime-client/src/lib.rs +++ b/lambda-runtime-client/src/lib.rs @@ -17,7 +17,7 @@ //! extern crate serde_derive; //! extern crate serde_json; //! -//! use lambda_runtime_client::{RuntimeClient, HttpRuntimeClient, EventContext}; +//! use lambda_runtime_client::{RuntimeClient, EventContext}; //! //! #[derive(Serialize, Deserialize, Debug)] //! struct CustomEvent { diff --git a/lambda-runtime/Cargo.toml b/lambda-runtime/Cargo.toml index 443ec310..07c1e713 100644 --- a/lambda-runtime/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -16,7 +16,7 @@ hyper = "^0.12" tokio = "^0.1" backtrace = "^0.3" lambda_runtime_client = { path = "../lambda-runtime-client"} -libc = "^0.2" +chrono = "^0.4" simple_logger = "^1" [dev-dependencies] diff --git a/lambda-runtime/src/context.rs b/lambda-runtime/src/context.rs index 47d19d04..88e699a6 100644 --- a/lambda-runtime/src/context.rs +++ b/lambda-runtime/src/context.rs @@ -1,5 +1,7 @@ use std::env; +use chrono::Utc; + use backtrace; use env as lambda_env; use error::HandlerError; @@ -101,28 +103,8 @@ impl Context { /// Returns the remaining time in the execution in milliseconds. This is based on the /// deadline header passed by Lambda's Runtime APIs. pub fn get_time_remaining_millis(&self) -> u128 { - self.deadline - systime::get_time_ms() - } -} - -/// Iternal implementation used to retriev the current `MONOTINIC_CLOCK` time from -/// libc; - -mod systime { - use libc; - - #[cfg(any(target_os = "macos", target_os = "ios"))] - pub(super) fn get_time_ms() -> u128 { - unsafe { u128::from(libc::mach_absolute_time()) / 1_000_000 } - } - - #[cfg(not(any(windows, target_os = "macos", target_os = "ios")))] - pub(super) fn get_time_ms() -> u128 { - let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 }; - unsafe { - libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts); - } - return ((ts.tv_sec as u128) * 1_000) + ((ts.tv_nsec as u128) / 1_000_000); + let millis = Utc::now().timestamp_millis(); + self.deadline - millis as u128 } } @@ -133,7 +115,7 @@ pub(crate) mod tests { use std::{thread::sleep, time}; fn get_deadline(secs: u8) -> u128 { - systime::get_time_ms() + (u128::from(secs) * 1_000) + Utc::now().timestamp_millis() as u128 + (u128::from(secs) * 1_000) } pub(crate) fn test_context(deadline: u8) -> Context { diff --git a/lambda-runtime/src/lib.rs b/lambda-runtime/src/lib.rs index aa1a7120..882d8fa7 100644 --- a/lambda-runtime/src/lib.rs +++ b/lambda-runtime/src/lib.rs @@ -42,8 +42,8 @@ extern crate log; extern crate backtrace; +extern crate chrono; extern crate lambda_runtime_client; -extern crate libc; extern crate serde; extern crate serde_json; extern crate tokio;