make ELF.cascade accept an optional promise (#757)

* Make EventLoopFuture.cascade and cascadeFailure accept an optional promise

Motivation:

fixes #756

Modifications:

Change EventLoopFuture.cascade to func cascade(promise: EventLoopPromise<T>?)

Result:

EventLoopFuture.cascade can be called without needing to check if the promise is nil
This commit is contained in:
David Skrundz 2019-01-19 06:59:12 -07:00 committed by Johannes Weiss
parent f50230aeba
commit f7357e66ab
5 changed files with 11 additions and 21 deletions

View File

@ -802,9 +802,7 @@ class BaseSocketChannel<T: BaseSocket>: SelectableChannel, ChannelCore {
registerPromise.futureResult.whenFailure { (_: Error) in registerPromise.futureResult.whenFailure { (_: Error) in
self.close(promise: nil) self.close(promise: nil)
} }
if let promise = promise {
registerPromise.futureResult.cascadeFailure(promise: promise) registerPromise.futureResult.cascadeFailure(promise: promise)
}
if self.lifecycleManager.isPreRegistered { if self.lifecycleManager.isPreRegistered {
// we expect kqueue/epoll registration to always succeed which is basically true, except for errors that // we expect kqueue/epoll registration to always succeed which is basically true, except for errors that

View File

@ -395,10 +395,8 @@ public final class ChannelPipeline: ChannelInvoker {
self.remove0(ctx: ctx, promise: promise) self.remove0(ctx: ctx, promise: promise)
} }
if let promise = promise {
contextFuture.cascadeFailure(promise: promise) contextFuture.cascadeFailure(promise: promise)
} }
}
/// Remove a `ChannelHandler` from the `ChannelPipeline`. /// Remove a `ChannelHandler` from the `ChannelPipeline`.
/// ///
@ -412,10 +410,8 @@ public final class ChannelPipeline: ChannelInvoker {
self.remove0(ctx: ctx, promise: promise) self.remove0(ctx: ctx, promise: promise)
} }
if let promise = promise {
contextFuture.cascadeFailure(promise: promise) contextFuture.cascadeFailure(promise: promise)
} }
}
/// Remove a `ChannelHandler` from the `ChannelPipeline`. /// Remove a `ChannelHandler` from the `ChannelPipeline`.
/// ///

View File

@ -749,7 +749,8 @@ extension EventLoopFuture {
/// ///
/// - parameters: /// - parameters:
/// - promise: The `EventLoopPromise` to fulfill with the results of this future. /// - promise: The `EventLoopPromise` to fulfill with the results of this future.
public func cascade(promise: EventLoopPromise<T>) { public func cascade(promise: EventLoopPromise<T>?) {
guard let promise = promise else { return }
_whenCompleteWithValue { v in _whenCompleteWithValue { v in
switch v { switch v {
case .failure(let err): case .failure(let err):
@ -769,7 +770,8 @@ extension EventLoopFuture {
/// ///
/// - parameters: /// - parameters:
/// - promise: The `EventLoopPromise` to fulfill with the results of this future. /// - promise: The `EventLoopPromise` to fulfill with the results of this future.
public func cascadeFailure<U>(promise: EventLoopPromise<U>) { public func cascadeFailure<U>(promise: EventLoopPromise<U>?) {
guard let promise = promise else { return }
self.whenFailure { err in self.whenFailure { err in
promise.fail(error: err) promise.fail(error: err)
} }

View File

@ -124,11 +124,11 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler {
responseHead.headers.replaceOrAdd(name: "Content-Encoding", value: algorithm!.rawValue) responseHead.headers.replaceOrAdd(name: "Content-Encoding", value: algorithm!.rawValue)
initializeEncoder(encoding: algorithm!) initializeEncoder(encoding: algorithm!)
pendingResponse.bufferResponseHead(responseHead) pendingResponse.bufferResponseHead(responseHead)
chainPromise(promise) pendingWritePromise.futureResult.cascade(promise: promise)
case .body(let body): case .body(let body):
if algorithm != nil { if algorithm != nil {
pendingResponse.bufferBodyPart(body) pendingResponse.bufferBodyPart(body)
chainPromise(promise) pendingWritePromise.futureResult.cascade(promise: promise)
} else { } else {
ctx.write(data, promise: promise) ctx.write(data, promise: promise)
} }
@ -141,7 +141,7 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler {
} }
pendingResponse.bufferResponseEnd(httpData) pendingResponse.bufferResponseEnd(httpData)
chainPromise(promise) pendingWritePromise.futureResult.cascade(promise: promise)
emitPendingWrites(ctx: ctx) emitPendingWrites(ctx: ctx)
algorithm = nil algorithm = nil
deinitializeEncoder() deinitializeEncoder()
@ -212,12 +212,6 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler {
deflateEnd(&stream) deflateEnd(&stream)
} }
private func chainPromise(_ promise: EventLoopPromise<Void>?) {
if let promise = promise {
pendingWritePromise.futureResult.cascade(promise: promise)
}
}
/// Emits all pending buffered writes to the network, optionally compressing the /// Emits all pending buffered writes to the network, optionally compressing the
/// data. Resets the pending write buffer and promise. /// data. Resets the pending write buffer and promise.
/// ///

View File

@ -315,7 +315,7 @@ public final class HTTPServerPipelineHandler: ChannelDuplexHandler {
case .quiescingLastRequestEndReceived: case .quiescingLastRequestEndReceived:
ctx.write(data).then { ctx.write(data).then {
ctx.close() ctx.close()
}.cascade(promise: promise ?? ctx.channel.eventLoop.makePromise()) }.cascade(promise: promise)
case .acceptingEvents, .quiescingWaitingForRequestEnd: case .acceptingEvents, .quiescingWaitingForRequestEnd:
ctx.write(data, promise: promise) ctx.write(data, promise: promise)
} }