Skip to content

Commit 653b680

Browse files
committed
Improve Logging
1 parent bb1615c commit 653b680

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

Sources/HTTPConnection.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ struct HTTPConnection {
5858
struct HTTPRequestSequence<S: AsyncSequence>: AsyncSequence, AsyncIteratorProtocol where S.Element == UInt8 {
5959
typealias Element = HTTPRequest
6060
private let bytes: S
61-
private var isComplete: Bool = false
6261

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

6968
mutating func next() async throws -> HTTPRequest? {
70-
guard !isComplete else { return nil }
71-
let request = try await HTTPRequestDecoder.decodeRequest(from: bytes)
72-
isComplete = !request.shouldKeepAlive
73-
return request
69+
do {
70+
return try await HTTPRequestDecoder.decodeRequest(from: bytes)
71+
} catch SocketError.disconnected {
72+
return nil
73+
} catch {
74+
throw error
75+
}
7476
}
7577
}

Sources/HTTPServer.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,18 @@ public final actor HTTPServer {
103103
}
104104

105105
private func handleConnection(_ connection: HTTPConnection) async {
106-
logger?.logInfo("open connection: \(connection.hostname)")
106+
logger?.logOpenConnection(connection)
107107
do {
108108
for try await request in connection.requests {
109+
logger?.logRequest(request, on: connection)
109110
let response = await handleRequest(request)
110111
try await connection.sendResponse(response)
111-
guard response.shouldKeepAlive else { break }
112112
}
113113
} catch {
114-
logger?.logError("connection error: \(error.localizedDescription)")
114+
logger?.logError(error, on: connection)
115115
}
116116
try? await connection.close()
117-
logger?.logInfo("close connection: \(connection.hostname)")
117+
logger?.logCloseConnection(connection)
118118
}
119119

120120
private func handleRequest(_ request: HTTPRequest) async -> HTTPResponse {
@@ -140,3 +140,28 @@ public final actor HTTPServer {
140140
}
141141
}
142142
}
143+
144+
extension HTTPLogging {
145+
146+
func logOpenConnection(_ connection: HTTPConnection) {
147+
logInfo("\(connection.identifer) open connection")
148+
}
149+
150+
func logCloseConnection(_ connection: HTTPConnection) {
151+
logInfo("\(connection.identifer) close connection")
152+
}
153+
154+
func logRequest(_ request: HTTPRequest, on connection: HTTPConnection) {
155+
logInfo("\(connection.identifer) request: \(request.method.rawValue) \(request.path)")
156+
}
157+
158+
func logError(_ error: Error, on connection: HTTPConnection) {
159+
logError("\(connection.identifer) error: \(error.localizedDescription)")
160+
}
161+
}
162+
163+
private extension HTTPConnection {
164+
var identifer: String {
165+
"<\(hostname)>"
166+
}
167+
}

Sources/Socket/Socket.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ struct Socket: Sendable, Hashable {
125125
let count = Socket.read(file, &byte, 1)
126126
if count == 1 {
127127
return byte
128+
} else if count == 0 {
129+
throw SocketError.disconnected
128130
} else if errno == EWOULDBLOCK {
129131
throw SocketError.blocked
130132
}

Sources/Socket/SocketError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ import Foundation
3434
enum SocketError: LocalizedError, Equatable {
3535
case failed(type: String, errno: Int32, message: String)
3636
case blocked
37+
case disconnected
3738

3839
var errorDescription: String? {
3940
switch self {
4041
case .failed(let type, let errno, let message):
4142
return "SocketError. \(type)(\(errno)): \(message)"
4243
case .blocked:
4344
return "SocketError. Blocked"
45+
case .disconnected:
46+
return "SocketError. Disconnected"
4447
}
4548
}
4649

0 commit comments

Comments
 (0)