Skip to content

Commit 48525aa

Browse files
committed
1 parent 1c7788b commit 48525aa

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Diff for: Sources/TencentSCFRuntime/SCF+Codable.swift

+2
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ internal struct CodableVoidClosureWrapper<In: Decodable>: SCFHandler {
9292

9393
/// Implementation of a`ByteBuffer` to `In` decoding.
9494
extension EventLoopSCFHandler where In: Decodable {
95+
@inlinable
9596
public func decode(buffer: ByteBuffer) throws -> In {
9697
try self.decoder.decode(In.self, from: buffer)
9798
}
9899
}
99100

100101
/// Implementation of `Out` to `ByteBuffer` encoding.
101102
extension EventLoopSCFHandler where Out: Encodable {
103+
@inlinable
102104
public func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
103105
try self.encoder.encode(value, using: allocator)
104106
}

Diff for: Sources/TencentSCFRuntimeCore/SCF+String.swift

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ internal struct StringVoidClosureWrapper: SCFHandler {
100100

101101
extension EventLoopSCFHandler where In == String {
102102
/// Implementation of a `ByteBuffer` to `String` decoding.
103+
@inlinable
103104
public func decode(buffer: ByteBuffer) throws -> String {
104105
var buffer = buffer
105106
guard let string = buffer.readString(length: buffer.readableBytes) else {
@@ -111,6 +112,7 @@ extension EventLoopSCFHandler where In == String {
111112

112113
extension EventLoopSCFHandler where Out == String {
113114
/// Implementation of `String` to `ByteBuffer` encoding.
115+
@inlinable
114116
public func encode(allocator: ByteBufferAllocator, value: String) throws -> ByteBuffer? {
115117
// FIXME: reusable buffer
116118
var buffer = allocator.buffer(capacity: value.utf8.count)

Diff for: Sources/TencentSCFRuntimeCore/SCFHandler.swift

+16-11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public protocol SCFHandler: EventLoopSCFHandler {
5252
}
5353

5454
extension SCF {
55+
@usableFromInline
5556
internal static let defaultOffloadQueue = DispatchQueue(label: "SCFHandler.offload")
5657
}
5758

@@ -64,6 +65,7 @@ extension SCFHandler {
6465
/// `SCFHandler` is offloading the processing to a `DispatchQueue`.
6566
/// This is slower but safer, in case the implementation blocks the `EventLoop`.
6667
/// Performance sensitive cloud functions should be based on `EventLoopSCFHandler` which does not offload.
68+
@inlinable
6769
public func handle(context: SCF.Context, event: In) -> EventLoopFuture<Out> {
6870
let promise = context.eventLoop.makePromise(of: Out.self)
6971
// FIXME: reusable DispatchQueue
@@ -140,18 +142,19 @@ public protocol EventLoopSCFHandler: ByteBufferSCFHandler {
140142

141143
extension EventLoopSCFHandler {
142144
/// Driver for `ByteBuffer` -> `In` decoding and `Out` -> `ByteBuffer` encoding
145+
@inlinable
143146
public func handle(context: SCF.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?> {
144-
switch self.decodeIn(buffer: event) {
145-
case .failure(let error):
147+
let input: In
148+
do { input = try self.decode(buffer: event) }
149+
catch {
146150
return context.eventLoop.makeFailedFuture(CodecError.requestDecoding(error))
147-
case .success(let `in`):
148-
return self.handle(context: context, event: `in`).flatMapThrowing { out in
149-
switch self.encodeOut(allocator: context.allocator, value: out) {
150-
case .failure(let error):
151-
throw CodecError.responseEncoding(error)
152-
case .success(let buffer):
153-
return buffer
154-
}
151+
}
152+
153+
return self.handle(context: context, event: input).flatMapThrowing { output in
154+
do {
155+
return try self.encode(allocator: context.allocator, value: output)
156+
} catch {
157+
throw CodecError.responseEncoding(error)
155158
}
156159
}
157160
}
@@ -175,6 +178,7 @@ extension EventLoopSCFHandler {
175178

176179
/// Implementation of `ByteBuffer` to `Void` decoding.
177180
extension EventLoopSCFHandler where Out == Void {
181+
@inlinable
178182
public func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
179183
nil
180184
}
@@ -212,7 +216,8 @@ extension ByteBufferSCFHandler {
212216
}
213217
}
214218

215-
private enum CodecError: Error {
219+
@usableFromInline
220+
enum CodecError: Error {
216221
case requestDecoding(Error)
217222
case responseEncoding(Error)
218223
}

0 commit comments

Comments
 (0)