Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specialization of the RouterResponder #444

Merged
merged 5 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
7 changes: 4 additions & 3 deletions Sources/Hummingbird/Router/Trie/RouterTrie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ 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>) {
@_documentation(visibility: 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
9 changes: 6 additions & 3 deletions Sources/Hummingbird/Router/TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import HummingbirdCore

/// URI Path Trie Builder
@_spi(Internal) public struct RouterPathTrieBuilder<Value: Sendable> {
@_documentation(visibility: internal)
public struct RouterPathTrieBuilder<Value: Sendable> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be public. It is only ever used to build a router. I'm not sure why the TrieRouter serialize functions have been made @inlineable. They don't need to be. Building a router doesn't need to be optmized., It is only the resolve functions that should be @inlineable. By removing the inlineable on the serialize functions this can be make internal again.

@usableFromInline
var root: Node

@_documentation(visibility: internal)
public init() {
self.root = Node(key: .null, output: nil)
}
Expand All @@ -41,7 +43,7 @@ import HummingbirdCore
}
}

@_spi(Internal) public func build() -> RouterTrie<Value> {
internal func build() -> RouterTrie<Value> {
.init(base: self)
}

Expand All @@ -50,7 +52,8 @@ import HummingbirdCore
}

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

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 import Hummingbird
import XCTest

class TrieRouterTests: XCTestCase {
Expand Down