Skip to content

Commit 8aea720

Browse files
author
Nick Tchayka
authored
Merge pull request #76 from dnikolovv/more-efficient-context-initialization
More efficient context initialization
2 parents 1eda275 + c2f82bb commit 8aea720

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

aws-lambda-haskell-runtime.cabal

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: c21b3c988844f7c4bfb806aff4e307c5d5dc63a7973eff4a83eb329e7bccb1c9
7+
-- hash: f12bd3188248690adb9f0c4972dd66cb983b9057555d14205384c600255cb999
88

99
name: aws-lambda-haskell-runtime
10-
version: 3.0.0
10+
version: 3.0.1
1111
synopsis: Haskell runtime for AWS Lambda
1212
description: Please see the README on GitHub at <https://github.com/theam/aws-lambda-haskell-runtime#readme>
1313
category: AWS

package.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aws-lambda-haskell-runtime
2-
version: 3.0.0
2+
version: 3.0.1
33
github: "theam/aws-lambda-haskell-runtime"
44
license: Apache-2.0
55
author: Nikita Tchayka

src/Aws/Lambda/Runtime.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
2+
13
{-# LANGUAGE ScopedTypeVariables #-}
24
{-# LANGUAGE TypeApplications #-}
35

@@ -35,10 +37,13 @@ runLambda initializeCustomContext callback = do
3537
manager <- Http.newManager httpManagerSettings
3638
customContext <- initializeCustomContext
3739
customContextRef <- newIORef customContext
40+
context <- Context.initialize @context customContextRef `catch` errorParsing `catch` variableNotSet
3841
forever $ do
3942
lambdaApi <- Environment.apiEndpoint `catch` variableNotSet
4043
event <- ApiInfo.fetchEvent manager lambdaApi `catch` errorParsing
41-
context <- Context.initialize @context customContextRef event `catch` errorParsing `catch` variableNotSet
44+
45+
-- Purposefully shadowing to prevent using the initial "empty" context
46+
context <- Context.setEventData context event
4247

4348
(((invokeAndRun callback manager lambdaApi event context
4449
`Checked.catch` \err -> Publish.parsingError err lambdaApi context manager)

src/Aws/Lambda/Runtime/Context.hs

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Aws.Lambda.Runtime.Context
22
( Context(..)
33
, initialize
4+
, setEventData
45
) where
56

67
import Control.Exception.Safe.Checked
@@ -29,25 +30,40 @@ initialize
2930
:: Throws Error.Parsing
3031
=> Throws Error.EnvironmentVariableNotSet
3132
=> IORef context
32-
-> ApiInfo.Event
3333
-> IO (Context context)
34-
initialize customContextRef ApiInfo.Event{..} = do
34+
initialize customContextRef = do
3535
functionName <- Environment.functionName
3636
version <- Environment.functionVersion
3737
logStream <- Environment.logStreamName
3838
logGroup <- Environment.logGroupName
3939
memoryLimitInMb <- Environment.functionMemory
4040

41-
Environment.setXRayTrace traceId
42-
pure Context
41+
pure $ Context
4342
{ functionName = functionName
4443
, functionVersion = version
4544
, logStreamName = logStream
4645
, logGroupName = logGroup
4746
, memoryLimitInMb = memoryLimitInMb
48-
, invokedFunctionArn = invokedFunctionArn
49-
, xrayTraceId = traceId
50-
, awsRequestId = awsRequestId
51-
, deadline = deadlineMs
5247
, customContext = customContextRef
48+
49+
-- We set those to "empty" values because they will be assigned
50+
-- from the incoming event once one has been received. (see setEventData)
51+
, invokedFunctionArn = mempty
52+
, xrayTraceId = mempty
53+
, awsRequestId = mempty
54+
, deadline = 0
5355
}
56+
57+
-- | Sets the context's event data
58+
setEventData
59+
:: Context context
60+
-> ApiInfo.Event
61+
-> IO (Context context)
62+
setEventData context ApiInfo.Event{..} = do
63+
Environment.setXRayTrace traceId
64+
65+
return $ context
66+
{ invokedFunctionArn = invokedFunctionArn
67+
, xrayTraceId = traceId
68+
, awsRequestId = awsRequestId
69+
, deadline = deadlineMs }

0 commit comments

Comments
 (0)