Skip to content

Commit

Permalink
Allow specialization of the RouterResponder (#444)
Browse files Browse the repository at this point in the history
* Allow specialization of the RouterResponder

# Conflicts:
#	Sources/Hummingbird/Router/Trie/Trie+serialize.swift

* Make the router's API public for use in benchmarks

* Fix formatting

* Put router serialization under SPI again

* Fix build error

---------

Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
  • Loading branch information
Joannis and adam-fowler committed May 20, 2024
1 parent 71d6b99 commit aed1e36
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions Benchmarks/Benchmarks/Router/RouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
import Benchmark
import HTTPTypes
import Hummingbird
import NIOEmbedded
import NIOHTTPTypes
@_spi(Internal) import HummingbirdCore
import HummingbirdCore
import Logging
import NIOCore
import NIOEmbedded
import NIOHTTPTypes
import NIOPosix

/// Implementation of a basic request context that supports everything the Hummingbird library needs
Expand Down
7 changes: 6 additions & 1 deletion Sources/Hummingbird/Router/EndpointResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import HTTPTypes

/// Stores endpoint responders for each HTTP method
@usableFromInline
struct EndpointResponders<Context: BaseRequestContext>: Sendable {
init(path: String) {
self.path = path
self.methods = [:]
}

public func getResponder(for method: HTTPRequest.Method) -> (any HTTPResponder<Context>)? {
@inlinable
public func getResponder(for method: __shared HTTPRequest.Method) -> (any HTTPResponder<Context>)? {
return self.methods[method]
}

Expand All @@ -41,6 +43,9 @@ struct EndpointResponders<Context: BaseRequestContext>: Sendable {
}
}

@usableFromInline
var methods: [HTTPRequest.Method: any HTTPResponder<Context>]

@usableFromInline
var path: String
}
6 changes: 6 additions & 0 deletions Sources/Hummingbird/Router/RouterResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import NIOCore

public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
@usableFromInline
let trie: RouterTrie<EndpointResponders<Context>>

@usableFromInline
let notFoundResponder: any HTTPResponder<Context>

@usableFromInline
let options: RouterOptions

init(
Expand All @@ -33,6 +38,7 @@ public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
/// Respond to request by calling correct handler
/// - Parameter request: HTTP request
/// - Returns: EventLoopFuture that will be fulfilled with the Response
@inlinable
public func respond(to request: Request, context: Context) async throws -> Response {
let path: String
if self.options.contains(.caseInsensitive) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Router/Trie/RouterTrie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ struct Trie: Sendable {
init() {}
}

@_spi(Internal) public final class RouterTrie<Value: Sendable>: Sendable {
@_documentation(visibility: internal)
public final class RouterTrie<Value: Sendable>: Sendable {
@usableFromInline
let trie: Trie

@usableFromInline
let values: [Value?]

@inlinable
@_spi(Internal) public init(base: RouterPathTrieBuilder<Value>) {
var trie = Trie()
var values: [Value?] = []
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Router/Trie/Trie+resolve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import NIOCore
extension RouterTrie {
/// Resolve a path to a `Value` if available
@inlinable
@_spi(Internal) public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? {
public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? {
let pathComponents = path.split(separator: "/", omittingEmptySubsequences: true)
var pathComponentsIterator = pathComponents.makeIterator()
var parameters = Parameters()
Expand Down
3 changes: 0 additions & 3 deletions Sources/Hummingbird/Router/Trie/Trie+serialize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import NIOCore

extension RouterTrie {
@inlinable
static func serialize(
_ node: RouterPathTrieBuilder<Value>.Node,
trie: inout Trie,
Expand Down Expand Up @@ -82,7 +81,6 @@ extension RouterTrie {
trie.nodes[nodeIndex].nextSiblingNodeIndex = trie.nodes.count
}

@inlinable
static func serializeChildren(
of node: RouterPathTrieBuilder<Value>.Node,
trie: inout Trie,
Expand All @@ -95,7 +93,6 @@ extension RouterTrie {
}
}

@inlinable
internal static func highestPriorityFirst(lhs: RouterPathTrieBuilder<Value>.Node, rhs: RouterPathTrieBuilder<Value>.Node) -> Bool {
lhs.key.priority > rhs.key.priority
}
Expand Down
7 changes: 2 additions & 5 deletions Sources/Hummingbird/Router/TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import HummingbirdCore
@usableFromInline
var root: Node

public init() {
@_spi(Internal) public init() {
self.root = Node(key: .null, output: nil)
}

Expand All @@ -28,7 +28,7 @@ import HummingbirdCore
/// - entry: Path for entry
/// - value: Value to add to this path if one does not exist already
/// - onAdd: How to edit the value at this path
public func addEntry(_ entry: RouterPath, value: @autoclosure () -> Value, onAdd: (Node) -> Void = { _ in }) {
@_spi(Internal) public func addEntry(_ entry: RouterPath, value: @autoclosure () -> Value, onAdd: (Node) -> Void = { _ in }) {
var node = self.root
for key in entry {
node = node.addChild(key: key, output: nil)
Expand All @@ -51,13 +51,10 @@ import HummingbirdCore

/// Trie Node. Each node represents one component of a URI path
@_spi(Internal) public final class Node {
@usableFromInline
let key: RouterPath.Element

@usableFromInline
var children: [Node]

@usableFromInline
var value: Value?

init(key: RouterPath.Element, output: Value?) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdTests/TrieRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

@_spi(Internal) import Hummingbird
@testable @_spi(Internal) import Hummingbird
import XCTest

class TrieRouterTests: XCTestCase {
Expand Down

0 comments on commit aed1e36

Please sign in to comment.