Skip to content

Commit a3ced8c

Browse files
normanmaurertomerd
authored andcommitted
Refactor code to be more robust and more swift-like. (#4)
- Ensure we fail all promises when the Channel is closed or we receive an error to prevent any promise leaks. - Replace class by enum to ensure people not init it by mistake - Use struct where class is not needed.
1 parent e78abdc commit a3ced8c

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

Sources/SwiftAwsLambda/HttpClient.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,26 @@ private class UnaryHTTPHandler: ChannelInboundHandler, ChannelOutboundHandler {
240240
}
241241

242242
func errorCaught(ctx: ChannelHandlerContext, error: Error) {
243-
let promise = buffer.removeFirst()
244-
promise.fail(error: error)
243+
// In HTTP we should fail all promises as we close the Channel.
244+
self.failAllPromises(error: error)
245245
ctx.close(promise: nil)
246246
}
247+
248+
func channelInactive(ctx: ChannelHandlerContext) {
249+
// Fail all promises
250+
self.failAllPromises(error: HTTPClientError.connectionClosed)
251+
ctx.fireChannelInactive()
252+
}
253+
254+
private func failAllPromises(error: Error) {
255+
while let promise = buffer.first {
256+
promise.fail(error: error)
257+
buffer.removeFirst()
258+
}
259+
}
247260
}
248261

249262
private enum HTTPClientError: Error {
250263
case invalidRequest
264+
case connectionClosed
251265
}

Sources/SwiftAwsLambda/Lambda+Codable.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
import Foundation
1616

