fix tick worker
This commit is contained in:
parent
1c1bec9d78
commit
153f172daf
@ -132,13 +132,14 @@ public actor PeriodicWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static func runFixedRate(configuration: Configuration, operation: @escaping Operation, onError: @escaping ErrorHandler) async {
|
private static func runFixedRate(configuration: Configuration, operation: @escaping Operation, onError: @escaping ErrorHandler) async {
|
||||||
var nextRun = ContinuousClock.now
|
let clock = ContinuousClock()
|
||||||
|
var nextRun = clock.now
|
||||||
if !configuration.runImmediately {
|
if !configuration.runImmediately {
|
||||||
nextRun = nextRun.advanced(by: configuration.interval)
|
nextRun = nextRun.advanced(by: configuration.interval)
|
||||||
}
|
}
|
||||||
|
|
||||||
while !Task.isCancelled {
|
while !Task.isCancelled {
|
||||||
let now = ContinuousClock.now
|
let now = clock.now
|
||||||
if nextRun > now {
|
if nextRun > now {
|
||||||
let delay = now.duration(to: nextRun)
|
let delay = now.duration(to: nextRun)
|
||||||
guard await sleep(interval: delay, tolerance: configuration.tolerance) else {
|
guard await sleep(interval: delay, tolerance: configuration.tolerance) else {
|
||||||
@ -155,7 +156,7 @@ public actor PeriodicWorker {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let afterRun = ContinuousClock.now
|
let afterRun = clock.now
|
||||||
repeat {
|
repeat {
|
||||||
nextRun = nextRun.advanced(by: configuration.interval)
|
nextRun = nextRun.advanced(by: configuration.interval)
|
||||||
} while nextRun <= afterRun
|
} while nextRun <= afterRun
|
||||||
|
|||||||
@ -90,11 +90,11 @@ actor SDLContextActor {
|
|||||||
nonisolated private let provider: NEPacketTunnelProvider
|
nonisolated private let provider: NEPacketTunnelProvider
|
||||||
|
|
||||||
// 处理权限控制
|
// 处理权限控制
|
||||||
private var updatePolicyTask: Task<Void, Never>?
|
|
||||||
private let policyService: PolicyService
|
private let policyService: PolicyService
|
||||||
|
private var updatePolicyWorker: PeriodicWorker?
|
||||||
|
|
||||||
// stunRequest任务
|
// stunRequest任务
|
||||||
private var stunRequestTask: Task<Void, Never>?
|
private var stunRequestWorker: PeriodicWorker?
|
||||||
|
|
||||||
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher) {
|
public init(provider: NEPacketTunnelProvider, config: SDLConfiguration, rsaCipher: RSACipher) {
|
||||||
let puncherActor = SDLPuncherActor()
|
let puncherActor = SDLPuncherActor()
|
||||||
@ -183,11 +183,11 @@ actor SDLContextActor {
|
|||||||
await self.arpServer.stop()
|
await self.arpServer.stop()
|
||||||
await self.sessionManager.clear()
|
await self.sessionManager.clear()
|
||||||
|
|
||||||
self.stunRequestTask?.cancel()
|
await self.stunRequestWorker?.stop()
|
||||||
self.stunRequestTask = nil
|
self.stunRequestWorker = nil
|
||||||
|
|
||||||
self.updatePolicyTask?.cancel()
|
await self.updatePolicyWorker?.stop()
|
||||||
self.updatePolicyTask = nil
|
self.updatePolicyWorker = nil
|
||||||
await self.policyService.clear()
|
await self.policyService.clear()
|
||||||
|
|
||||||
await self.packetOutboundActor.stop()
|
await self.packetOutboundActor.stop()
|
||||||
@ -349,19 +349,27 @@ extension SDLContextActor {
|
|||||||
|
|
||||||
// 注册成功super的回调函数
|
// 注册成功super的回调函数
|
||||||
private func whenRegistedSuper() async {
|
private func whenRegistedSuper() async {
|
||||||
self.updatePolicyTask?.cancel()
|
await self.updatePolicyWorker?.stop()
|
||||||
|
let policyService = self.policyService
|
||||||
self.updatePolicyTask = Task {
|
let superServiceProxy = self.superServiceProxy
|
||||||
do {
|
|
||||||
while true {
|
let updatePolicyWorker = PeriodicWorker(
|
||||||
try await Task.sleep(for: .seconds(300))
|
configuration: .init(
|
||||||
SDLLogger.log("[SDLContext] updatePolicyTask execute")
|
interval: .seconds(300),
|
||||||
await self.policyService.updatePolicy(superServiceProxy: self.superServiceProxy)
|
runImmediately: false,
|
||||||
}
|
mode: .fixedDelay,
|
||||||
} catch let err {
|
errorPolicy: .keepRunning(delay: .seconds(5))
|
||||||
|
),
|
||||||
|
operation: {
|
||||||
|
SDLLogger.log("[SDLContext] updatePolicyTask execute")
|
||||||
|
await policyService.updatePolicy(superServiceProxy: superServiceProxy)
|
||||||
|
},
|
||||||
|
onError: { err in
|
||||||
SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)")
|
SDLLogger.log("[SDLContext] updatePolicyTask stop with err: \(err)")
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
|
self.updatePolicyWorker = updatePolicyWorker
|
||||||
|
await updatePolicyWorker.start()
|
||||||
|
|
||||||
// 启动stun任务
|
// 启动stun任务
|
||||||
await self.startStunRequestTask()
|
await self.startStunRequestTask()
|
||||||
@ -519,23 +527,17 @@ extension SDLContextActor {
|
|||||||
|
|
||||||
// MARK: -- StunRequestTask
|
// MARK: -- StunRequestTask
|
||||||
private func startStunRequestTask() async {
|
private func startStunRequestTask() async {
|
||||||
self.stunRequestTask?.cancel()
|
await self.stunRequestWorker?.stop()
|
||||||
self.stunRequestTask = nil
|
|
||||||
|
|
||||||
// 处理心跳逻辑
|
let stunRequestWorker = PeriodicWorker(
|
||||||
self.stunRequestTask = Task { [weak self] in
|
configuration: .init(
|
||||||
let timerStream = SDLAsyncTimerStream()
|
interval: .seconds(8),
|
||||||
timerStream.start(interval: .seconds(8))
|
runImmediately: true,
|
||||||
|
mode: .fixedDelay,
|
||||||
for await _ in timerStream.stream {
|
errorPolicy: .keepRunning(delay: .seconds(5))
|
||||||
if Task.isCancelled {
|
),
|
||||||
break
|
operation: { [weak self] in
|
||||||
}
|
|
||||||
|
|
||||||
let probeReply = try? await self?.ipv6AssistClient?.probe(requestTimeout: .seconds(3))
|
let probeReply = try? await self?.ipv6AssistClient?.probe(requestTimeout: .seconds(3))
|
||||||
if Task.isCancelled {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if let v6Info = probeReply?.v6Info, let v6Address = SDLUtil.ipv6DataToString(v6Info.v6) {
|
if let v6Info = probeReply?.v6Info, let v6Address = SDLUtil.ipv6DataToString(v6Info.v6) {
|
||||||
SDLLogger.log("[SDLContext] probe ipv6 address: \(v6Address)")
|
SDLLogger.log("[SDLContext] probe ipv6 address: \(v6Address)")
|
||||||
@ -543,15 +545,14 @@ extension SDLContextActor {
|
|||||||
SDLLogger.log("[SDLContext] probe ipv6 address: empty")
|
SDLLogger.log("[SDLContext] probe ipv6 address: empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if Task.isCancelled {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
await self?.sendStunRequest(v6Info: probeReply?.v6Info)
|
await self?.sendStunRequest(v6Info: probeReply?.v6Info)
|
||||||
|
},
|
||||||
|
onError: { err in
|
||||||
|
SDLLogger.log("[SDLContext] udp stunRequestTask stop with err: \(err)")
|
||||||
}
|
}
|
||||||
|
)
|
||||||
SDLLogger.log("[SDLContext] udp stunRequestTask cancel")
|
self.stunRequestWorker = stunRequestWorker
|
||||||
}
|
await stunRequestWorker.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func sendStunRequest(v6Info: SDLV6Info?) async {
|
private func sendStunRequest(v6Info: SDLV6Info?) async {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user