21 lines
873 B
Swift
21 lines
873 B
Swift
import Foundation
|
|
|
|
/// Missing capabilities back off independently; success in another zone cannot reset this state.
|
|
nonisolated struct SensorSchedule {
|
|
private(set) var deadline: TimeInterval = 0
|
|
private var failures = 0
|
|
private let successInterval: TimeInterval
|
|
private let failureInterval: TimeInterval
|
|
init(successInterval: TimeInterval = 10, failureInterval: TimeInterval = 60) {
|
|
self.successInterval = successInterval
|
|
self.failureInterval = failureInterval
|
|
}
|
|
|
|
func isDue(at now: TimeInterval) -> Bool { now >= deadline }
|
|
|
|
mutating func complete(available: Bool, at now: TimeInterval, retryInterval: TimeInterval? = nil) {
|
|
failures = available ? 0 : min(failures + 1, 8)
|
|
deadline = now + (retryInterval ?? (available ? successInterval : min(600, failureInterval * pow(2, Double(failures - 1)))))
|
|
}
|
|
}
|