Some cleanliness changes. (#7)
* Use DispatchWorkItem.cancel() for cancellation. Make StateManagedChannel.beginActivating() use the same methodology as all the others. * A needless `if let x = opt` that could be replaced with a question mark.
This commit is contained in:
parent
fb47fc1614
commit
e14a834008
|
|
@ -105,19 +105,19 @@ internal class NIOTSEventLoop: QoSEventLoop {
|
||||||
return Scheduled(promise: p, cancellationTask: { } )
|
return Scheduled(promise: p, cancellationTask: { } )
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch has no support for cancellation, so instead we synchronize over this nice variable.
|
// Dispatch support for cancellation exists at the work-item level, so we explicitly create one here.
|
||||||
var cancelled = false
|
// We set the QoS on this work item and explicitly enforce it when the block runs.
|
||||||
|
let workItem = DispatchWorkItem(qos: qos, flags: .enforceQoS) {
|
||||||
self.taskQueue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds + UInt64(time.nanoseconds)), qos: qos) {
|
|
||||||
guard !cancelled else { return }
|
|
||||||
do {
|
do {
|
||||||
p.succeed(result: try task())
|
p.succeed(result: try task())
|
||||||
} catch {
|
} catch {
|
||||||
p.fail(error: error)
|
p.fail(error: error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.taskQueue.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: DispatchTime.now().uptimeNanoseconds + UInt64(time.nanoseconds)), execute: workItem)
|
||||||
|
|
||||||
return Scheduled(promise: p, cancellationTask: { self.taskQueue.async { cancelled = true } })
|
return Scheduled(promise: p, cancellationTask: { workItem.cancel() })
|
||||||
}
|
}
|
||||||
|
|
||||||
public func shutdownGracefully(queue: DispatchQueue, _ callback: @escaping (Error?) -> Void) {
|
public func shutdownGracefully(queue: DispatchQueue, _ callback: @escaping (Error?) -> Void) {
|
||||||
|
|
|
||||||
|
|
@ -46,12 +46,10 @@ internal enum ChannelState<ActiveSubstate: ActiveChannelSubstate> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate mutating func beginActivating() throws {
|
fileprivate mutating func beginActivating() throws {
|
||||||
switch self {
|
guard case .registered = self else {
|
||||||
case .registered:
|
|
||||||
self = .activating
|
|
||||||
case .idle, .activating, .active, .inactive:
|
|
||||||
throw NIOTSErrors.InvalidChannelStateTransition()
|
throw NIOTSErrors.InvalidChannelStateTransition()
|
||||||
}
|
}
|
||||||
|
self = .activating
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate mutating func becomeActive() throws {
|
fileprivate mutating func becomeActive() throws {
|
||||||
|
|
@ -242,9 +240,7 @@ extension StateManagedChannel {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let promise = promise {
|
promise?.succeed(result: ())
|
||||||
promise.succeed(result: ())
|
|
||||||
}
|
|
||||||
self.pipeline.fireChannelActive()
|
self.pipeline.fireChannelActive()
|
||||||
self.readIfNeeded0()
|
self.readIfNeeded0()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue