Skip to content

Commit 8c583c1

Browse files
authored
Switch to Chrono for deadline timestamp, doc fix (#4)
* Updated header values for provided runtime and switched to ms time remaining * Fixed issue with deadline not being set in context and switched to realtime clock to get correct epoch * Removed libc dependency and switched to chrono for deadline calculation * Fixed docs example not to import HttpRuntimeClient
1 parent 6b05202 commit 8c583c1

File tree

5 files changed

+9
-27
lines changed

5 files changed

+9
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambda-runtime-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! extern crate serde_derive;
1818
//! extern crate serde_json;
1919
//!
20-
//! use lambda_runtime_client::{RuntimeClient, HttpRuntimeClient, EventContext};
20+
//! use lambda_runtime_client::{RuntimeClient, EventContext};
2121
//!
2222
//! #[derive(Serialize, Deserialize, Debug)]
2323
//! struct CustomEvent {

lambda-runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ hyper = "^0.12"
1616
tokio = "^0.1"
1717
backtrace = "^0.3"
1818
lambda_runtime_client = { path = "../lambda-runtime-client"}
19-
libc = "^0.2"
19+
chrono = "^0.4"
2020
simple_logger = "^1"
2121

2222
[dev-dependencies]

lambda-runtime/src/context.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::env;
22

3+
use chrono::Utc;
4+
35
use backtrace;
46
use env as lambda_env;
57
use error::HandlerError;
@@ -101,28 +103,8 @@ impl Context {
101103
/// Returns the remaining time in the execution in milliseconds. This is based on the
102104
/// deadline header passed by Lambda's Runtime APIs.
103105
pub fn get_time_remaining_millis(&self) -> u128 {
104-
self.deadline - systime::get_time_ms()
105-
}
106-
}
107-
108-
/// Iternal implementation used to retriev the current `MONOTINIC_CLOCK` time from
109-
/// libc;
110-
111-
mod systime {
112-
use libc;
113-
114-
#[cfg(any(target_os = "macos", target_os = "ios"))]
115-
pub(super) fn get_time_ms() -> u128 {
116-
unsafe { u128::from(libc::mach_absolute_time()) / 1_000_000 }
117-
}
118-
119-
#[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
120-
pub(super) fn get_time_ms() -> u128 {
121-
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
122-
unsafe {
123-
libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts);
124-
}
125-
return ((ts.tv_sec as u128) * 1_000) + ((ts.tv_nsec as u128) / 1_000_000);
106+
let millis = Utc::now().timestamp_millis();
107+
self.deadline - millis as u128
126108
}
127109
}
128110

@@ -133,7 +115,7 @@ pub(crate) mod tests {
133115
use std::{thread::sleep, time};
134116

135117
fn get_deadline(secs: u8) -> u128 {
136-
systime::get_time_ms() + (u128::from(secs) * 1_000)
118+
Utc::now().timestamp_millis() as u128 + (u128::from(secs) * 1_000)
137119
}
138120

139121
pub(crate) fn test_context(deadline: u8) -> Context {

lambda-runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
extern crate log;
4343

4444
extern crate backtrace;
45+
extern crate chrono;
4546
extern crate lambda_runtime_client;
46-
extern crate libc;
4747
extern crate serde;
4848
extern crate serde_json;
4949
extern crate tokio;

0 commit comments

Comments
 (0)