Skip to content

Commit 6b05202

Browse files
authored
Updated header values for provided runtime (#2)
* 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
1 parent c3dfd7b commit 6b05202

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

lambda-runtime-client/src/client.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ pub enum LambdaHeaders {
3434
impl fmt::Display for LambdaHeaders {
3535
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3636
match self {
37-
LambdaHeaders::RequestId => write!(f, "X-Amz-Aws-Request-Id"),
38-
LambdaHeaders::FunctionArn => write!(f, "X-Amz-Invoked-Function-Arn"),
39-
LambdaHeaders::TraceId => write!(f, "X-Amzn-Trace-Id"),
40-
LambdaHeaders::Deadline => write!(f, "X-Amz-Deadline-Ns"),
41-
LambdaHeaders::ClientContext => write!(f, "X-Amz-Client-Context"),
42-
LambdaHeaders::CognitoIdentity => write!(f, "X-Amz-Cognito-Identity"),
37+
LambdaHeaders::RequestId => write!(f, "Lambda-Runtime-Aws-Request-Id"),
38+
LambdaHeaders::FunctionArn => write!(f, "Lambda-Runtime-Invoked-Function-Arn"),
39+
LambdaHeaders::TraceId => write!(f, "Lambda-Runtime-Trace-Id"),
40+
LambdaHeaders::Deadline => write!(f, "Lambda-Runtime-Deadline-Ms"),
41+
LambdaHeaders::ClientContext => write!(f, "Lambda-Runtime-Client-Context"),
42+
LambdaHeaders::CognitoIdentity => write!(f, "Lambda-Runtime-Cognito-Identity"),
4343
}
4444
}
4545
}

lambda-runtime/src/context.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl Context {
100100

101101
/// Returns the remaining time in the execution in milliseconds. This is based on the
102102
/// deadline header passed by Lambda's Runtime APIs.
103-
pub fn get_time_remaining_mills(&self) -> u128 {
104-
(self.deadline - systime::get_time_ns()) / 1000 / 1000
103+
pub fn get_time_remaining_millis(&self) -> u128 {
104+
self.deadline - systime::get_time_ms()
105105
}
106106
}
107107

@@ -112,17 +112,17 @@ mod systime {
112112
use libc;
113113

114114
#[cfg(any(target_os = "macos", target_os = "ios"))]
115-
pub(super) fn get_time_ns() -> u128 {
116-
unsafe { u128::from(libc::mach_absolute_time()) }
115+
pub(super) fn get_time_ms() -> u128 {
116+
unsafe { u128::from(libc::mach_absolute_time()) / 1_000_000 }
117117
}
118118

119119
#[cfg(not(any(windows, target_os = "macos", target_os = "ios")))]
120-
pub(super) fn get_time_ns() -> u128 {
120+
pub(super) fn get_time_ms() -> u128 {
121121
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
122122
unsafe {
123-
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
123+
libc::clock_gettime(libc::CLOCK_REALTIME, &mut ts);
124124
}
125-
return (ts.tv_sec as u128) * 1000000000 + (ts.tv_nsec as u128);
125+
return ((ts.tv_sec as u128) * 1_000) + ((ts.tv_nsec as u128) / 1_000_000);
126126
}
127127
}
128128

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

135135
fn get_deadline(secs: u8) -> u128 {
136-
systime::get_time_ns() + (u128::from(secs) * 1000 * 1000 * 1000)
136+
systime::get_time_ms() + (u128::from(secs) * 1_000)
137137
}
138138

139139
pub(crate) fn test_context(deadline: u8) -> Context {
@@ -160,7 +160,7 @@ pub(crate) mod tests {
160160
println!("Set deadline to: {}", ctx.deadline);
161161
sleep(time::Duration::new(2, 0));
162162

163-
let remaining = ctx.get_time_remaining_mills();
163+
let remaining = ctx.get_time_remaining_millis();
164164
assert!(
165165
remaining > 7800 && remaining < 8200,
166166
"Remaining time in millis outside the expected range: {}",

lambda-runtime/src/runtime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ where
259259
handler_ctx.xray_trace_id = invocation_ctx.xray_trace_id;
260260
handler_ctx.client_context = invocation_ctx.client_context;
261261
handler_ctx.identity = invocation_ctx.identity;
262+
handler_ctx.deadline = invocation_ctx.deadline;
262263

263264
(ev, handler_ctx)
264265
}
@@ -304,7 +305,8 @@ pub(crate) mod tests {
304305
.get_runtime_api_endpoint()
305306
.expect("Could not get runtime endpoint"),
306307
None,
307-
).expect("Could not initialize client");
308+
)
309+
.expect("Could not initialize client");
308310
let handler = |_e: String, _c: context::Context| -> Result<String, HandlerError> { Ok("hello".to_string()) };
309311
let retries: i8 = 3;
310312
let runtime = Runtime::new(

0 commit comments

Comments
 (0)