mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
Add deadlines as an alternative to timeouts (#770)
Add deadlines as an alternative to timeouts Motivation: Address #603 Modifications: - Add NIODeadline type - Add func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T> - Reduce the use of DispatchTime in favor of Time - Replace timeout calls with deadline calls where it makes sense Result: Tasks can be scheduled after an amount of time has passed, or at a certain time.
This commit is contained in:
1 parent
de8d3e260e
commit
0eb85a4478
13 files changed
+172
-76
No files matched your search
+114
-11
@@ -208,6 +208,10 @@ public protocol EventLoop: EventLoopGroup {
|
||||
/// - returns: `EventLoopFuture` that is notified once the task was executed.
|
||||
func submit<T>(_ task: @escaping () throws -> T) -> EventLoopFuture<T>
|
||||
|
||||
/// Schedule a `task` that is executed by this `SelectableEventLoop` at the given time.
|
||||
@discardableResult
|
||||
func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T>
|
||||
|
||||
/// Schedule a `task` that is executed by this `SelectableEventLoop` after the given amount of time.
|
||||
@discardableResult
|
||||
func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T>
|
||||
@@ -291,6 +295,101 @@ extension TimeAmount: Comparable {
|
||||
}
|
||||
}
|
||||
|
||||
extension TimeAmount {
|
||||
public static func + (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(lhs.nanoseconds + rhs.nanoseconds)
|
||||
}
|
||||
|
||||
public static func - (lhs: TimeAmount, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(lhs.nanoseconds - rhs.nanoseconds)
|
||||
}
|
||||
|
||||
public static func * <T: BinaryInteger>(lhs: T, rhs: TimeAmount) -> TimeAmount {
|
||||
return TimeAmount(TimeAmount.Value(lhs) * rhs.nanoseconds)
|
||||
}
|
||||
|
||||
public static func * <T: BinaryInteger>(lhs: TimeAmount, rhs: T) -> TimeAmount {
|
||||
return TimeAmount(lhs.nanoseconds * TimeAmount.Value(rhs))
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a point in time.
|
||||
///
|
||||
/// Stores the time in nanoseconds as returned by `DispatchTime.now().uptimeNanoseconds`
|
||||
///
|
||||
/// `NIODeadline` allow chaining multiple tasks with the same deadline without needing to
|
||||
/// compute new timeouts for each step
|
||||
///
|
||||
/// ```
|
||||
/// func doSomething(deadline: NIODeadline) -> EventLoopFuture<Void> {
|
||||
/// return step1(deadline: deadline).then {
|
||||
/// step2(deadline: deadline)
|
||||
/// }
|
||||
/// }
|
||||
/// doSomething(deadline: .now() + .seconds(5))
|
||||
/// ```
|
||||
///
|
||||
/// - note: `NIODeadline` should not be used to represent a time interval
|
||||
public struct NIODeadline: Equatable, Hashable {
|
||||
public typealias Value = UInt64
|
||||
|
||||
/// The nanoseconds since boot representation of the `NIODeadline`.
|
||||
public let uptimeNanoseconds: Value
|
||||
|
||||
public static let distantPast = NIODeadline(0)
|
||||
public static let distantFuture = NIODeadline(DispatchTime.distantFuture.uptimeNanoseconds)
|
||||
|
||||
private init(_ nanoseconds: Value) {
|
||||
self.uptimeNanoseconds = nanoseconds
|
||||
}
|
||||
|
||||
public static func now() -> NIODeadline {
|
||||
return NIODeadline(DispatchTime.now().uptimeNanoseconds)
|
||||
}
|
||||
|
||||
public static func uptimeNanoseconds(_ nanoseconds: Value) -> NIODeadline {
|
||||
return NIODeadline(nanoseconds)
|
||||
}
|
||||
}
|
||||
|
||||
extension NIODeadline: Comparable {
|
||||
public static func < (lhs: NIODeadline, rhs: NIODeadline) -> Bool {
|
||||
return lhs.uptimeNanoseconds < rhs.uptimeNanoseconds
|
||||
}
|
||||
|
||||
public static func > (lhs: NIODeadline, rhs: NIODeadline) -> Bool {
|
||||
return lhs.uptimeNanoseconds > rhs.uptimeNanoseconds
|
||||
}
|
||||
}
|
||||
|
||||
extension NIODeadline: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return self.uptimeNanoseconds.description
|
||||
}
|
||||
}
|
||||
|
||||
extension NIODeadline {
|
||||
public static func - (lhs: NIODeadline, rhs: NIODeadline) -> TimeAmount {
|
||||
return .nanoseconds(TimeAmount.Value(lhs.uptimeNanoseconds - rhs.uptimeNanoseconds))
|
||||
}
|
||||
|
||||
public static func + (lhs: NIODeadline, rhs: TimeAmount) -> NIODeadline {
|
||||
if rhs.nanoseconds < 0 {
|
||||
return NIODeadline(lhs.uptimeNanoseconds - rhs.nanoseconds.magnitude)
|
||||
} else {
|
||||
return NIODeadline(lhs.uptimeNanoseconds + rhs.nanoseconds.magnitude)
|
||||
}
|
||||
}
|
||||
|
||||
public static func - (lhs: NIODeadline, rhs: TimeAmount) -> NIODeadline {
|
||||
if rhs.nanoseconds < 0 {
|
||||
return NIODeadline(lhs.uptimeNanoseconds + rhs.nanoseconds.magnitude)
|
||||
} else {
|
||||
return NIODeadline(lhs.uptimeNanoseconds - rhs.nanoseconds.magnitude)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension EventLoop {
|
||||
public func submit<T>(_ task: @escaping () throws -> T) -> EventLoopFuture<T> {
|
||||
let promise: EventLoopPromise<T> = makePromise(file: #file, line: #line)
|
||||
@@ -569,7 +668,7 @@ internal final class SelectableEventLoop: EventLoop {
|
||||
return thread.isCurrent
|
||||
}
|
||||
|
||||
public func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T> {
|
||||
public func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T> {
|
||||
let promise: EventLoopPromise<T> = makePromise()
|
||||
let task = ScheduledTask({
|
||||
do {
|
||||
@@ -579,7 +678,7 @@ internal final class SelectableEventLoop: EventLoop {
|
||||
}
|
||||
}, { error in
|
||||
promise.fail(error)
|
||||
},`in`)
|
||||
}, deadline)
|
||||
|
||||
let scheduled = Scheduled(promise: promise, cancellationTask: {
|
||||
self.tasksLock.withLockVoid {
|
||||
@@ -592,10 +691,14 @@ internal final class SelectableEventLoop: EventLoop {
|
||||
return scheduled
|
||||
}
|
||||
|
||||
public func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws -> T) -> Scheduled<T> {
|
||||
return scheduleTask(deadline: .now() + `in`, task)
|
||||
}
|
||||
|
||||
public func execute(_ task: @escaping () -> Void) {
|
||||
schedule0(ScheduledTask(task, { error in
|
||||
// do nothing
|
||||
}, .nanoseconds(0)))
|
||||
}, .now()))
|
||||
}
|
||||
|
||||
/// Add the `ScheduledTask` to be executed.
|
||||
@@ -648,7 +751,7 @@ internal final class SelectableEventLoop: EventLoop {
|
||||
return .block
|
||||
}
|
||||
|
||||
let nextReady = sched.readyIn(DispatchTime.now())
|
||||
let nextReady = sched.readyIn(.now())
|
||||
if nextReady <= .nanoseconds(0) {
|
||||
// Something is ready to be processed just do a non-blocking select of events.
|
||||
return .now
|
||||
@@ -698,7 +801,7 @@ internal final class SelectableEventLoop: EventLoop {
|
||||
tasksLock.withLockVoid {
|
||||
if !scheduledTasks.isEmpty {
|
||||
// We only fetch the time one time as this may be expensive and is generally good enough as if we miss anything we will just do a non-blocking select again anyway.
|
||||
let now = DispatchTime.now()
|
||||
let now: NIODeadline = .now()
|
||||
|
||||
// Make a copy of the tasks so we can execute these while not holding the lock anymore
|
||||
while tasksCopy.count < tasksCopy.capacity, let task = scheduledTasks.peek() {
|
||||
@@ -989,19 +1092,19 @@ final public class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
private final class ScheduledTask {
|
||||
let task: () -> Void
|
||||
private let failFn: (Error) ->()
|
||||
private let readyTime: TimeAmount.Value
|
||||
private let readyTime: NIODeadline
|
||||
|
||||
init(_ task: @escaping () -> Void, _ failFn: @escaping (Error) -> Void, _ time: TimeAmount) {
|
||||
init(_ task: @escaping () -> Void, _ failFn: @escaping (Error) -> Void, _ time: NIODeadline) {
|
||||
self.task = task
|
||||
self.failFn = failFn
|
||||
self.readyTime = time.nanoseconds + TimeAmount.Value(DispatchTime.now().uptimeNanoseconds)
|
||||
self.readyTime = time
|
||||
}
|
||||
|
||||
func readyIn(_ t: DispatchTime) -> TimeAmount {
|
||||
if readyTime < t.uptimeNanoseconds {
|
||||
func readyIn(_ t: NIODeadline) -> TimeAmount {
|
||||
if readyTime < t {
|
||||
return .nanoseconds(0)
|
||||
}
|
||||
return .nanoseconds(readyTime - TimeAmount.Value(t.uptimeNanoseconds))
|
||||
return readyTime - t
|
||||
}
|
||||
|
||||
func fail(_ error: Error) {
|
||||
|
||||
Reference in new issue
Block a user