@@ -20,6 +20,9 @@ Versions:
20
20
` LambdaWithBackgroundProcessingHandler ` .
21
21
- Update ` LambdaCodableAdapter ` to now be generic over any handler conforming to
22
22
` LambdaWithBackgroundProcessingHandler ` instead of ` LambdaHandler ` .
23
+ - v1.2:
24
+ - Remove ` ~Copyable ` from ` LambdaResponseStreamWriter ` and ` LambdaResponseWriter ` . Instead throw an error when
25
+ ` finish() ` is called multiple times or when ` write ` /` writeAndFinish ` is called after ` finish() ` .
23
26
24
27
## Motivation
25
28
@@ -144,8 +147,8 @@ of the `EventLoop` family of interfaces.
144
147
- This allows the lifecycle of the ` LambdaRuntime ` to be managed with ` swift-service-lifecycle ` _ alongside_ and in the
145
148
same way the lifecycles of the required services are managed, e.g.
146
149
` try await ServiceGroup(services: [postgresClient, ..., lambdaRuntime], ...).run() ` .
147
- - Dependencies can now be injected into ` LambdaRuntime ` — ` swift-service-lifecycle ` guarantees that the services will be
148
- initialized _ before _ the ` LambdaRuntime ` 's ` run() ` function is called .
150
+ - Dependencies can now be injected into ` LambdaRuntime ` . With ` swift-service-lifecycle ` , services will be initialized
151
+ together with ` LambdaRuntime ` .
149
152
- The required services can then be used within the handler in a structured concurrency manner.
150
153
` swift-service-lifecycle ` takes care of listening for termination signals and terminating the services as well as the
151
154
` LambdaRuntime ` in correct order.
@@ -196,22 +199,20 @@ below), which is the new base protocol for the `LambdaRuntime` (defined below as
196
199
197
200
``` swift
198
201
/// A writer object to write the Lambda response stream into
199
- public protocol LambdaResponseStreamWriter : ~ Copyable {
202
+ public protocol LambdaResponseStreamWriter {
200
203
/// Write a response part into the stream. The HTTP response is started lazily before the first call to `write(_:)`.
201
204
/// Bytes written to the writer are streamed continually.
202
205
func write (_ buffer : ByteBuffer) async throws
203
206
/// End the response stream and the underlying HTTP response.
204
- consuming func finish ()
207
+ func finish () async throws
205
208
/// Write a response part into the stream and end the response stream as well as the underlying HTTP response.
206
- consuming func writeAndFinish (_ buffer : ByteBuffer) async throws
209
+ func writeAndFinish (_ buffer : ByteBuffer) async throws
207
210
}
208
211
```
209
212
210
- It is important to note that ` LambdaResponseStreamWriter ` will be non-copyable, with the ` finish() ` and
211
- ` writeAndFinish(_:) ` functions having the ` consuming ` ownership keyword. This is so that the compiler can enforce the
212
- restriction of not being able to call ` write(_:) ` after any of the finishing functions have been called. The compiler
213
- can also prevent the finishing functions from being called multiple times. If the user does not call ` finish() ` , the
214
- library will automatically finish the stream after the last ` write ` utilizing the writers ` deinit ` .
213
+ If the user does not call ` finish() ` , the library will automatically finish the stream after the last ` write ` .
214
+ Appropriate errors will be thrown if ` finish() ` is called multiple times, or if ` write ` /` writeAndFinish ` is called after
215
+ ` finish() ` .
215
216
216
217
### LambdaContext
217
218
@@ -278,7 +279,7 @@ public protocol StreamingLambdaHandler {
278
279
/// headers will be sent.
279
280
/// - If ``LambdaResponseStreamWriter.finish()`` has already been called before the error is thrown, the
280
281
/// error will be logged.
281
- mutating func handle (_ event : ByteBuffer, responseWriter : consuming some LambdaResponseStreamWriter, context : LambdaContext) async throws
282
+ mutating func handle (_ event : ByteBuffer, responseWriter : some LambdaResponseStreamWriter, context : LambdaContext) async throws
282
283
}
283
284
```
284
285
@@ -303,7 +304,7 @@ An implementation that sends the number 1 to 10 every 500ms could look like this
303
304
struct SendNumbersWithPause : StreamingLambdaHandler {
304
305
func handle (
305
306
_ event : ByteBuffer,
306
- responseWriter : consuming some LambdaResponseStreamWriter,
307
+ responseWriter : some LambdaResponseStreamWriter,
307
308
context : LambdaContext
308
309
) async throws {
309
310
for i in 1 ... 10 {
@@ -357,12 +358,13 @@ any background work after the result has been sent to the AWS Lambda control pla
357
358
simply serves as a mechanism to return the output without explicitly returning from the ` handle ` function.
358
359
359
360
``` swift
360
- public protocol LambdaResponseWriter <Output>: ~ Copyable {
361
+ public protocol LambdaResponseWriter <Output> {
361
362
associatedtype Output
362
363
363
364
/// Sends the generic Output object (representing the computed result of the handler)
364
365
/// to the AWS Lambda response endpoint.
365
- consuming func write (_ : Output) async throws
366
+ /// An error will be thrown if this function is called more than once.
367
+ func write (_ : Output) async throws
366
368
}
367
369
368
370
public protocol LambdaWithBackgroundProcessingHandler {
@@ -377,7 +379,7 @@ public protocol LambdaWithBackgroundProcessingHandler {
377
379
/// Agnostic to JSON encoding/decoding
378
380
func handle (
379
381
_ event : Event,
380
- outputWriter : consuming some LambdaResponseWriter<Output>,
382
+ outputWriter : some LambdaResponseWriter<Output>,
381
383
context : LambdaContext
382
384
) async throws
383
385
}
@@ -400,7 +402,7 @@ struct BackgroundProcessingHandler: LambdaWithBackgroundProcessingHandler {
400
402
401
403
func handle (
402
404
_ event : Event,
403
- outputWriter : consuming some LambdaResponseWriter<Output>,
405
+ outputWriter : some LambdaResponseWriter<Output>,
404
406
context : LambdaContext
405
407
) async throws {
406
408
// Return result to the Lambda control plane
@@ -460,13 +462,13 @@ public final class LambdaRuntime<Handler>: ServiceLifecycle.Service, Sendable
460
462
/// ``NIOSingletons.posixEventLoopGroup``.
461
463
/// - Parameter logger: A logger
462
464
public init (
463
- handler : consuming sending Handler,
465
+ handler : sending Handler,
464
466
eventLoop : EventLoop = Lambda.defaultEventLoop ,
465
467
logger : Logger = Logger (label : " Lambda" )
466
468
)
467
469
468
470
/// Create a LambdaRuntime by passing a ``StreamingLambdaHandler``.
469
- public convenience init (handler : consuming sending Handler)
471
+ public convenience init (handler : sending Handler)
470
472
471
473
/// Starts the LambdaRuntime by connecting to the Lambda control plane to ask
472
474
/// for events to process. If the environment variable AWS_LAMBDA_RUNTIME_API is
@@ -569,7 +571,7 @@ public struct LambdaHandlerAdapter<
569
571
570
572
/// 1. Call the `self.handler.handle(...)` with `event` and `context`.
571
573
/// 2. Pass the generic `Output` object returned from `self.handler.handle(...)` to `outputWriter.write(_:)`
572
- public func handle (_ event : Event, outputWriter : consuming some LambdaResponseWriter<Output>, context : LambdaContext) async throws
574
+ public func handle (_ event : Event, outputWriter : some LambdaResponseWriter<Output>, context : LambdaContext) async throws
573
575
}
574
576
```
575
577
@@ -643,7 +645,7 @@ public struct LambdaCodableAdapter<
643
645
/// and the `LambdaContext`.
644
646
public mutating func handle (
645
647
_ request : ByteBuffer,
646
- responseWriter : consuming some LambdaResponseStreamWriter,
648
+ responseWriter : some LambdaResponseStreamWriter,
647
649
context : LambdaContext
648
650
) async throws
649
651
}
@@ -721,19 +723,19 @@ handle encoding/decoding themselves:
721
723
public struct StreamingClosureHandler : StreamingLambdaHandler {
722
724
723
725
public init (
724
- body : @escaping sending (ByteBuffer, consuming LambdaResponseStreamWriter, LambdaContext) async throws -> ()
726
+ body : @escaping sending (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> ()
725
727
)
726
728
727
729
public func handle (
728
730
_ request : ByteBuffer,
729
- responseWriter : consuming LambdaResponseStreamWriter,
731
+ responseWriter : LambdaResponseStreamWriter,
730
732
context : LambdaContext
731
733
) async throws
732
734
}
733
735
734
736
extension LambdaRuntime {
735
737
public init (
736
- body : @escaping sending (ByteBuffer, consuming LambdaResponseStreamWriter, LambdaContext) async throws -> ()
738
+ body : @escaping sending (ByteBuffer, LambdaResponseStreamWriter, LambdaContext) async throws -> ()
737
739
)
738
740
}
739
741
```
@@ -766,7 +768,7 @@ Its API would look like this:
766
768
/// The response can be empty, a single ByteBuffer or a response stream.
767
769
public struct LambdaResponse {
768
770
/// A writer to be used when creating a streamed response.
769
- public struct Writer : ~ Copyable {
771
+ public struct Writer {
770
772
/// Writes data to the response stream
771
773
public func write (_ byteBuffer : ByteBuffer) async throws
772
774
/// Closes off the response stream
@@ -840,7 +842,7 @@ For handlers conforming to the `LambdaHandler` protocol, we considered extending
840
842
add background work can override this function in their ` LambdaHandler ` conforming object.
841
843
842
844
``` swift
843
- public protocol LambdaHandler : ~ Copyable {
845
+ public protocol LambdaHandler {
844
846
associatedtype Event
845
847
associatedtype Output
846
848
0 commit comments