Skip to content

Commit

Permalink
Remove HBRequestChannel.init(allocator:logger:) protocol requirement (
Browse files Browse the repository at this point in the history
#389)

* Remove HBRequestChannel.init(allocator:logger:) conformance

* Fix benchmarks, cleanup
  • Loading branch information
adam-fowler authored Feb 20, 2024
1 parent 251e8e0 commit 8cb3e42
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 52 deletions.
7 changes: 2 additions & 5 deletions Benchmarks/Benchmarks/Router/RouterBenchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ import NIOPosix
struct BasicBenchmarkContext: HBRequestContext {
var coreContext: HBCoreRequestContext

init(
allocator: ByteBufferAllocator,
logger: Logger
) {
self.coreContext = .init(allocator: allocator, logger: logger)
public init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
}
}

Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let package = Package(
.product(name: "HTTPTypes", package: "swift-http-types"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
.product(name: "NIOHTTPTypes", package: "swift-nio-extras"),
.product(name: "NIOHTTPTypesHTTP1", package: "swift-nio-extras"),
.product(name: "NIOPosix", package: "swift-nio"),
Expand Down
22 changes: 2 additions & 20 deletions Sources/Hummingbird/Server/RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,6 @@ public protocol HBRequestContext: HBBaseRequestContext {
/// - channel: Channel that initiated this request
/// - logger: Logger used for this request
init(channel: Channel, logger: Logger)
/// initialize an `HBRequestContext`
/// - Parameters
/// - allocator: ByteBuffer allocator
/// - logger: Logger used for this request
init(allocator: ByteBufferAllocator, logger: Logger)
}

extension HBRequestContext {
/// Initialize an `HBRequestContext`
/// - Parameters:
/// - channel: Channel that initiated this request
/// - logger: Logger used for this request
public init(channel: Channel, logger: Logger) {
self.init(allocator: channel.allocator, logger: logger)
}
}

/// Implementation of a basic request context that supports everything the Hummingbird library needs
Expand All @@ -148,12 +133,9 @@ public struct HBBasicRequestContext: HBRequestContext {
/// - Parameters:
/// - allocator: Allocator
/// - logger: Logger
public init(
allocator: ByteBufferAllocator,
logger: Logger
) {
public init(channel: Channel, logger: Logger) {
self.coreContext = .init(
allocator: allocator,
allocator: channel.allocator,
logger: logger
)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdRouter/RouterBuilderContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public struct HBBasicRouterRequestContext: HBRequestContext, HBRouterRequestCont
public var routerContext: HBRouterBuilderContext
public var coreContext: HBCoreRequestContext

public init(allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(allocator: allocator, logger: logger)
public init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.routerContext = .init()
}
}
3 changes: 2 additions & 1 deletion Sources/HummingbirdXCT/HBXCTRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Atomics
import HTTPTypes
import NIOEmbedded
@_spi(HBXCT) import Hummingbird
@_spi(HBXCT) import HummingbirdCore
import Logging
Expand All @@ -38,7 +39,7 @@ struct HBXCTRouter<Responder: HBResponder>: HBXCTApplication where Responder.Con
self.logger = app.logger
self.makeContext = { logger in
Responder.Context(
allocator: ByteBufferAllocator(),
channel: NIOAsyncTestingChannel(),
logger: logger
)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/HummingbirdRouterTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ public struct HBTestRouterContext2: HBRouterRequestContext, HBRequestContext {
/// additional data
public var string: String

public init(allocator: ByteBufferAllocator, logger: Logger) {
public init(channel: Channel, logger: Logger) {
self.routerContext = .init()
self.coreContext = .init(allocator: allocator, logger: logger)
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.string = ""
}
}
13 changes: 4 additions & 9 deletions Tests/HummingbirdTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ final class ApplicationTests: XCTestCase {
return encoder
}

init(allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(allocator: allocator, logger: logger)
init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
}
}
struct Name: HBResponseCodable {
Expand Down Expand Up @@ -404,8 +404,8 @@ final class ApplicationTests: XCTestCase {

func testMaxUploadSize() async throws {
struct MaxUploadRequestContext: HBRequestContext {
init(allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(allocator: allocator, logger: logger)
init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
}

var coreContext: HBCoreRequestContext
Expand Down Expand Up @@ -448,11 +448,6 @@ final class ApplicationTests: XCTestCase {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.remoteAddress = channel.remoteAddress
}

init(allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(allocator: allocator, logger: logger)
self.remoteAddress = nil
}
}
let router = HBRouter(context: HBSocketAddressRequestContext.self)
router.get("/") { _, context -> String in
Expand Down
13 changes: 5 additions & 8 deletions Tests/HummingbirdTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,15 @@ final class RouterTests: XCTestCase {
}
}

public struct HBTestRouterContext2: HBRequestContext {
public init(
allocator: ByteBufferAllocator,
logger: Logger
) {
self.coreContext = .init(allocator: allocator, logger: logger)
struct HBTestRouterContext2: HBRequestContext {
init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
self.string = ""
}

/// parameters
public var coreContext: HBCoreRequestContext
var coreContext: HBCoreRequestContext

/// additional data
public var string: String
var string: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import Hummingbird
import HummingbirdXCT
import Logging
import NIOCore
import XCTest

class HummingBirdURLEncodedTests: XCTestCase {
Expand All @@ -27,11 +28,8 @@ class HummingBirdURLEncodedTests: XCTestCase {
struct URLEncodedCodingRequestContext: HBRequestContext {
var coreContext: HBCoreRequestContext

init(allocator: ByteBufferAllocator, logger: Logger) {
self.coreContext = .init(
allocator: allocator,
logger: logger
)
init(channel: Channel, logger: Logger) {
self.coreContext = .init(allocator: channel.allocator, logger: logger)
}

var requestDecoder: URLEncodedFormDecoder { .init() }
Expand Down

0 comments on commit 8cb3e42

Please sign in to comment.