Skip to content

Commit

Permalink
Copy RequestID from main branch (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Jan 3, 2024
1 parent 1b08e41 commit 48371bc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
7 changes: 1 addition & 6 deletions Sources/Hummingbird/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
//===----------------------------------------------------------------------===//

import Atomics
import HummingbirdCore
import Logging
import NIOCore
Expand Down Expand Up @@ -145,8 +144,7 @@ extension HBApplicationProtocol {
}

public func loggerWithRequestId(_ logger: Logger) -> Logger {
let requestId = globalRequestID.loadThenWrappingIncrement(by: 1, ordering: .relaxed)
return logger.with(metadataKey: "hb_id", value: .stringConvertible(requestId))
return logger.with(metadataKey: "hb_id", value: .stringConvertible(RequestID()))
}

/// Application class. Brings together all the components of Hummingbird together
Expand Down Expand Up @@ -238,6 +236,3 @@ extension Logger {
return logger
}
}

/// Current global request ID
private let globalRequestID = ManagedAtomic(0)
40 changes: 40 additions & 0 deletions Sources/Hummingbird/Server/RequestID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Atomics

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

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

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

func formatAsHexWithLeadingZeros(_ value: UInt64) -> String {
let string = String(value, radix: 16)
if string.count < 16 {
return String(repeating: "0", count: 16 - string.count) + string
} else {
return string
}
}

private static let high = String(UInt64.random(in: .min ... .max), radix: 16)
private static let globalRequestID = ManagedAtomic<UInt64>(UInt64.random(in: .min ... .max))
}
7 changes: 3 additions & 4 deletions Tests/HummingbirdRouterTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,14 @@ final class RouterTests: XCTestCase {
}
let app = HBApplication(responder: router)
try await app.test(.router) { client in
let idString = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let id = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let body = try XCTUnwrap(response.body)
return String(buffer: body)
}
let id = try XCTUnwrap(Int(idString))
try await client.XCTExecute(uri: "/id", method: .get) { response in
let body = try XCTUnwrap(response.body)
let id2 = Int(String(buffer: body))
XCTAssertEqual(id2, id + 1)
let id2 = String(buffer: body)
XCTAssertNotEqual(id2, id)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions Tests/HummingbirdTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,14 @@ final class RouterTests: XCTestCase {
}
let app = HBApplication(responder: router.buildResponder())
try await app.test(.router) { client in
let idString = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let id = try await client.XCTExecute(uri: "/id", method: .get) { response -> String in
let body = try XCTUnwrap(response.body)
return String(buffer: body)
}
let id = try XCTUnwrap(Int(idString))
try await client.XCTExecute(uri: "/id", method: .get) { response in
let body = try XCTUnwrap(response.body)
let id2 = Int(String(buffer: body))
XCTAssertEqual(id2, id + 1)
let id2 = String(buffer: body)
XCTAssertNotEqual(id, id2)
}
}
}
Expand Down

0 comments on commit 48371bc

Please sign in to comment.