Skip to content

Commit 3d3c303

Browse files
committed
remove unecessary sendable conformance
1 parent 9887530 commit 3d3c303

File tree

8 files changed

+167
-190
lines changed

8 files changed

+167
-190
lines changed

Sources/OpenAPILambda.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import OpenAPIRuntime
1919
import HTTPTypes
2020

2121
/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime)
22-
public protocol OpenAPILambda: Sendable {
22+
public protocol OpenAPILambda {
2323

2424
associatedtype Event: Decodable
2525
associatedtype Output: Encodable
@@ -43,7 +43,7 @@ public protocol OpenAPILambda: Sendable {
4343
extension OpenAPILambda {
4444
/// Returns the Lambda handler function for this OpenAPI Lambda implementation.
4545
/// - Returns: A handler function that can be used with AWS Lambda Runtime
46-
public static func handler() throws -> @Sendable (Event, LambdaContext) async throws -> Output {
46+
public static func handler() throws -> (Event, LambdaContext) async throws -> Output {
4747
try OpenAPILambdaHandler<Self>().handler
4848
}
4949

Sources/OpenAPILambdaHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import OpenAPIRuntime
1818
import HTTPTypes
1919

2020
/// Specialization of LambdaHandler which runs an OpenAPILambda
21-
struct OpenAPILambdaHandler<L: OpenAPILambda>: Sendable {
21+
struct OpenAPILambdaHandler<L: OpenAPILambda> {
2222

2323
private let router: OpenAPILambdaRouter
2424
private let transport: OpenAPILambdaTransport
@@ -59,7 +59,7 @@ struct OpenAPILambdaHandler<L: OpenAPILambda>: Sendable {
5959
let request = try lambda.request(context: context, from: event)
6060

6161
// route the request to find the handlers and extract the paramaters
62-
let (handler, parameters) = try await router.route(method: request.0.method, path: request.0.path!)
62+
let (handler, parameters) = try router.route(method: request.0.method, path: request.0.path!)
6363

6464
// call the request handler (and extract the HTTPRequest and HTTPBody)
6565
let httpRequest = request.0

Sources/OpenAPILambdaTransport.swift

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public typealias OpenAPILambdaResponse = (HTTPResponse, String?)
3030
public typealias OpenAPILambdaRequestParameters = [String: Substring]
3131

3232
/// an OpenAPI handler
33-
public typealias OpenAPIHandler = @Sendable (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> (
33+
public typealias OpenAPIHandler = (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> (
3434
HTTPResponse, HTTPBody?
3535
)
3636

3737
/// Lambda Transport for OpenAPI generator
38-
public struct OpenAPILambdaTransport: ServerTransport, Sendable {
38+
public struct OpenAPILambdaTransport: ServerTransport {
3939

4040
private let router: OpenAPILambdaRouter
4141

@@ -50,29 +50,6 @@ public struct OpenAPILambdaTransport: ServerTransport, Sendable {
5050
/// - queryItemNames: The names of query items to be extracted
5151
/// from the request URL that matches the provided HTTP operation.
5252
public func register(_ handler: @escaping OpenAPIHandler, method: HTTPRequest.Method, path: String) throws {
53-
54-
// call the actor-isolated `router.add()` function from a synchronous context
55-
// because ServerTransport.resgister() is synchronous
56-
let semaphore = DispatchSemaphore(value: 0)
57-
let errorBox = Box<Error?>(nil)
58-
59-
Task { @Sendable in
60-
do {
61-
try await self.router.add(method: method, path: path, handler: handler)
62-
}
63-
catch {
64-
errorBox.value = error
65-
}
66-
semaphore.signal()
67-
}
68-
semaphore.wait()
69-
if let error = errorBox.value {
70-
throw error
71-
}
72-
}
73-
74-
private final class Box<T>: @unchecked Sendable {
75-
var value: T
76-
init(_ value: T) { self.value = value }
53+
try self.router.add(method: method, path: path, handler: handler)
7754
}
7855
}

Sources/Router/OpenAPILambdaRouter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public enum OpenAPILambdaRouterError: Error {
2424
}
2525

2626
/// A router API
27-
protocol OpenAPILambdaRouter: Sendable {
27+
protocol OpenAPILambdaRouter {
2828
/// add a route for a given HTTP method and path and associate a handler
29-
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) async throws
29+
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws
3030

3131
/// Retrieve the handler and path parameter for a given HTTP method and path
32-
func route(method: HTTPRequest.Method, path: String) async throws -> (
32+
func route(method: HTTPRequest.Method, path: String) throws -> (
3333
OpenAPIHandler, OpenAPILambdaRequestParameters
3434
)
3535
}

Sources/Router/OpenAPILambdaRouterNode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import HTTPTypes
2222
/// - a path element (represented as a `String`),
2323
/// - a path parameter (represented as a `String`)
2424
/// - a handler, only for leaf nodes (represented as `OpenAPIHandler`)
25-
actor Node {
25+
final class Node {
2626
let value: NodeValue
2727
var children: [String: Node] = [:]
2828

@@ -62,8 +62,8 @@ actor Node {
6262
/// - Throws:
6363
/// - URIPathCollectionError.canNotAddChildToHandlerNode when trying to add a child to leaf node of type `.handler`
6464
/// - URIPathCollectionError.canNotHaveMultipleParamChilds when trying to add multiple child node of type `.parameter`
65-
6665
func add(pathElement: String) throws -> Node { try add(child: NodeValue.pathElement(pathElement)) }
66+
6767
/// Convenience method to add a child node of type path parameter to this node
6868
/// - Parameter:
6969
/// - pathParameter: the name of a path parameter. A path parameter is a `{name}` usually found between `/` characters in the URI
@@ -72,8 +72,8 @@ actor Node {
7272
/// - Throws:
7373
/// - URIPathCollectionError.canNotAddChildToHandlerNode when trying to add a child to leaf node of type `.handler`
7474
/// - URIPathCollectionError.canNotHaveMultipleParamChilds when trying to add multiple child node of type `.parameter`
75-
7675
func add(parameter: String) throws -> Node { try add(child: NodeValue.pathParameter(parameter)) }
76+
7777
/// Convenience method to add a child node of type handler to this node
7878
/// - Parameter:
7979
/// - handler: a function handler. A handler MUST be a leaf node (it has no children) and is of type `OpenAPIHandler`

Sources/Router/OpenAPILambdaRouterTrie.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ struct TrieRouter: OpenAPILambdaRouter {
1919
private let uriPath: URIPathCollection = URIPath()
2020

2121
/// add a route for a given HTTP method and path and associate a handler
22-
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) async throws {
23-
try await uriPath.add(method: method, path: path, handler: handler)
22+
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws {
23+
try uriPath.add(method: method, path: path, handler: handler)
2424
}
2525

2626
/// Retrieve the handler and path parameter for a given HTTP method and path
27-
func route(method: HTTPRequest.Method, path: String) async throws -> (
27+
func route(method: HTTPRequest.Method, path: String) throws -> (
2828
OpenAPIHandler, OpenAPILambdaRequestParameters
29-
) { try await uriPath.find(method: method, path: path) }
29+
) { try uriPath.find(method: method, path: path) }
3030
}
3131

3232
enum URIPathCollectionError: Error {
3333
case canNotAddChildToHandlerNode
3434
case canNotHaveMultipleParamChilds
3535
}
3636

37-
protocol URIPathCollection: Sendable {
37+
protocol URIPathCollection {
3838
func root() -> Node
39-
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) async throws
40-
func find(method: HTTPRequest.Method, path: String) async throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters)
39+
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws
40+
func find(method: HTTPRequest.Method, path: String) throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters)
4141
}
4242

4343
/// A Trie graph representation of an URI path + its handler
@@ -56,10 +56,10 @@ struct URIPath: URIPathCollection {
5656
/// - method: the HTTP method to use as first node
5757
/// - path: the full path as received from OpenAPI registration (including parameters encoded as {param name}
5858
/// - handler: the OpenAPI handler to invoke for such path
59-
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) async throws {
59+
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws {
6060

6161
// add or retrieve the HTTP method node
62-
var node = try await root().add(httpMethod: method)
62+
var node = try root().add(httpMethod: method)
6363

6464
// add each path element as child nodes
6565
let pathComponents = path.split(separator: "/")
@@ -68,15 +68,15 @@ struct URIPath: URIPathCollection {
6868
var comp = component
6969
comp.removeFirst()
7070
comp.removeLast()
71-
node = try await node.add(parameter: String(comp))
71+
node = try node.add(parameter: String(comp))
7272
}
7373
else {
74-
node = try await node.add(pathElement: String(component))
74+
node = try node.add(pathElement: String(component))
7575
}
7676
}
7777

7878
// finally add the handler
79-
_ = try await node.add(handler: handler)
79+
_ = try node.add(handler: handler)
8080
}
8181

8282
/// Navigate the tree to find the handler
@@ -91,13 +91,13 @@ struct URIPath: URIPathCollection {
9191
/// - OpenAPILambdaRouterError.noRouteForPath when there is no handler in the graph for the given combination of HTTP method and path
9292
/// - OpenAPILambdaRouterError.noRouteForMethod when there is no handler for that HTTP method
9393
/// - OpenAPILambdaRouterError.noHandlerForPath when there is no handler as leaf node of the tree. This is a programming error and should not happen
94-
func find(method: HTTPRequest.Method, path: String) async throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters)
94+
func find(method: HTTPRequest.Method, path: String) throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters)
9595
{
9696
var parameters: OpenAPILambdaRequestParameters = [:]
9797
let root: Node = root()
9898

9999
// first node is the HTTP Method
100-
guard let nodeHTTP = await root.children[method.rawValue] else {
100+
guard let nodeHTTP = root.children[method.rawValue] else {
101101
throw OpenAPILambdaRouterError.noRouteForMethod(method)
102102
}
103103

@@ -106,14 +106,14 @@ struct URIPath: URIPathCollection {
106106
let pathComponents = path.prefix(while: { $0 != "?" }).split(separator: "/")
107107
var currentNode = nodeHTTP
108108
for component in pathComponents {
109-
if let child = await currentNode.child(with: component) {
109+
if let child = currentNode.child(with: component) {
110110
// found a node with path element, continue to explore
111111
currentNode = child
112112
}
113113
else {
114114
// no path element for this component, maybe this component is a parameter value
115115
// let's see if we have a child param node
116-
if let child = await currentNode.parameterChild() {
116+
if let child = currentNode.parameterChild() {
117117
let paramName = child.value.asString!
118118

119119
//TODO: do not collect param when another child is a path with matching name ?
@@ -133,7 +133,7 @@ struct URIPath: URIPathCollection {
133133
}
134134

135135
//at this stage, current node must have a handler child
136-
guard let handlerNode = await currentNode.handlerChild() else {
136+
guard let handlerNode = currentNode.handlerChild() else {
137137
throw OpenAPILambdaRouterError.noHandlerForPath(path)
138138
}
139139

0 commit comments

Comments
 (0)