57 Commits

Author SHA1 Message Date
Sébastien Stormacq 8676c8933a apply swiftformat (#342)
* apply swiftformat

* update dep on Swift Docc to v1.3.0

* force usage of swift docc plugin 1.3.0
2024-08-26 12:25:41 +02:00
Alessio Buratti 79fa2c2bee [Draft] Detached tasks (#334)
* First prototype

* Fix build

* Removes task cancellation

https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666713889

* Force user to handle errors

https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666712903

* Remove EventLoop API

https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666712244

* Make DetachedTaskContainer internal

https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666710596
https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666706576

* Removes @unchecked Sendable

https://github.com/swift-server/swift-aws-lambda-runtime/pull/334#discussion_r1666707646

* Invoke awaitAll() from async context

* Fix ambiguous expression type for swift 5.7

* Fix visibility of detachedBackgroundTask

* Add swift-doc

* Add example usage to readme

* Add tests

---------

Co-authored-by: Sébastien Stormacq <sebastien.stormacq@gmail.com>
2024-08-23 18:50:22 +02:00
Sébastien Stormacq 872183bbe9 allow compilation with the static linux sdk (musl) (#331) 2024-06-26 10:09:25 +02:00
jsonfry 17a993e3b1 Local lambda invocation endpoint env var (#321)
add #321
2024-05-22 14:37:53 +02:00
Sébastien Stormacq 221978224c Add tracing of request events + mention LOG_LEVEL in README (#315)
* add tracing of request events + mention LOG_LEVEL in README

* fix typo

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>

* clarify the use of env variable and the swift cli

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>

* clarify language about HTTP server

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>

* formatting

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>

* factor in additional suggestions from @mahdibm

* combine two logger statement into one and print only for Kb of payload

---------

Co-authored-by: Mahdi Bahrami <github@mahdibm.com>
2024-05-15 09:56:35 +02:00
tomer doron 8d9f44b783 allow custom initialization of the HandlerType of the LambdaRuntime (#310)
Motivation:

Provide the flexibility for custom initialization of the HandlerType as this will often be required by higher level frameworks.

Modifications:
* Modify the LambdaRuntime type to accept a closure to provide the handler rather than requiring that it is provided by a static method on the Handler type
* Update downstream code to use HandlerProvider
* Update upstream code to support passing Handler Type of Handler Provider
* Add and update tests

Originally suggested and coded by @tachyonics in https://github.com/swift-server/swift-aws-lambda-runtime/pull/308
2024-01-18 13:48:10 -08:00
tomer doron 42eaaf4fb1 remove redundant pre-concurrency flag (#314)
motivation: address warnings

changes:
* remove pre-concurrency flag when not longer needed
2023-12-18 11:49:03 -08:00
tachyonics 0fbd8448af Don't log work fetching failure at error. (#313) 2023-12-08 12:49:03 -08:00
Sébastien Stormacq c85c875593 [doc] Getting Started documentation and tutorial (#300)
improved getting started documentation, with DocC
2023-09-11 10:43:06 -07:00
tomer doron e221dd8362 remove swift-backtrace in swift >= 5.9 (#305)
motivation: swift 5.9 ships with builtin backtrace support \o/

changes:
* remove the dependency on swift-backtrace when using swift 5.9 or above
* conditionalize the call to Backtrace.install to relevant versions only
2023-09-07 18:39:11 -07:00
tomer doron a5fb165f65 fix concurrency api usage (#282)
motivation: cleanup incorrect concurent code in test

changes: use .get instead of .wait
2023-01-10 16:24:11 -08:00
tomer doron c915322eca API Refactoring (#273)
motivation: define stable API in preperation 1.0 release

changes:
* require swift 5.7, remove redundant backwards compatibility code
* make LambdaHandler, EventLoopLambdaHandler, and ByteBufferLambdaHandler disjointed protocols to reduce API surface area
* create coding wrappers for LambdaHandler and EventLoopLambdaHandler to provide bridge to ByteBufferLambdaHandler
* reuse output ByteBuffer to reduce allocations
* add new SimpleLambdaHandler with no-op initializer for simple lambda use cases
* update callsites and tests
* update examples

Co-authored-by: Yim Lee <yim_lee@apple.com>
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2022-11-09 10:08:36 -08:00
Yim Lee 98a23b64bb DocC setup (#270) 2022-08-21 20:29:08 -07:00
Fabian Fett ac52960abd Initialize LambdaRuntime with concrete HandlerType + Docu fixes (#260) 2022-04-20 22:14:03 +02:00
tomer doron e5b44962bd Prefix data structures with Lambda instead of namespacing them (#256)
motivation: consisten naming convention

changes:
* Lambda.InitializationContext -> LambdaInitializationContext
* Lambda.Runner -> LambdaRunner
* Lambda.Configuration -> LambdaConfiguration
* Lambda.RuntimeError -> LambdaRuntimeError
* adjust call sites, tests, and examples
2022-04-15 13:33:54 +02:00
tomer doron 3c3529b4dc adoption of sendable (#252)
motivation: adopt to sendable requirments in swift 5.6

changes:
* define sendable shims for protocols and structs that may be used in async context
* adjust tests
* add a test to make sure no warning are emitted
2022-04-14 09:39:00 -07:00
tomer doron 4d0bba4617 termination handler (#251)
motivation: make it simpler to register shutdown hooks

changes:
* introduce Terminator helper that allow registering and de-registaring shutdown handlers
* expose the new terminator hanler on the InitializationContext and deprecate ShutdownContext
* deprecate the Handler::shutdown protocol requirment
* update the runtime code to use the new terminator instead of calling shutdown on the handler
* add and adjust tests
2022-04-13 12:16:26 -07:00
Stefan Nienhuis c1f694f35a Add default value for traceID header (#246)
* Add default value for traceID header
* Implement Invocation traceID test
2022-02-19 14:16:51 -08:00
Fabian Fett d06d22c0e0 Lambda factory as a protocol requirement. (#244) 2022-01-13 19:10:20 +01:00
Fabian Fett 5d235c0a3b Add ControlPlaneRequestEncoder (#239)
Add a new `ControlPlaneRequestEncoder` that encodes all control plane requests into an existing, reused buffer.
2021-12-11 13:35:57 +01:00
Fabian Fett bc78f60e32 Add LambdaRequestID (#243)
If we want to minimize allocations for every invocation, we need to look at types that currently allocate. Currently we use a String to hold the request id. However since the request id is a uuid, that string is 36 characters long. This is way above the 15 character string allocation threshold. The go to type in this case would be `UUID`. However `UUID` is in Foundation and we want to keep the lambda runtime Foundation free.

This pr introduces a LambdaRequestID to represent a uuid. One nice side effect of having our own uuid case is: We can make writing the uuid-string to a ByteBuffer allocation free (since no intermediate translation to a string is needed first).
2021-12-11 13:05:48 +01:00
Fabian Fett b8d89ca208 Add ControlPlaneRequest, ControlPlaneResponse (#238)
### Motivation:

In the coming weeks, we want to restructure the LambdaRuntime internals in such a way, that we allocate a lot less resources per invocation. To accomplish this, we want to encode and decode the ControlPlaneAPI requests directly from their semantic value. For this reason, we need a specialized ControlPlaneRequest and ControlPlaneResponse type that we can encode from and decode into.

### Modifications:

- Add `ControlPlaneRequest`, `ControlPlaneResponse`
- Move `Invocation` and `ErrorResponse` into same file as `ControlPlaneRequest`
- Remove `Lambda` namespace around `Invocation`
2021-12-06 21:17:38 +01:00
YR Chen 4317c5334f Adopt concurrency adoption guidelines (#230)
Adopt Swift Concurrency adoption guidelines for Swift Server Libraries (swift-server/guides#70).

- Use #if compiler(>=5.5) && canImport(_Concurrency) to judge if Concurrency is available
- Some clean up
2021-12-06 20:52:35 +01:00
Fabian Fett 011e8ddc4b Rename Lambda.Context to LambdaContext (#233) 2021-09-30 01:56:58 +02:00
Fabian Fett afab5102a1 Rename Lambda.Lifecycle to LambdaRuntime (#232) 2021-09-29 09:10:30 +02:00
Fabian Fett 7c1dea035d Remove dependency on _NIOConcurrency (#229) 2021-09-23 19:44:08 +02:00
Fabian Fett e5aa488a26 [RFC] Drop event label from handle methods in LambdaHandlers (#225)
* Drop event label from handle method in LambdaHandlers
* Rename `In` to `Event` and `Out` to `Output` in `EventLoopLambdaHandler`
2021-09-22 16:59:07 +02:00
Fabian Fett dbd675df6f Reorder Event and Context. (#224)
motivation: follow API design guidelines

changes: Reorder `event` and `context` n public APIs
2021-08-24 15:26:39 -07:00
Fabian Fett 454fe2e037 Drop sync and closure APIs (#222)
motivation: with async/await, no need in closure based APIs

changes:
* Drop closure APIs
* Rename AsyncLambdaHandler -> LambdaHandler
* Removed unnecassary public acls from tests
2021-08-24 14:20:13 -07:00
Fabian Fett 782c0f38ac InitializationContext should be a value not reference type (#219) 2021-08-19 15:57:23 +02:00
Fabian Fett f01a9b607b Use explicit NIO imports (#220)
- Use explicit NIO modules `NIOCore`, `NIOPosix` and `NIOEmbedded` introduced with SwiftNIO 2.32.0
2021-08-19 09:42:23 +02:00
Joel Saltzman 245674cd6b Renamed completeWithAsync to completeWithTask (#221)
* Renamed EventLoopPromise's `completeWithAsync` to `completeWithTask` (based on the SwiftNIO renaming)
* Updated swift-nio dependency to 2.32.0
2021-08-19 09:23:53 +02:00
Cory Benfield 48dc507d26 Clean up dependency graph and imports (#218)
- Correctly express the dependency on NIO in Package.swift
- Correctly import _NIOConcurrency in files where it's used
2021-08-13 10:50:18 +02:00
Juan A. Reyes 774bbf1ae7 Change Lambda.Context from class to struct (#217)
Motivation: Lambda.Context is currently a class that only holds values. For this reason it should be a value type itself.

Changes: Lambda.Context is now a struct, that is backed by a CoW storage class, to ensure performance remains high.
2021-08-11 08:59:51 +02: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
Fabian Fett 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
Fabian Fett 19a9f92538 Inline default LambdaHandler implementations (#201)
Add `@inlinable` to default LambdaHandler implementations. This ensures that those implementations can be inlined into user code and can be specialized for their input and output types.
2021-04-14 22:59:42 +02: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
Filipp Fediakov 0afbf42226 Fix typo in LambdaRunner.swift documentation (#171) 2020-09-25 18:12:09 +02:00
tomer doron f3c68d66c4 better error message when invoking local lambda server with incorrect http method (#157)
motivation: nicer error messages

changes: return 405 when using wrong http method on the lambda invocatoin endpoint (local debug server)
2020-08-06 14:39:32 -07:00
tomer doron 9ebc9dda20 give better error when lambda fails to start (#155)
motivation: in error cases where lamnda fails to start the process fails silently and exists with code 0

changes: change the blocking API to check on the result and fatalError if lambda cannot be started / run
2020-08-06 14:08:25 -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
tomer doron 2bac89639f add syncShutdown to LambdaHandler (#132)
motivation: make shutdown easier to use

changes:
* override shutdown to use the offloadQueue to perform syncShutdown
* add empty syncShutdown that can be implmented by the concrete Lambda function
2020-06-18 12:48:13 -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 3b29513652 handle lambda errors correctly in mock server (#104)
motivation: better mock server

changes:
* add handler for :requestID/error
* return .accepted to lambda
* return .internalServerError to client + error json
* small refactoring of response code in mock server to make it DRYer
2020-06-01 13:15:30 -07:00
tomer doron e2ac820289 add an option to start the local debugging server based on an env variable (#87)
motivation: make using the local debugging server easier to turn off/on without the need to change code when oyu are preparing to deploy

changes:
* add code to lambda so that in debug mode only, if the LOCAL_LAMBDA_SERVER_ENABLED env variable is set the local debugging server is started
* make withLocalServer internal
* update example code
2020-06-01 12:38:06 -07:00