Skip to content

Commit f21f4a3

Browse files
committed
allocator
1 parent 38d7088 commit f21f4a3

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

Sources/AWSLambdaRuntimeCore/LambdaContext.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ extension Lambda {
3333
/// Most importantly the `EventLoop` must never be blocked.
3434
public let eventLoop: EventLoop
3535

36-
internal init(logger: Logger, eventLoop: EventLoop) {
36+
/// `ByteBufferAllocator` to allocate `ByteBuffer`
37+
public let allocator: ByteBufferAllocator
38+
39+
internal init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator) {
3740
self.eventLoop = eventLoop
3841
self.logger = logger
42+
self.allocator = allocator
3943
}
4044
}
4145
}
@@ -87,7 +91,8 @@ extension Lambda {
8791
cognitoIdentity: String? = nil,
8892
clientContext: String? = nil,
8993
logger: Logger,
90-
eventLoop: EventLoop) {
94+
eventLoop: EventLoop,
95+
allocator: ByteBufferAllocator) {
9196
self.requestID = requestID
9297
self.traceID = traceID
9398
self.invokedFunctionARN = invokedFunctionARN
@@ -96,7 +101,7 @@ extension Lambda {
96101
self.deadline = deadline
97102
// utility
98103
self.eventLoop = eventLoop
99-
self.allocator = ByteBufferAllocator()
104+
self.allocator = allocator
100105
// mutate logger with context
101106
var logger = logger
102107
logger[metadataKey: "awsRequestID"] = .string(requestID)

Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ extension Lambda {
2121
internal final class Runner {
2222
private let runtimeClient: RuntimeClient
2323
private let eventLoop: EventLoop
24+
private let allocator: ByteBufferAllocator
2425

2526
private var isGettingNextInvocation = false
2627

2728
init(eventLoop: EventLoop, configuration: Configuration) {
2829
self.eventLoop = eventLoop
2930
self.runtimeClient = RuntimeClient(eventLoop: self.eventLoop, configuration: configuration.runtimeEngine)
31+
self.allocator = ByteBufferAllocator()
3032
}
3133

3234
/// Run the user provided initializer. This *must* only be called once.
@@ -36,7 +38,9 @@ extension Lambda {
3638
logger.debug("initializing lambda")
3739
// 1. create the handler from the factory
3840
// 2. report initialization error if one occured
39-
let context = InitializationContext(logger: logger, eventLoop: self.eventLoop)
41+
let context = InitializationContext(logger: logger,
42+
eventLoop: self.eventLoop,
43+
allocator: self.allocator)
4044
return factory(context)
4145
// Hopping back to "our" EventLoop is importnant in case the factory returns a future
4246
// that originated from a foreign EventLoop/EventLoopGroup.
@@ -61,7 +65,10 @@ extension Lambda {
6165
}.flatMap { invocation, event in
6266
// 2. send invocation to handler
6367
self.isGettingNextInvocation = false
64-
let context = Context(logger: logger, eventLoop: self.eventLoop, invocation: invocation)
68+
let context = Context(logger: logger,
69+
eventLoop: self.eventLoop,
70+
allocator: self.allocator,
71+
invocation: invocation)
6572
logger.debug("sending invocation to lambda handler \(handler)")
6673
return handler.handle(context: context, event: event)
6774
// Hopping back to "our" EventLoop is importnant in case the handler returns a future that
@@ -94,15 +101,16 @@ extension Lambda {
94101
}
95102

96103
private extension Lambda.Context {
97-
convenience init(logger: Logger, eventLoop: EventLoop, invocation: Lambda.Invocation) {
104+
convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) {
98105
self.init(requestID: invocation.requestID,
99106
traceID: invocation.traceID,
100107
invokedFunctionARN: invocation.invokedFunctionARN,
101108
deadline: DispatchWallTime(millisSinceEpoch: invocation.deadlineInMillisSinceEpoch),
102109
cognitoIdentity: invocation.cognitoIdentity,
103110
clientContext: invocation.clientContext,
104111
logger: logger,
105-
eventLoop: eventLoop)
112+
eventLoop: eventLoop,
113+
allocator: allocator)
106114
}
107115
}
108116

Sources/AWSLambdaTesting/Lambda+Testing.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ extension Lambda {
102102
invokedFunctionARN: config.invokedFunctionARN,
103103
deadline: .now() + config.timeout,
104104
logger: logger,
105-
eventLoop: eventLoop)
105+
eventLoop: eventLoop,
106+
allocator: ByteBufferAllocator())
106107

107108
return try eventLoop.flatSubmit {
108109
handler.handle(context: context, event: event)

Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ class LambdaTest: XCTestCase {
263263
cognitoIdentity: nil,
264264
clientContext: nil,
265265
logger: Logger(label: "test"),
266-
eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next())
266+
eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(),
267+
allocator: ByteBufferAllocator())
267268
XCTAssertGreaterThan(context.deadline, .now())
268269

269270
let expiredContext = Lambda.Context(requestID: context.requestID,
@@ -273,7 +274,8 @@ class LambdaTest: XCTestCase {
273274
cognitoIdentity: context.cognitoIdentity,
274275
clientContext: context.clientContext,
275276
logger: context.logger,
276-
eventLoop: context.eventLoop)
277+
eventLoop: context.eventLoop,
278+
allocator: context.allocator)
277279
XCTAssertLessThan(expiredContext.deadline, .now())
278280
}
279281

@@ -285,7 +287,8 @@ class LambdaTest: XCTestCase {
285287
cognitoIdentity: nil,
286288
clientContext: nil,
287289
logger: Logger(label: "test"),
288-
eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next())
290+
eventLoop: MultiThreadedEventLoopGroup(numberOfThreads: 1).next(),
291+
allocator: ByteBufferAllocator())
289292
XCTAssertLessThanOrEqual(context.getRemainingTime(), .seconds(1))
290293
XCTAssertGreaterThan(context.getRemainingTime(), .milliseconds(800))
291294
}

Tests/AWSLambdaRuntimeTests/Lambda+CodeableTest.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class CodableLambdaTest: XCTestCase {
7272
cognitoIdentity: nil,
7373
clientContext: nil,
7474
logger: Logger(label: "test"),
75-
eventLoop: self.eventLoopGroup.next())
75+
eventLoop: self.eventLoopGroup.next(),
76+
allocator: ByteBufferAllocator())
7677
}
7778
}
7879

0 commit comments

Comments
 (0)