Skip to content

Switch to Chrono for deadline timestamp, doc fix #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lambda-runtime-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lambda-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 5 additions & 23 deletions lambda-runtime/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::env;

use chrono::Utc;

use backtrace;
use env as lambda_env;
use error::HandlerError;
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lambda-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down