1717
extension Lambda {
18-
public class func run<In: Decodable, Out: Encodable>(_ closure: @escaping LambdaCodableClosure<In, Out>) -> LambdaLifecycleResult {
18+
public static func run<In: Decodable, Out: Encodable>(_ closure: @escaping LambdaCodableClosure<In, Out>) -> LambdaLifecycleResult {
1919
return run(LambdaClosureWrapper(closure))
2020
}
2121

22-
public class func run<T>(_ handler: T) -> LambdaLifecycleResult where T: LambdaCodableHandler {
22+
public static func run<T>(_ handler: T) -> LambdaLifecycleResult where T: LambdaCodableHandler {
2323
return run(handler as LambdaHandler)
2424
}
2525

2626
// for testing
27-
internal class func run<In: Decodable, Out: Encodable>(closure: @escaping LambdaCodableClosure<In, Out>, maxTimes: Int) -> LambdaLifecycleResult {
27+
internal static func run<In: Decodable, Out: Encodable>(closure: @escaping LambdaCodableClosure<In, Out>, maxTimes: Int) -> LambdaLifecycleResult {
2828
return run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
2929
}
3030

3131
// for testing
32-
internal class func run<T>(handler: T, maxTimes: Int) -> LambdaLifecycleResult where T: LambdaCodableHandler {
32+
internal static func run<T>(handler: T, maxTimes: Int) -> LambdaLifecycleResult where T: LambdaCodableHandler {
3333
return run(handler: handler as LambdaHandler, maxTimes: maxTimes)
3434
}
3535
}
@@ -80,6 +80,7 @@ public extension LambdaCodableHandler {
8080
}
8181
}
8282

83+
// This is a class as encoder amd decoder are a class, which means its cheaper to hold a reference to both in a class then a struct.
8384
private class LambdaCodableJsonCodec<In: Decodable, Out: Encodable>: LambdaCodableCodec<In, Out> {
8485
private let encoder = JSONEncoder()
8586
private let decoder = JSONDecoder()
@@ -92,7 +93,7 @@ private class LambdaCodableJsonCodec<In: Decodable, Out: Encodable>: LambdaCodab
9293
}
9394
}
9495

95-
private class LambdaClosureWrapper<In: Decodable, Out: Encodable>: LambdaCodableHandler {
96+
private struct LambdaClosureWrapper<In: Decodable, Out: Encodable>: LambdaCodableHandler {
9697
typealias Codec = LambdaCodableJsonCodec<In, Out>
9798

9899
private let closure: LambdaCodableClosure<In, Out>

Sources/SwiftAwsLambda/Lambda+String.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
extension Lambda {
16-
public class func run(_ closure: @escaping LambdaStringClosure) -> LambdaLifecycleResult {
16+
public static func run(_ closure: @escaping LambdaStringClosure) -> LambdaLifecycleResult {
1717
return run(LambdaClosureWrapper(closure))
1818
}
1919

20-
public class func run(_ handler: LambdaStringHandler) -> LambdaLifecycleResult {
20+
public static func run(_ handler: LambdaStringHandler) -> LambdaLifecycleResult {
2121
return run(handler as LambdaHandler)
2222
}
2323

2424
// for testing
25-
internal class func run(closure: @escaping LambdaStringClosure, maxTimes: Int) -> LambdaLifecycleResult {
25+
internal static func run(closure: @escaping LambdaStringClosure, maxTimes: Int) -> LambdaLifecycleResult {
2626
return run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
2727
}
2828

2929
// for testing
30-
internal class func run(handler: LambdaStringHandler, maxTimes: Int) -> LambdaLifecycleResult {
30+
internal static func run(handler: LambdaStringHandler, maxTimes: Int) -> LambdaLifecycleResult {
3131
return run(handler: handler as LambdaHandler, maxTimes: maxTimes)
3232
}
3333
}
@@ -58,7 +58,7 @@ public extension LambdaStringHandler {
5858
}
5959
}
6060

61-
private class LambdaClosureWrapper: LambdaStringHandler {
61+
private struct LambdaClosureWrapper: LambdaStringHandler {
6262
private let closure: LambdaStringClosure
6363
init(_ closure: @escaping LambdaStringClosure) {
6464
self.closure = closure

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414

1515
import NIO
1616

17-
public final class Lambda {
18-
public class func run(_ closure: @escaping LambdaClosure) -> LambdaLifecycleResult {
17+
public enum Lambda {
18+
public static func run(_ closure: @escaping LambdaClosure) -> LambdaLifecycleResult {
1919
return run(LambdaClosureWrapper(closure))
2020
}
2121

22-
public class func run(_ handler: LambdaHandler) -> LambdaLifecycleResult {
22+
public static func run(_ handler: LambdaHandler) -> LambdaLifecycleResult {
2323
return Lifecycle(handler: handler).start()
2424
}
2525

2626
// for testing
27-
internal class func run(closure: @escaping LambdaClosure, maxTimes: Int) -> LambdaLifecycleResult {
27+
internal static func run(closure: @escaping LambdaClosure, maxTimes: Int) -> LambdaLifecycleResult {
2828
return run(handler: LambdaClosureWrapper(closure), maxTimes: maxTimes)
2929
}
3030

3131
// for testing
32-
internal class func run(handler: LambdaHandler, maxTimes: Int) -> LambdaLifecycleResult {
32+
internal static func run(handler: LambdaHandler, maxTimes: Int) -> LambdaLifecycleResult {
3333
return Lifecycle(handler: handler, maxTimes: maxTimes).start()
3434
}
3535

@@ -38,7 +38,7 @@ public final class Lambda {
3838
private let max: Int
3939
private var counter: Int = 0
4040

41-
public init(handler: LambdaHandler, maxTimes: Int = 0) {
41+
init(handler: LambdaHandler, maxTimes: Int = 0) {
4242
self.handler = handler
4343
max = maxTimes
4444
assert(max >= 0)
@@ -87,7 +87,7 @@ public struct LambdaContext {
8787
public let deadlineNs: String?
8888
}
8989

90-
private class LambdaClosureWrapper: LambdaHandler {
90+
private struct LambdaClosureWrapper: LambdaHandler {
9191
private let closure: LambdaClosure
9292
init(_ closure: @escaping LambdaClosure) {
9393
self.closure = closure

0 commit comments

Comments
 (0)