diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index b8426b30c..dfa0edcb9 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -57,9 +57,9 @@ public final class RepeatedTask { private let delay: TimeAmount private let eventLoop: EventLoop private var scheduled: Scheduled>? - private var task: (() -> EventLoopFuture)? + private var task: ((RepeatedTask) -> EventLoopFuture)? - internal init(interval: TimeAmount, eventLoop: EventLoop, task: @escaping () -> EventLoopFuture) { + internal init(interval: TimeAmount, eventLoop: EventLoop, task: @escaping (RepeatedTask) -> EventLoopFuture) { self.delay = interval self.eventLoop = eventLoop self.task = task @@ -80,7 +80,9 @@ public final class RepeatedTask { guard let task = self.task else { return } - self.scheduled = eventLoop.scheduleTask(in: delay, task) + self.scheduled = eventLoop.scheduleTask(in: delay) { + task(self) + } self.reschedule() } @@ -88,6 +90,8 @@ public final class RepeatedTask { /// /// Whether the execution of the task is immediately canceled depends on whether the execution of a task has already begun. /// This means immediate cancellation is not guaranteed. + /// + /// The safest way to cancel is by using the passed reference of `RepeatedTask` inside the task closure. public func cancel() { if self.eventLoop.inEventLoop { self.cancel0() @@ -116,6 +120,10 @@ public final class RepeatedTask { self.reschedule0() } } + + scheduled.futureResult.whenFailure { (_: Error) in + self.cancel0() + } } private func reschedule0() { @@ -123,7 +131,9 @@ public final class RepeatedTask { guard let task = self.task else { return } - self.scheduled = self.eventLoop.scheduleTask(in: self.delay, task) + self.scheduled = self.eventLoop.scheduleTask(in: self.delay) { + task(self) + } self.reschedule() } } @@ -308,10 +318,10 @@ extension EventLoop { /// - task: The closure that will be executed. /// - return: `RepeatedTask` @discardableResult - public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping () throws -> Void) -> RepeatedTask { - let futureTask: () -> EventLoopFuture = { + public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) throws -> Void) -> RepeatedTask { + let futureTask: (RepeatedTask) -> EventLoopFuture = { repeatedTask in do { - try task() + try task(repeatedTask) return self.newSucceededFuture(result: ()) } catch { return self.newFailedFuture(error: error) @@ -328,7 +338,7 @@ extension EventLoop { /// - task: The closure that will be executed. /// - return: `RepeatedTask` @discardableResult - public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping () -> EventLoopFuture) -> RepeatedTask { + public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) -> EventLoopFuture) -> RepeatedTask { let repeated = RepeatedTask(interval: delay, eventLoop: self, task: task) repeated.begin(in: initialDelay) return repeated diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index 43bf4f9c5..e54846a7f 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -103,28 +103,15 @@ public class EventLoopTest : XCTestCase { let expect = expectation(description: "Is cancelling RepatedTask") let counter = Atomic(value: 0) - let lock = Lock() - var repeatedTask: RepeatedTask? = nil // protected by `lock` - let task = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in + eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { repeatedTask -> Void in if counter.load() == 0 { XCTAssertTrue(DispatchTime.now().uptimeNanoseconds - nanos >= initialDelay.nanoseconds) } else if counter.load() == count { expect.fulfill() - while true { - let mTask = lock.withLock { repeatedTask } - guard let task = mTask else { - usleep(10_000) - continue - } - task.cancel() - break - } + repeatedTask.cancel() } counter.store(counter.load() + 1) } - lock.withLock { - repeatedTask = task - } waitForExpectations(timeout: 1) { _ in XCTAssertEqual(counter.load(), count + 1) @@ -144,7 +131,7 @@ public class EventLoopTest : XCTestCase { let expect = expectation(description: "Is cancelling RepatedTask") let group = DispatchGroup() group.enter() - let repeatedTask = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in + let repeatedTask = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { (_: RepeatedTask) -> Void in group.leave() expect.fulfill() } @@ -164,7 +151,7 @@ public class EventLoopTest : XCTestCase { var repeated: RepeatedTask? weak var weakRepeated: RepeatedTask? - repeated = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in } + repeated = eventLoopGroup.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { (_: RepeatedTask) -> Void in } weakRepeated = repeated XCTAssertNotNil(weakRepeated) repeated?.cancel() @@ -179,7 +166,7 @@ public class EventLoopTest : XCTestCase { var eventLoopGroup: EventLoopGroup? = MultiThreadedEventLoopGroup(numberOfThreads: 1) weak var weakEventLoop = eventLoopGroup?.next() - eventLoopGroup?.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { () -> Void in } + eventLoopGroup?.next().scheduleRepeatedTask(initialDelay: initialDelay, delay: delay) { (_: RepeatedTask) -> Void in } XCTAssertNoThrow(try eventLoopGroup?.syncShutdownGracefully()) XCTAssertNotNil(weakEventLoop) eventLoopGroup = nil