Skip to content

Commit

Permalink
Changes based off comments... (#359)
Browse files Browse the repository at this point in the history
* Changes based off comments

* Remove threadPoolSize parameter

* Add BidirectionalStreaming test

* Use package scope
  • Loading branch information
adam-fowler authored Jan 26, 2024
1 parent ad0cd0b commit 3666669
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
6 changes: 1 addition & 5 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension HBApplicationProtocol {
@Sendable func respond(to request: HBRequest, channel: Channel) async throws -> HBResponse {
let context = Self.Responder.Context(
channel: channel,
logger: loggerWithRequestId(self.logger)
logger: self.logger.with(metadataKey: "hb_id", value: .stringConvertible(RequestID()))
)
// respond to request
var response = try await responder.respond(to: request, context: context)
Expand Down Expand Up @@ -147,10 +147,6 @@ extension HBApplicationProtocol {
}
}

public func loggerWithRequestId(_ logger: Logger) -> Logger {
return logger.with(metadataKey: "hb_id", value: .stringConvertible(RequestID()))
}

/// Application class. Brings together all the components of Hummingbird together
///
/// ```
Expand Down
24 changes: 6 additions & 18 deletions Sources/Hummingbird/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@ public struct HBApplicationConfiguration: Sendable {
// MARK: Member variables

/// Bind address for server
public let address: HBBindAddress
public var address: HBBindAddress
/// Server name to return in "server" header
public let serverName: String?
public var serverName: String?
/// Defines the maximum length for the queue of pending connections
public let backlog: Int
public var backlog: Int
/// Allows socket to be bound to an address that is already in use.
public let reuseAddress: Bool
public var reuseAddress: Bool
#if canImport(Network)
/// TLS options for NIO Transport services
public let tlsOptions: TSTLSOptions
public var tlsOptions: TSTLSOptions
#endif

/// don't run the HTTP server
public let noHTTPServer: Bool

// MARK: Initialization

/// Initialize HBApplication configuration
Expand All @@ -51,14 +48,11 @@ public struct HBApplicationConfiguration: Sendable {
/// - backlog: the maximum length for the queue of pending connections. If a connection request arrives with the queue full,
/// the client may receive an error with an indication of ECONNREFUSE
/// - reuseAddress: Allows socket to be bound to an address that is already in use.
/// - noHTTPServer: Don't start up the HTTP server.
public init(
address: HBBindAddress = .hostname(),
serverName: String? = nil,
backlog: Int = 256,
reuseAddress: Bool = true,
threadPoolSize: Int = 2,
noHTTPServer: Bool = false
reuseAddress: Bool = true
) {
self.address = address
self.serverName = serverName
Expand All @@ -67,8 +61,6 @@ public struct HBApplicationConfiguration: Sendable {
#if canImport(Network)
self.tlsOptions = .none
#endif

self.noHTTPServer = noHTTPServer
}

#if canImport(Network)
Expand All @@ -78,22 +70,18 @@ public struct HBApplicationConfiguration: Sendable {
/// - address: Bind address for server
/// - serverName: Server name to return in "server" header
/// - reuseAddress: Allows socket to be bound to an address that is already in use.
/// - noHTTPServer: Don't start up the HTTP server.
/// - tlsOptions: TLS options for when you are using NIOTransportServices
public init(
address: HBBindAddress = .hostname(),
serverName: String? = nil,
reuseAddress: Bool = true,
noHTTPServer: Bool = false,
tlsOptions: TSTLSOptions
) {
self.address = address
self.serverName = serverName
self.backlog = 256 // not used by Network framework
self.reuseAddress = reuseAddress
self.tlsOptions = tlsOptions

self.noHTTPServer = noHTTPServer
}

#endif
Expand Down
9 changes: 1 addition & 8 deletions Sources/Hummingbird/Middleware/MiddlewareGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ public final class HBMiddlewareGroup<Context> {
var middlewares: [any HBMiddlewareProtocol<Context>]

/// Initialize `HBMiddlewareGroup`
///
/// Set middleware array to be empty
public init() {
// this is set by WebSocketRouterGroup so this needs to be kept public
self.middlewares = []
}

init(middlewares: [any HBMiddlewareProtocol<Context>]) {
init(middlewares: [any HBMiddlewareProtocol<Context>] = []) {
self.middlewares = middlewares
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Hummingbird/Server/RequestID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
import Atomics

/// Generate Unique ID for each request
struct RequestID: CustomStringConvertible {
package struct RequestID: CustomStringConvertible {
let low: UInt64

init() {
package init() {
self.low = Self.globalRequestID.loadThenWrappingIncrement(by: 1, ordering: .relaxed)
}

var description: String {
package var description: String {
Self.high + self.formatAsHexWithLeadingZeros(self.low)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdCore/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public actor HBServer<ChildChannel: HBChildChannel>: Service {
/// Start server
/// - Parameter responder: Object that provides responses to requests sent to the server
/// - Returns: EventLoopFuture that is fulfilled when server has started
public func makeServer(childChannelSetup: ChildChannel, configuration: HBServerConfiguration) async throws -> AsyncServerChannel {
func makeServer(childChannelSetup: ChildChannel, configuration: HBServerConfiguration) async throws -> AsyncServerChannel {
let bootstrap: ServerBootstrapProtocol
#if canImport(Network)
if let tsBootstrap = self.createTSBootstrap(configuration: configuration) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdXCT/HBXCTRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct HBXCTRouter<Responder: HBResponder>: HBXCTApplication where Responder.Con
head: .init(method: method, scheme: "http", authority: "localhost", path: uri, headerFields: headers),
body: .stream(streamer)
)
let logger = loggerWithRequestId(self.logger)
let logger = self.logger.with(metadataKey: "hb_id", value: .stringConvertible(RequestID()))
let context = self.makeContext(logger)

group.addTask {
Expand Down
23 changes: 23 additions & 0 deletions Tests/HummingbirdTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,29 @@ final class ApplicationTests: XCTestCase {
}
}

/// test we can create out own application type conforming to HBApplicationProtocol
func testBidirectionalStreaming() async throws {
let buffer = randomBuffer(size: 1024 * 1024)
let router = HBRouter()
router.post("/") { request, context -> HBResponse in
.init(
status: .ok,
body: .init { writer in
for try await buffer in request.body {
let processed = context.allocator.buffer(bytes: buffer.readableBytesView.map {$0 ^ 0xff })
try await writer.write(processed)
}
}
)
}
let app = HBApplication(router: router)
try await app.test(.live) { client in
try await client.XCTExecute(uri: "/", method: .post, body: buffer) { response in
XCTAssertEqual(response.body, ByteBuffer(bytes: buffer.readableBytesView.map {$0 ^ 0xff }))
}
}
}

// MARK: Helper functions

func getServerTLSConfiguration() throws -> TLSConfiguration {
Expand Down

0 comments on commit 3666669

Please sign in to comment.