Skip to content

Commit 798b178

Browse files
committed
Prefer deadline as a Time instead of just ms
1 parent 9c95839 commit 798b178

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ and get tiny binaries (250kb) and single-digit millisecond cold starts!
1111
Create a `bootstrap.nim` with the following:
1212

1313
```nim
14-
import awslambda, json
14+
import awslambda, json, times
1515
1616
proc handler(event: JsonNode, context: LambdaContext): JsonNode =
17-
echo "Hi from nim!", context.awsRequestId, " Deadline is: ", context.deadlineMs, "ms"
17+
echo "Hi from nim! Invocation will timeout at: ", context.deadline.format("yyyy-MM-dd'T'HH:mm:ss'.'fff")
1818
1919
event["newKey"] = %*"newVal"
2020
@@ -66,7 +66,7 @@ type
6666
logStreamName: string
6767
awsRequestId: string
6868
invokedFunctionArn: string
69-
deadlineMs: int
69+
deadline: Time
7070
identity: JsonNode
7171
clientContext: JsonNode
7272
```

awslambda.nim

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, httpclient, json, strutils
1+
import os, httpclient, json, strutils, times
22

33
type
44
LambdaContext* = tuple
@@ -9,7 +9,7 @@ type
99
logStreamName: string
1010
awsRequestId: string
1111
invokedFunctionArn: string
12-
deadlineMs: int
12+
deadline: Time
1313
identity: JsonNode
1414
clientContext: JsonNode
1515

@@ -32,6 +32,8 @@ proc startLambda*(handler: proc(event: JsonNode, context: LambdaContext): JsonNo
3232

3333
putEnv("_X_AMZN_TRACE_ID", res.headers.getOrDefault("Lambda-Runtime-Trace-Id"))
3434

35+
var deadlineMs = parseInt(res.headers.getOrDefault("Lambda-Runtime-Deadline-Ms"))
36+
3537
var context: LambdaContext
3638
context = (
3739
functionName: functionName,
@@ -41,7 +43,7 @@ proc startLambda*(handler: proc(event: JsonNode, context: LambdaContext): JsonNo
4143
logStreamName: logStreamName,
4244
awsRequestId: string(res.headers.getOrDefault("Lambda-Runtime-Aws-Request-Id")),
4345
invokedFunctionArn: string(res.headers.getOrDefault("Lambda-Runtime-Invoked-Function-Arn")),
44-
deadlineMs: parseInt(res.headers.getOrDefault("Lambda-Runtime-Deadline-Ms")),
46+
deadline: initTime(deadlineMs div 1_000, (deadlineMs mod 1_000) * 1_000_000),
4547
identity: newJNull(),
4648
clientContext: newJNull(),
4749
)

awslambda.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.1.0"
3+
version = "0.2.0"
44
author = "Michael Hart"
55
description = "A package to compile nim functions for AWS Lambda"
66
license = "MIT"

0 commit comments

Comments
 (0)