调整生命周期的管理

This commit is contained in:
anlicheng 2026-05-05 16:50:41 +08:00
parent 385edf3c6c
commit b6a23226bc

View File

@ -161,25 +161,16 @@ actor SDLContextActor {
} }
private func startSuperClient() async throws { private func startSuperClient() async throws {
SDLLogger.log("[SDLContext] try start quicClient", for: .debug)
// monitor // monitor
let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443) let quicClient = SDLQUICClient(host: self.config.serverHost, port: 1443)
self.quicClient = quicClient self.quicClient = quicClient
await quicClient.start() await quicClient.start()
defer {
Task {
await self.quicClient?.stop()
self.quicClient = nil
SDLLogger.log("[SDLContext] quicClient: stop")
}
}
// quic // quic
try await Task.sleep(for: .seconds(0.5)) try await Task.sleep(for: .seconds(0.5))
SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)") SDLLogger.log("[SDLContext] start quic client: \(self.config.serverHost)")
do {
try await withTaskCancellationHandler { try await withTaskCancellationHandler {
try await withThrowingTaskGroup { group in try await withThrowingTaskGroup { group in
defer { defer {
@ -199,16 +190,20 @@ actor SDLContextActor {
try Task.checkCancellation() try Task.checkCancellation()
await quicClient.send(type: .ping, data: Data()) await quicClient.send(type: .ping, data: Data())
} }
SDLLogger.log("[SDLQUICClient] udp pingTask cancel", for: .debug)
} }
try await group.next() try await group.next()
} }
} onCancel: { } onCancel: {
SDLLogger.log("[SDLQUICClient] quicClient taskGroup cancel", for: .debug)
Task { Task {
await quicClient.stop() await quicClient.stop()
} }
} }
} catch let err {
await quicClient.stop()
throw err
}
} }
private func handleQUICMessage(message: SDLQUICInboundMessage) async { private func handleQUICMessage(message: SDLQUICInboundMessage) async {
@ -289,12 +284,7 @@ actor SDLContextActor {
let dnsClient = DNSCloudClient(host: self.config.serverHost, port: 15353) let dnsClient = DNSCloudClient(host: self.config.serverHost, port: 15353)
self.dnsClient = dnsClient self.dnsClient = dnsClient
dnsClient.start() dnsClient.start()
do {
defer {
self.dnsClient = nil
dnsClient.stop()
}
try await withTaskCancellationHandler { try await withTaskCancellationHandler {
for try await packet in dnsClient.packetFlow { for try await packet in dnsClient.packetFlow {
try Task.checkCancellation() try Task.checkCancellation()
@ -305,6 +295,10 @@ actor SDLContextActor {
} onCancel: { } onCancel: {
dnsClient.stop() dnsClient.stop()
} }
} catch let err {
dnsClient.stop()
throw err
}
} }
private func startDnsLocalMonitor() { private func startDnsLocalMonitor() {
@ -325,13 +319,7 @@ actor SDLContextActor {
SDLLogger.log("[SDLContext] dnsLocalClient started") SDLLogger.log("[SDLContext] dnsLocalClient started")
self.dnsLocalClient = dnsLocalClient self.dnsLocalClient = dnsLocalClient
defer { do {
Task {
self.dnsLocalClient = nil
await dnsLocalClient.stop()
}
}
try await withTaskCancellationHandler { try await withTaskCancellationHandler {
// //
for try await packet in dnsLocalClient.packetFlow { for try await packet in dnsLocalClient.packetFlow {
@ -345,6 +333,10 @@ actor SDLContextActor {
await dnsLocalClient.stop() await dnsLocalClient.stop()
} }
} }
} catch let err {
await dnsLocalClient.stop()
throw err
}
} }
private func startUDPHoleMonitor() { private func startUDPHoleMonitor() {