Skip to content

Commit

Permalink
Improve Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Feb 20, 2022
1 parent bb1615c commit 653b680
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
12 changes: 7 additions & 5 deletions Sources/HTTPConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ struct HTTPConnection {
struct HTTPRequestSequence<S: AsyncSequence>: AsyncSequence, AsyncIteratorProtocol where S.Element == UInt8 {
typealias Element = HTTPRequest
private let bytes: S
private var isComplete: Bool = false

init(bytes: S) {
self.bytes = bytes
Expand All @@ -67,9 +66,12 @@ struct HTTPRequestSequence<S: AsyncSequence>: AsyncSequence, AsyncIteratorProtoc
func makeAsyncIterator() -> HTTPRequestSequence { self }

mutating func next() async throws -> HTTPRequest? {
guard !isComplete else { return nil }
let request = try await HTTPRequestDecoder.decodeRequest(from: bytes)
isComplete = !request.shouldKeepAlive
return request
do {
return try await HTTPRequestDecoder.decodeRequest(from: bytes)
} catch SocketError.disconnected {
return nil
} catch {
throw error
}
}
}
33 changes: 29 additions & 4 deletions Sources/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,18 @@ public final actor HTTPServer {
}

private func handleConnection(_ connection: HTTPConnection) async {
logger?.logInfo("open connection: \(connection.hostname)")
logger?.logOpenConnection(connection)
do {
for try await request in connection.requests {
logger?.logRequest(request, on: connection)
let response = await handleRequest(request)
try await connection.sendResponse(response)
guard response.shouldKeepAlive else { break }
}
} catch {
logger?.logError("connection error: \(error.localizedDescription)")
logger?.logError(error, on: connection)
}
try? await connection.close()
logger?.logInfo("close connection: \(connection.hostname)")
logger?.logCloseConnection(connection)
}

private func handleRequest(_ request: HTTPRequest) async -> HTTPResponse {
Expand All @@ -140,3 +140,28 @@ public final actor HTTPServer {
}
}
}

extension HTTPLogging {

func logOpenConnection(_ connection: HTTPConnection) {
logInfo("\(connection.identifer) open connection")
}

func logCloseConnection(_ connection: HTTPConnection) {
logInfo("\(connection.identifer) close connection")
}

func logRequest(_ request: HTTPRequest, on connection: HTTPConnection) {
logInfo("\(connection.identifer) request: \(request.method.rawValue) \(request.path)")
}

func logError(_ error: Error, on connection: HTTPConnection) {
logError("\(connection.identifer) error: \(error.localizedDescription)")
}
}

private extension HTTPConnection {
var identifer: String {
"<\(hostname)>"
}
}
2 changes: 2 additions & 0 deletions Sources/Socket/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ struct Socket: Sendable, Hashable {
let count = Socket.read(file, &byte, 1)
if count == 1 {
return byte
} else if count == 0 {
throw SocketError.disconnected
} else if errno == EWOULDBLOCK {
throw SocketError.blocked
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Socket/SocketError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ import Foundation
enum SocketError: LocalizedError, Equatable {
case failed(type: String, errno: Int32, message: String)
case blocked
case disconnected

var errorDescription: String? {
switch self {
case .failed(let type, let errno, let message):
return "SocketError. \(type)(\(errno)): \(message)"
case .blocked:
return "SocketError. Blocked"
case .disconnected:
return "SocketError. Disconnected"
}
}

Expand Down

0 comments on commit 653b680

Please sign in to comment.