Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address thread-safety of SCNetworkReachability #295

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion Sources/Segment/Plugins/Platforms/Vendors/AppleUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,66 @@ extension ConnectionStatus {
#else
self = .online(.wifi)
#endif

} else {
self = .offline
}
}
}


// MARK: -- Connection Status stuff

internal class ConnectionMonitor {
private var timer: QueueTimer? = nil

static let shared = ConnectionMonitor()

@Atomic var connectionStatus: ConnectionStatus = .unknown

init() {
self.timer = QueueTimer(interval: 300, immediate: true) { [weak self] in
guard let self else { return }
self.check()
}
}

internal func check() {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)

guard let defaultRouteReachability = (withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) { zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}) else {
connectionStatus = .unknown
return
}

var flags : SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
connectionStatus = .unknown
return
}

connectionStatus = ConnectionStatus(reachabilityFlags: flags)
}
}

internal func connectionStatus() -> ConnectionStatus {
return ConnectionMonitor.shared.connectionStatus
}

/*
/* 5-minute timer to check connection status. Checking this for
every event that comes through seems like overkill. */

private var __segment_connectionStatus: ConnectionStatus = .unknown
private var __segment_connectionStatusTimer: QueueTimer? = nil
private var __segment_connectionStatusLock = NSLock()

internal func __segment_connectionStatusCheck() -> ConnectionStatus {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
Expand All @@ -369,4 +421,20 @@ internal func connectionStatus() -> ConnectionStatus {
return ConnectionStatus(reachabilityFlags: flags)
}

internal func connectionStatus() -> ConnectionStatus {
// the locking may seem like overkill since we're updating it in a queue
// however, it is necessary since we're polling. :(
if __segment_connectionStatusTimer == nil {
__segment_connectionStatusTimer = QueueTimer(interval: 300, immediate: true) {
__segment_connectionStatusLock.lock()
defer { __segment_connectionStatusLock.unlock() }
__segment_connectionStatus = __segment_connectionStatusCheck()
}
}

__segment_connectionStatusLock.lock()
defer { __segment_connectionStatusLock.unlock() }
return __segment_connectionStatus
}
*/
#endif
10 changes: 7 additions & 3 deletions Sources/Segment/Utilities/QueueTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ internal class QueueTimer {

static var timers = [QueueTimer]()

static func schedule(interval: TimeInterval, queue: DispatchQueue = .main, handler: @escaping () -> Void) {
static func schedule(interval: TimeInterval, immediate: Bool = false, queue: DispatchQueue = .main, handler: @escaping () -> Void) {
let timer = QueueTimer(interval: interval, queue: queue, handler: handler)
Self.timers.append(timer)
}

init(interval: TimeInterval, queue: DispatchQueue = .main, handler: @escaping () -> Void) {
init(interval: TimeInterval, immediate: Bool = false, queue: DispatchQueue = .main, handler: @escaping () -> Void) {
self.interval = interval
self.queue = queue
self.handler = handler

timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
timer.schedule(deadline: .now() + self.interval, repeating: self.interval)
if immediate {
timer.schedule(deadline: .now(), repeating: self.interval)
} else {
timer.schedule(deadline: .now() + self.interval, repeating: self.interval)
}
timer.setEventHandler { [weak self] in
self?.handler()
}
Expand Down
Loading