Skip to content

Commit 8cb3e42

Browse files
authored
Remove HBRequestChannel.init(allocator:logger:) protocol requirement (#389)
* Remove HBRequestChannel.init(allocator:logger:) conformance * Fix benchmarks, cleanup
1 parent 251e8e0 commit 8cb3e42

File tree

9 files changed

+23
-52
lines changed

9 files changed

+23
-52
lines changed

Benchmarks/Benchmarks/Router/RouterBenchmarks.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ import NIOPosix
2525
struct BasicBenchmarkContext: HBRequestContext {
2626
var coreContext: HBCoreRequestContext
2727

28-
init(
29-
allocator: ByteBufferAllocator,
30-
logger: Logger
31-
) {
32-
self.coreContext = .init(allocator: allocator, logger: logger)
28+
public init(channel: Channel, logger: Logger) {
29+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
3330
}
3431
}
3532

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ let package = Package(
9191
.product(name: "HTTPTypes", package: "swift-http-types"),
9292
.product(name: "NIOCore", package: "swift-nio"),
9393
.product(name: "NIOConcurrencyHelpers", package: "swift-nio"),
94+
.product(name: "NIOEmbedded", package: "swift-nio"),
9495
.product(name: "NIOHTTPTypes", package: "swift-nio-extras"),
9596
.product(name: "NIOHTTPTypesHTTP1", package: "swift-nio-extras"),
9697
.product(name: "NIOPosix", package: "swift-nio"),

Sources/Hummingbird/Server/RequestContext.swift

+2-20
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,6 @@ public protocol HBRequestContext: HBBaseRequestContext {
122122
/// - channel: Channel that initiated this request
123123
/// - logger: Logger used for this request
124124
init(channel: Channel, logger: Logger)
125-
/// initialize an `HBRequestContext`
126-
/// - Parameters
127-
/// - allocator: ByteBuffer allocator
128-
/// - logger: Logger used for this request
129-
init(allocator: ByteBufferAllocator, logger: Logger)
130-
}
131-
132-
extension HBRequestContext {
133-
/// Initialize an `HBRequestContext`
134-
/// - Parameters:
135-
/// - channel: Channel that initiated this request
136-
/// - logger: Logger used for this request
137-
public init(channel: Channel, logger: Logger) {
138-
self.init(allocator: channel.allocator, logger: logger)
139-
}
140125
}
141126

142127
/// Implementation of a basic request context that supports everything the Hummingbird library needs
@@ -148,12 +133,9 @@ public struct HBBasicRequestContext: HBRequestContext {
148133
/// - Parameters:
149134
/// - allocator: Allocator
150135
/// - logger: Logger
151-
public init(
152-
allocator: ByteBufferAllocator,
153-
logger: Logger
154-
) {
136+
public init(channel: Channel, logger: Logger) {
155137
self.coreContext = .init(
156-
allocator: allocator,
138+
allocator: channel.allocator,
157139
logger: logger
158140
)
159141
}

Sources/HummingbirdRouter/RouterBuilderContext.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public struct HBBasicRouterRequestContext: HBRequestContext, HBRouterRequestCont
3737
public var routerContext: HBRouterBuilderContext
3838
public var coreContext: HBCoreRequestContext
3939

40-
public init(allocator: ByteBufferAllocator, logger: Logger) {
41-
self.coreContext = .init(allocator: allocator, logger: logger)
40+
public init(channel: Channel, logger: Logger) {
41+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
4242
self.routerContext = .init()
4343
}
4444
}

Sources/HummingbirdXCT/HBXCTRouter.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import Atomics
1616
import HTTPTypes
17+
import NIOEmbedded
1718
@_spi(HBXCT) import Hummingbird
1819
@_spi(HBXCT) import HummingbirdCore
1920
import Logging
@@ -38,7 +39,7 @@ struct HBXCTRouter<Responder: HBResponder>: HBXCTApplication where Responder.Con
3839
self.logger = app.logger
3940
self.makeContext = { logger in
4041
Responder.Context(
41-
allocator: ByteBufferAllocator(),
42+
channel: NIOAsyncTestingChannel(),
4243
logger: logger
4344
)
4445
}

Tests/HummingbirdRouterTests/RouterTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ public struct HBTestRouterContext2: HBRouterRequestContext, HBRequestContext {
434434
/// additional data
435435
public var string: String
436436

437-
public init(allocator: ByteBufferAllocator, logger: Logger) {
437+
public init(channel: Channel, logger: Logger) {
438438
self.routerContext = .init()
439-
self.coreContext = .init(allocator: allocator, logger: logger)
439+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
440440
self.string = ""
441441
}
442442
}

Tests/HummingbirdTests/ApplicationTests.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ final class ApplicationTests: XCTestCase {
334334
return encoder
335335
}
336336

337-
init(allocator: ByteBufferAllocator, logger: Logger) {
338-
self.coreContext = .init(allocator: allocator, logger: logger)
337+
init(channel: Channel, logger: Logger) {
338+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
339339
}
340340
}
341341
struct Name: HBResponseCodable {
@@ -404,8 +404,8 @@ final class ApplicationTests: XCTestCase {
404404

405405
func testMaxUploadSize() async throws {
406406
struct MaxUploadRequestContext: HBRequestContext {
407-
init(allocator: ByteBufferAllocator, logger: Logger) {
408-
self.coreContext = .init(allocator: allocator, logger: logger)
407+
init(channel: Channel, logger: Logger) {
408+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
409409
}
410410

411411
var coreContext: HBCoreRequestContext
@@ -448,11 +448,6 @@ final class ApplicationTests: XCTestCase {
448448
self.coreContext = .init(allocator: channel.allocator, logger: logger)
449449
self.remoteAddress = channel.remoteAddress
450450
}
451-
452-
init(allocator: ByteBufferAllocator, logger: Logger) {
453-
self.coreContext = .init(allocator: allocator, logger: logger)
454-
self.remoteAddress = nil
455-
}
456451
}
457452
let router = HBRouter(context: HBSocketAddressRequestContext.self)
458453
router.get("/") { _, context -> String in

Tests/HummingbirdTests/RouterTests.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -483,18 +483,15 @@ final class RouterTests: XCTestCase {
483483
}
484484
}
485485

486-
public struct HBTestRouterContext2: HBRequestContext {
487-
public init(
488-
allocator: ByteBufferAllocator,
489-
logger: Logger
490-
) {
491-
self.coreContext = .init(allocator: allocator, logger: logger)
486+
struct HBTestRouterContext2: HBRequestContext {
487+
init(channel: Channel, logger: Logger) {
488+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
492489
self.string = ""
493490
}
494491

495492
/// parameters
496-
public var coreContext: HBCoreRequestContext
493+
var coreContext: HBCoreRequestContext
497494

498495
/// additional data
499-
public var string: String
496+
var string: String
500497
}

Tests/HummingbirdTests/URLEncodedForm/Application+URLEncodedFormTests.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import Hummingbird
1616
import HummingbirdXCT
1717
import Logging
18+
import NIOCore
1819
import XCTest
1920

2021
class HummingBirdURLEncodedTests: XCTestCase {
@@ -27,11 +28,8 @@ class HummingBirdURLEncodedTests: XCTestCase {
2728
struct URLEncodedCodingRequestContext: HBRequestContext {
2829
var coreContext: HBCoreRequestContext
2930

30-
init(allocator: ByteBufferAllocator, logger: Logger) {
31-
self.coreContext = .init(
32-
allocator: allocator,
33-
logger: logger
34-
)
31+
init(channel: Channel, logger: Logger) {
32+
self.coreContext = .init(allocator: channel.allocator, logger: logger)
3533
}
3634

3735
var requestDecoder: URLEncodedFormDecoder { .init() }

0 commit comments

Comments
 (0)