调整变量的ARC

This commit is contained in:
anlicheng 2026-05-06 16:05:03 +08:00
parent f9fa7bf73e
commit b858be7310
4 changed files with 35 additions and 12 deletions

View File

@ -374,7 +374,10 @@ actor SDLContextActor {
self.udpHoleLocalAddress = localAddress
defer {
self.udpHole = nil
if self.udpHole === udpHole {
self.udpHole = nil
self.udpHoleLocalAddress = nil
}
}
// nat
@ -430,8 +433,10 @@ actor SDLContextActor {
}
defer {
self.udpHoleV6?.stop()
self.udpHoleV6 = nil
if self.udpHoleV6 === udpHoleV6 {
udpHoleV6.stop()
self.udpHoleV6 = nil
}
}
try await withThrowingTaskGroup { group in
@ -485,12 +490,18 @@ actor SDLContextActor {
self.flowSessionManager.clear()
self.udpHoleMonitorTask?.cancel()
let udpHole = self.udpHole
self.udpHole = nil
self.udpHoleLocalAddress = nil
self.udpHoleV6?.stop()
let udpHoleV6 = self.udpHoleV6
self.udpHoleV6 = nil
self.udpHoleMonitorTask?.cancel()
self.udpHoleMonitorTask = nil
udpHoleV6?.stop()
await udpHole?.stop()
self.dnsMonitorTask?.cancel()
self.dnsLocalMonitorTask?.cancel()

View File

@ -82,6 +82,7 @@ actor DNSLocalClient {
self.connection = connection
self.cleanupTask = cleanupTask
connection.start(queue: .global())
}

View File

@ -76,7 +76,7 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
private let messageContinuation: AsyncThrowingStream<(SocketAddress, SDLHoleMessage), Error>.Continuation
private var isMessageContinuationFinished: Bool = false
private var counterActor: SDLUDPCounter
private let counterActor: SDLUDPCounter
//
init() throws {
@ -102,8 +102,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
self.channel = channel
let counterActor = self.counterActor
Task {
await self.counterActor.start()
await counterActor.start()
}
return localAddress
@ -117,8 +118,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
let remoteAddress = envelope.remoteAddress
let bytesCount = buffer.readableBytes
let counterActor = self.counterActor
Task {
await self.counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount)
await counterActor.increment(direction: .inbound, from: remoteAddress.description, bytes: bytesCount)
}
do {
@ -147,8 +149,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
return
}
let counterActor = self.counterActor
Task {
await self.counterActor.increment(direction: .outbound, from: remoteAddress.description, bytes: data.count)
await counterActor.increment(direction: .outbound, from: remoteAddress.description, bytes: data.count)
}
var buffer = channel.allocator.buffer(capacity: data.count + 1)
@ -162,8 +165,8 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
channel.writeAndFlush(envelope, promise: promise)
}
promise.futureResult.whenFailure { err in
self.finishMessageContinuationIfNeed(throwing: .sendFaied(err))
promise.futureResult.whenFailure { [weak self] err in
self?.finishMessageContinuationIfNeed(throwing: .sendFaied(err))
}
}
@ -173,8 +176,9 @@ private final class SDLUDPHoleHandler: ChannelInboundHandler {
self.channel = nil
try? self.group.syncShutdownGracefully()
let counterActor = self.counterActor
Task {
await self.counterActor.stop()
await counterActor.stop()
}
SDLLogger.log("[SDLUDPHole] stopped", for: .debug)

View File

@ -118,9 +118,16 @@ final class SDLUDPHoleV6: ChannelInboundHandler {
self.messageContinuation.finish()
self.eventContinuation.finish()
let channel = self.channel
self.channel = nil
try? channel?.close().wait()
try? self.group.syncShutdownGracefully()
}
deinit {
SDLLogger.log("[SDLUDPHoleV6] deinit", for: .debug)
}
}