Pass RepeatedTask to its task closure (#515)

Pass the reference to RepeatedTask to its closure to allow a thread-safe cancelling.

Motivation:

With the current implementation, one has to create a reference and use a lock to thread-safely cancel the RepeatedTask. This makes the use more difficult and error-prone.

Modifications:

Added a RepeatedTask parameter to the scheduleRepeatedTask closure.

Result:

It is now possible to cancel the RepeatedTask from within the task closure.
This commit is contained in:
Franz Busch authored and Cory Benfield committed 2018-07-17 16:07:55 +01:00
1 parent 6a90e6a682
commit ae13d135f7
2 files changed
+23 -26

No files matched your search

+18 -8
View File
@@ -57,9 +57,9 @@ public final class RepeatedTask {
private let delay: TimeAmount
private let eventLoop: EventLoop
private var scheduled: Scheduled<EventLoopFuture<Void>>?
private var task: (() -> EventLoopFuture<Void>)?
private var task: ((RepeatedTask) -> EventLoopFuture<Void>)?
internal init(interval: TimeAmount, eventLoop: EventLoop, task: @escaping () -> EventLoopFuture<Void>) {
internal init(interval: TimeAmount, eventLoop: EventLoop, task: @escaping (RepeatedTask) -> EventLoopFuture<Void>) {
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<Void> = {
public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) throws -> Void) -> RepeatedTask {
let futureTask: (RepeatedTask) -> EventLoopFuture<Void> = { 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<Void>) -> RepeatedTask {
public func scheduleRepeatedTask(initialDelay: TimeAmount, delay: TimeAmount, _ task: @escaping (RepeatedTask) -> EventLoopFuture<Void>) -> RepeatedTask {
let repeated = RepeatedTask(interval: delay, eventLoop: self, task: task)
repeated.begin(in: initialDelay)
return repeated
+5 -18
View File
@@ -103,28 +103,15 @@ public class EventLoopTest : XCTestCase {
let expect = expectation(description: "Is cancelling RepatedTask")
let counter = Atomic<Int>(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