punchnet-macos/Tun/Punchnet/Actors/SDLSupervisor.swift
2026-03-10 16:30:31 +08:00

33 lines
922 B
Swift

//
// SDLSupervisor.swift
// punchnet
//
// Created by on 2026/3/10.
//
actor SDLSupervisor {
private var loopChildWorkers: [Task<Void, Never>] = []
func addWorker(name: String, _ body: @escaping () async throws -> Void, retryDelay: Duration = .seconds(2)) {
let worker = Task(name: name) {
while !Task.isCancelled {
do {
try await body()
} catch is CancellationError {
break
} catch let err {
SDLLogger.shared.log("[Supervisor] worker \(name) crashed: \(err.localizedDescription)")
try? await Task.sleep(for: retryDelay)
}
}
}
self.loopChildWorkers.append(worker)
}
func stop() {
self.loopChildWorkers.forEach { $0.cancel() }
self.loopChildWorkers.removeAll()
}
}