Skip to content

Commit

Permalink
Synchonize access to retry timer
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Sep 17, 2024
1 parent 1839e63 commit ff8bfb2
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions OpenHABCore/Sources/OpenHABCore/Util/NetworkTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class NetworkTracker: ObservableObject {
private var connectionObjects: [ConnectionObject] = []

private var retryTimer: DispatchSourceTimer?
private let timerQueue = DispatchQueue(label: "com.openhab.networktracker.timerQueue")

private init() {
monitor = NWPathMonitor()
Expand Down Expand Up @@ -182,19 +183,22 @@ public final class NetworkTracker: ObservableObject {
// Start the retry timer to attempt connection every N seconds
private func startRetryTimer(_ retryInterval: TimeInterval) {
cancelRetryTimer()
retryTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
retryTimer?.schedule(deadline: .now() + retryInterval, repeating: retryInterval)
retryTimer?.setEventHandler { [weak self] in
os_log("Network status: Retry timer firing", log: OSLog.default, type: .info)
self?.attemptConnection()
timerQueue.sync {
retryTimer = DispatchSource.makeTimerSource(queue: timerQueue)
retryTimer?.schedule(deadline: .now() + retryInterval, repeating: retryInterval)
retryTimer?.setEventHandler { [weak self] in
os_log("Network status: Retry timer firing", log: OSLog.default, type: .info)
self?.attemptConnection()
}
retryTimer?.resume()
}
retryTimer?.resume()
}

// Cancel the retry timer
private func cancelRetryTimer() {
retryTimer?.cancel()
retryTimer = nil
timerQueue.sync {
retryTimer?.cancel()
retryTimer = nil
}
}

private func setActiveServer(_ server: ConnectionObject? = nil) {
Expand Down

0 comments on commit ff8bfb2

Please sign in to comment.