53 Commits
Author SHA1 Message Date
Donald Ness 699ada1724 Add guards for code that relies on _Concurrency (#237)
This allows Xcode 13 to still compile on macOS 11 which does not have symbols for Concurrency yet
See swift-server/swift-service-lifecycle#110
2021-10-04 17:20:07 -07:00
Fabian Fett a9e15b13b3 Fix platform requirements (#214)
- Replace the placeholder platform requirements with `@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)` 
- Bump required dependency versions: `swift-nio`, `swift-log` and `swift-backtrace`
2021-07-21 20:47:35 +02:00
DwayneCoussement 9a7d9d0941 SNS MessageAttributes are optional (#208)
motivation: Notice that if you check the contents of the SQS records, some kind of transform happened on the original SNS message. MessageAttributes in the above example, these will not be set, so this is the initial fix.

changes: make MessageAttributes are optional
2021-06-11 10:14:48 -07:00
Fabian Fettandtomer doron 19fc295aa0 async/await support (#186)
- Add an `AsyncLambdaHandler`. Will be renamed to `LambdaHandler` as soon as we drop the current callback based `LambdaHandler`.
- The default way to use an `AsyncLambdaHandler` is to use `@main` to execute it. Don't use `Lambda.run` for it. We wan't to remove `Lambda.run` for 1.0.

Co-authored-by: tomer doron <tomerd@apple.com>
2021-04-27 22:47:11 +02:00
ML edbfa795b6 Updated S3.Event (#195)
motivation: support s3:ObjectRemoved:* event

changes:
* Changed the S3.Event object size field to an optional due to the missing field on a s3:ObjectRemoved:* event
* Added one additional test to check propper json decoding
2021-04-15 10:35:08 -07:00
Fabian Fett ab13e51984 Performance Improvements (#199)
Modifications:
- Replace `HTTPHandler` with `NIOHTTPClientResponseAggregator`, to fix a CoW issue. This increases performance.
- Ensure that we only use one `EventLoop` in tests.
- Use `syncOperations` to setup the `HTTPClient`.
2021-04-14 00:21:38 +02:00
Fabian Fett a49a87341a Update swiftformat (#196)
changes: 
* Update swiftformat from 0.44 to 0.47
* Adjust rules
2021-03-26 10:34:34 -07:00
DwayneCoussement e33e4afbf0 Add support for AppSync events. (#187)
motivation: Support AWS AppSync events
changes: add Codable models for AppSync events
2021-03-02 13:05:50 -08:00
tomer doron f731664c4d remove requirment on macOS 10.13 (#156)
motivation: simplify downstream libraries

changes: precondition out debugging uses cases on macOS < 10.13
2020-08-10 10:02:12 -07:00
pokryfka 1b594cad62 Generate trace ID in correct format (#139)
motivation: more correct Xray TraceID

changes:
* add AmazonHeaders::generateXRayTraceID
* Generate correct trace ID in LocalLambda Server
* Generate correct trace ID in LambdaRuntimeClientTest
2020-07-07 18:53:25 -07:00
Adam Fowler 5130278087 SES time zone (#135)
motivation: Sometimes common header date comes with an alphabetical timezone in brackets after the numeric timezone

changes:
* Support common header date format with brackets
* Update readme with SES Event link
2020-06-27 13:16:52 -07:00
Adam Fowler d2f5c1c324 Added SES Event type (#130)
motivation: support event triggers from SES

changes: Added SES Event type
2020-06-17 09:53:53 -07:00
Fabian Fett 437a60d7a0 Added shutdown() -> EventLoopFuture<Void> to the ByteBufferLambdaHandler (#122)
* Added `syncShutdown() throws` to the `ByteBufferLambdaHandler`

* Updated to use a `ShutdownContext`

* Review comments addressed.
2020-06-17 07:34:01 -07:00
Ro-M 77280662e3 Added header when reporting failing invocations or initializations #116 (#128)
* allow the users of client to provide headers
* add initialization error header
* add error header to failed invocations
* fix external setting of ip and port via config
* add tests
2020-06-17 06:55:05 -07:00
tomer doron d1a927650a add initialization context (#126)
motivation: in more complex initialization scearios you may want access to a logger or other utilities

changes:
* introduce new InitializationContext type that could be extended in the future without breaking the API in semantic-major way
* instead of passing in EventLoop to the handler factory, pass in a context that includes a Logger, an EventLoop and a ByteBufferAllocator
* fix a bug where we dont hop back to the event loop when coming back from the handler
* adjust tests to the new signature
2020-06-16 11:08:00 -07:00
Fabian Fett 0008e59bab Rename payload to event (#115) 2020-06-07 07:47:05 -07:00
tomer doron 8c24cffd97 fix context members to follow swift APi guidelines (#88)
motivation: nicer API

changes:
* requestID -> requestID
* traceId -> traceID
* invokedFunctionArn -> invokedFunctionARN
* adjust examples
2020-05-27 11:46:31 -07:00
Fabian Fettandtom doron 53930fef38 add debug functionality to test with mock server (#73)
motivation: allow end to end testing locally

changes:
* add a Lambda+LocalServer which exposes Lambda.withLocalServer available only in DEBUG mode
* local server can receive POST requests with payloads on a configurable endpoint and and send them to the Lambda
* move Lifecycle completely into EventLoop
* remove all locks since running in one EventLoop
* Use UUID for requestId

Co-authored-by: tom doron <tomer@apple.com>
2020-05-15 16:56:17 -07:00
Fabian Fettandtomer doron ab3ce640aa Use NIO in Single Threaded Mode (#68)
motivation: better performance 

changes:  employ SwiftNIO's new Single Threaded Mode

Co-authored-by: tomer doron <tomerd@apple.com>
2020-05-12 13:41:10 -07:00
Fabian Fett 1d9637279a Moved Foundation to AWSLambdaRuntime; AWSLambdaRuntime renamed to AWSLambdaRuntimeCore (#41)
motivation: enable non-foundation module for performance sensitive use cases

changes: 
* rename AWSLambdaRuntime to AWSLambdaRuntimeCore 
* create AWSLambdaRuntime for Foundation dependent functionality 
* have (new) AWSLambdaRuntime export AWSLambdaRuntimeCore
* adjust tests
2020-05-07 09:37:28 -07:00
Fabian Fett 29d631709f Added DynamoDB Event (#65)
motivation: support DynamoDB triggers

changes: added DynamoDB Event abstractions and encoders/decoders
2020-05-07 09:05:11 -07:00
Fabian Fett 9df8a1a468 Rename RequestWork to GetNextInvocation (#74) 2020-05-07 08:37:07 -07:00
tomer doron 2324074c33 improve type aliases and API docs (#64)
motivation: better documentation, happier users

changes: 
* add more information on public APIs
* move and simplify type aliases
2020-04-28 14:31:43 -07:00
Fabian Fett 8509674ed0 Add apigateway and alb (#52)
motivation: support ALB and APIGateway Events

changes: add Codable abstractions for ALB and APIGateway v1+v2  Events
2020-04-27 10:49:44 -07:00
Fabian Fett 920302a4c3 Improve testing utils (#60)
motivation: make testing lambda easy

changes:
* add a AWSLambdaTesting module
* add helper methods for testing different types of Lambda handlers / closures
2020-04-23 00:11:05 -07:00
Fabian Fett 7a946064e7 Added SNS & SQS Events (#46)
* Added SNS & SQS Events
* Added Base64 util
2020-04-03 10:36:47 -07:00
Fabian Fett 3fad2f5751 Added Cloudwatch Event (#48)
motivation: support cloud watch events

changes: 
* add aws region abstraction
* add generic cloud watch abstraction 
* add common / key cloud watch events
2020-03-30 14:07:18 -07:00
Fabian Fett 643dd91375 Added S3 Event (#45)
motivation: support S3 Events

changes:
- Created `AWSLambdaEvents` target
- Added S3 Event as first event
- Use propertyWrapper for customers Encoding/Decoding
2020-03-24 16:01:40 -07:00
Fabian Fett 2cd6bce4d3 Remove use of JSONEncoder for Error Reporting (#42)
motivation: better performance for error response encoding 

changes: use string builder instead of Codable -> String with JSONEncoder
2020-03-18 20:23:53 -07:00
tomer doron b61aab012e rename project to SwiftAWSLambdaRuntime (#38)
motivation: unique, accurate name

changes:
* rename project to SwiftAWSLambdaRuntime
* rename main module to AWSLambdaRuntime
* rename / simplify sample module names
* adjust readme and scripts
2020-03-13 14:48:25 -07:00
tomer doron 88f3b504f4 swift 5.2 (#37)
motivation: amazon linux support will land in 5.2

changes:
* remove inline as its default in 5.2
* remove linux test and lean on --enable-test-discovery
* adjust package syntax
* format code to match 5.2
2020-03-13 12:54:12 -07:00
tomer doron 7d1b309035 refactor api 1 (#33)
motivation: nicer apis, happier users

changes:
* use ByteBuffer + EventLoopFuture for core APIs instead of byte array and callbacks
* create three base protocols: ByteBufferLambdaHandler, EventLoopLambdaHandler and LambdaHandler
* abstract common encoding/decoding functionality into a EventLoopLambdaHandler, reducing most code from string/codable handlers
* only the highest level LambdaHandler is offloaded to a DispatchQueue, lower level is run on the same EventLoop as the core library
* refine encoding/decoding logic
* inline all the things
* adjust tests
* adjust api docs
* adjust readme
2020-03-13 11:44:22 -07:00
tomer doron 2e04511741 move config, lifecycle, context to separate files (#32)
motivation: easier code navigation

changes: move config, lifecycle, context to separate source files
2020-03-11 12:01:18 -07:00
tomer doron a05185185e refactor bootstrapping (#28)
motivation: make initialization logic more robust, allowing setup at contructor time and also async bootstrap

changes:
* remove the initialize method and replace it with a factory pattern. 
* update core API and logic to support new initialization flow.
* add tests to various initialization flows
2020-03-10 11:17:31 -07:00
Fabian Fettandtom doron d0466172b6 Add Deadline (#27)
Motivation:
- As a developer I want to be able to know how much time I have left to execute my lambda, before it times out

Changes:
- storing the deadline on Context and Invocation
- added getRemainingTime() to Context which returns a TimeAmount
- Renamed `Invocation.deadline` to `Invocation.deadlineInMillisSinceEpoch` to better reflect 
- fixed MockLambdaServer to not return “keep-alive” anymore

Co-authored-by: tom doron <tomer@apple.com>
2020-03-10 10:30:12 -07:00
tomer doron a6af55cc9a refactor tests (#23)
motivation: get test suite in better shape for core API changes

changes:
* test lambda behavior instead of sevrer error handling in string/codable test suites
* add more basic behavior tests to core tesr suite
* refacotr common parts to make tests DRYer
* remove custom result types (pre swift 5 leftovers)
2020-03-09 09:59:55 -07:00
Fabian Fett ddee38c367 LambdaRuntimeClient works with Invocation (#19)
### Motivation:

- We want to store different entities that are needed when executing a handler within the LambdaContext (Logger, EventLoop, ByteBufferAllocator, …)
- Currently the LambdaRuntimeClient creates the LambdaContext. Having the LambdaContext with the Logger, EventLoop and ByteBufferAllocator be created from the LambdaRuntimeClient feels to me too much for me.
- Conceptionally the Lambda control plane api call is “get next Invocation” (API naming)

### Changes:

- LambdaRuntimeClient responds with an Invocation and does not use the LambdaContext at all anymore.
- LambdaRunner creates the LambdaContext with the Invocation, Logger and EventLoop.
- LambdaContext has been renamed to Lambda.Context
- Lambda.Context is a class now, since it is conceptionally not a value type and might be passed around a lot
- Lambda.Context properties `traceId`, `invokedFunctionArn`, `deadline` are not optional anymore since they will be always set when executing a lambda
- Creating an Invocation can fail with LambdaRuntimeClientError.invocationMissingHeader(String), if non optional headers are not present
- the test MockLambdaServer and the performance test MockServer always return headers for deadline, traceId and function arn (static for now – could be changed with Behaviour flag?!)

### Open ends:

- we will need to build some kind of Deadline into the context (See also #9 - probably for a different PR)
- we have a stupid mapping between ByteBuffer and [UInt8] in the LambdaRunner for now (marked with two TODOs). I don’t want to change this in this PR since it will lead to huge merge conflicts down the road with the potentiall API changes we have in mind.
2020-03-08 15:10:03 -07:00
tomer doron 08f75f51c7 refactor (#13)
motivation: simpler concurrency, better tests

changes:
* ensure http client is called in a single-threaded manner and remove locks 
* refactor test
* make mock server more robust
2020-03-08 12:41:09 -07:00
Fabian Fett df6638d63b Don’t create an EventLoopFuture chain (#4)
motivation: prevent memory bloat

changes: 
* recurse instead of event loop chain
* fix flaky test
2020-03-06 12:27:52 -08:00
Tom Doron 209906d390 performance improvments (#17)
motivation: better performance

changes:
* simply url configuration to ip and port so we can reoslve the socket add ress faster
* skip happy eyeballs
* limit eventloop group to 1 thread
* remove use of UUID and URL
* fix backtrace url
2020-03-04 13:54:55 -08:00
Tom Doron c3d4ad5a39 performance tests (#16)
motivation: benchmark for comparison of warm/cold runs

changes:
* refactor configuration
* add mock server that can be used by perf tests
* add simple perf test script
* change redundant classes to structs, make remaining classes final
* make offloading opt-in
* safer locking
* fix format
2020-03-04 11:01:15 -08:00
Tom Doron fb6de922de more refactoring (#14)
motivation: improve code quality, handle keep-alive

changes:
* refactor http client to keep warm connection to runtime engine when possible and handle request timeouts
* refactor configuration into the main lambda class to surface it more clearly
* refactor lifecycle code handle shutdown properly and easier to reason about
* add tests
2019-09-26 07:11:12 -07:00
Tom Doron 2201da9944 more refactoring (#13)
motivation: make lambda execution sequence easier to reason about

changes:
* add `peekFailure` extension to `EventLoopFuture` to handle error without side effects
* update process chain to make use of `peekFailure`
2019-09-18 08:46:53 -07:00
Tom Doron c31ee33105 refactor (#12)
motivation: make code simpler to reason about, better use of swift-nio

changes:
* remove main loop on global queue + wait, replace with recursion
* make better use of EvenLoopFuture, instead of result types to signal errors
* inject lifecycleId so we can share offloading queue
* improve logging
* adjust and improve tests
* update sanity and generate linux tests script to work better with dates and swiftformat
2019-09-16 10:05:02 +01:00
George Barnett 5206176688 Add lambda initialization (#11)
Motivation:

When initialization is expensive it's useful for it to be done upfront
instead of for each invocation of the lambda. Similarly, it's helpful
for errors thrown during initialization to be reported to the runtime.

Modifications:

- Add support for initialization in 'LambdaHandler'
- Report init errors to '/init/error'

Result:

Implementers of 'LambdaHandler' can run their own initialization and have
errors reported.
2019-09-10 11:01:18 -04:00
Tom Doron 4b9cd8350c update code to swift5 and nio2 (#10)
* update code to swift5 and nio2

motivation: support newer version of swift and underlying dependencies

changes:
* adjust code to swift 5
* use swift 5 result type and better error handling for json encoding/decoding
* adjust code to nio 2
* add dependency on swift-log for logging instead of print statements
* make logger as part of context
* improve error handling
* add dependecy on swift-backtrace to capture crashes
* improve lifecycle 
* adjust tests
* add default formatting rules and fix formatting
* improve readme
2019-08-05 14:05:48 -07:00
Tom Doron 20309e2ad7 code style (#9)
motivation: nicer code

changes
* rename Result to ResultType since swift is getting formal Result soon
* use Handler instead of T for generics in CodableLambda which is more readable
* simpler bytes->String decoding
2018-12-03 18:25:46 -08:00
Tom Doron e3f3a979ee add docs (#8)
motivation: help users better undersatnd how to use the library

changes:
* add API docs to public APIs
* add narattive docs in README
2018-11-30 15:35:37 -08:00
Tom Doron 61a90e123c hide LambdaLifecycleResult from users (#6)
hide LambdaLifecycleResult from users

motivation: cleaner end-user API

changes:
* make LambdaLifecycleResult internal
* change run, _run method signatures to hide LambdaLifecycleResult from end-user API
* adjust tests
2018-11-30 15:11:53 -08:00
Tom Doron 6d618f2c33 shutdown handler, lambda api adjustment, latest swift-nio (#5)
* shutdown handler, lambda api adjustment, latest swift-nio

motivation:
* shutdown: while not required, demonstrate better example for shutdown
* latest nio and api adjustments: fixes runtime issues observed when running for realz

changes:
* add signal based shutdown hook that terminates and cleans up resources
* pull latest swift-nio to get fix to thread renaming
* adjust aws lambda api prefix and headers
* reformat linux tests code
2018-11-21 12:57:10 -05:00