Fork Future-Swift and adjust it to ensure callback execution is always done on the EventLoop.

- Also remove all methods that use Dispatch for now as we not need these
- Ensure we will detect if the user calls a blocking method while on the EventLoop and crash to guard against deadlock
This commit is contained in:
Norman Maurer committed 2017-06-12 13:44:10 +02:00
1 parent c0a2810840
commit 58d0883b2d
13 files changed
+322 -622

No files matched your search

-1
View File
@@ -14,7 +14,6 @@
import Foundation
import Sockets
import Future
public final class ServerBootstrap {
+6 -14
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import Future
import Sockets
#if os(Linux)
@@ -553,12 +552,6 @@ public protocol ChannelCore : class{
var eventLoop: EventLoop { get }
}
extension ChannelCore {
public func close0(promise: Promise<Void> = Promise<Void>()) {
close0(promise: promise, error: ChannelError.closed)
}
}
/*
All methods exposed by Channel are thread-safe
*/
@@ -914,7 +907,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
}
}
public final func close0(promise: Promise<Void> = Promise<Void>(), error: Error = ChannelError.closed) {
public final func close0(promise: Promise<Void>, error: Error) {
assert(eventLoop.inEventLoop)
guard !closed else {
@@ -1037,8 +1030,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
// Call before triggering the close of the Channel.
pipeline.fireChannelReadComplete0()
close0(error: err)
close0(promise: eventLoop.newPromise(type: Void.self), error: err)
return
@@ -1094,7 +1086,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
try selectableEventLoop.deregister(channel: self)
} catch let err {
pipeline.fireErrorCaught0(error: err)
close0(error: err)
close0(promise: eventLoop.newPromise(type: Void.self), error: err)
}
}
@@ -1112,7 +1104,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
try selectableEventLoop.reregister(channel: self)
} catch let err {
pipeline.fireErrorCaught0(error: err)
close0(error: err)
close0(promise: eventLoop.newPromise(type: Void.self), error: err)
}
}
@@ -1128,7 +1120,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
return true
} catch let err {
pipeline.fireErrorCaught0(error: err)
close0(error: err)
close0(promise: eventLoop.newPromise(type: Void.self), error: err)
return false
}
}
@@ -1152,7 +1144,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore {
}
} catch let err {
close0(error: err)
close0(promise: eventLoop.newPromise(type: Void.self), error: err)
// we handled all writes
return true
-1
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import Future
import Sockets
public protocol ChannelHandler : class {
-1
View File
@@ -16,7 +16,6 @@
//
import Foundation
import Future
/**
ChannelHandler implementation which enforces back-pressure by stop reading from the remote-peer when it can not write back fast-enough and start reading again
-1
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import Future
import Sockets
public protocol OutboundData { }
+3 -4
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import Future
import Sockets
@@ -73,7 +72,7 @@ public final class ChannelPipeline : ChannelInboundInvoker {
}
public func remove(handler: ChannelHandler) -> Future<Bool> {
let promise = Promise<Bool>()
let promise = eventLoop.newPromise(type: Bool.self)
if eventLoop.inEventLoop {
remove0(handler: handler, promise: promise)
} else {
@@ -112,7 +111,7 @@ public final class ChannelPipeline : ChannelInboundInvoker {
}
public func remove(name: String) -> Future<Bool> {
let promise = Promise<Bool>()
let promise = eventLoop.newPromise(type: Bool.self)
if eventLoop.inEventLoop {
remove0(name: name, promise: promise)
} else {
@@ -456,7 +455,7 @@ private final class HeadChannelHandler : ChannelHandler {
}
func close(ctx: ChannelHandlerContext, promise: Promise<Void>) {
ctx.channel!._unsafe.close0(promise: promise)
ctx.channel!._unsafe.close0(promise: promise, error: ChannelError.closed)
}
func read(ctx: ChannelHandlerContext) {
-15
View File
@@ -1,15 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
+23 -2
View File
@@ -15,7 +15,6 @@
import Foundation
import Dispatch
import Sockets
import Future
class EmbeddedEventLoop : EventLoop {
@@ -26,6 +25,24 @@ class EmbeddedEventLoop : EventLoop {
// Would be better to have this as a Queue
var tasks: [() -> ()] = Array()
public func submit<T>(task: @escaping () throws-> (T)) -> Future<T> {
let promise = Promise<T>(eventLoop: self, checkForPossibleDeadlock: false)
execute(task: {() -> () in
do {
promise.succeed(result: try task())
} catch let err {
promise.fail(error: err)
}
})
return promise.futureResult
}
public func newPromise<T>(type: T.Type) -> Promise<T> {
return Promise<T>(eventLoop: self, checkForPossibleDeadlock: false)
}
// We're not really running a loop here. Tasks aren't run until run() is called,
// at which point we run everything that's been submitted. Anything newly submitted
// either gets on that train if it's still moving or
@@ -59,10 +76,14 @@ class EmbeddedEventLoop : EventLoop {
class EmbeddedChannelCore : ChannelCore {
var closed: Bool { return closePromise.futureResult.fulfilled }
var closePromise: Promise<Void> = Promise<Void>()
var eventLoop: EventLoop = EmbeddedEventLoop()
var closePromise: Promise<Void>
init() {
closePromise = eventLoop.newPromise(type: Void.self)
}
deinit { closePromise.succeed(result: ()) }
var outboundBuffer: [Any] = []
+5 -3
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import Future
import Sockets
import ConcurrencyHelpers
@@ -21,11 +20,14 @@ public protocol EventLoop: EventLoopGroup {
var inEventLoop: Bool { get }
func execute(task: @escaping () -> ())
func submit<T>(task: @escaping () throws-> (T)) -> Future<T>
func newPromise<T>(type: T.Type) -> Promise<T>
func newFailedFuture<T>(type: T.Type, error: Error) -> Future<T>
func newSucceedFuture<T>(result: T) -> Future<T>
}
extension EventLoop {
public func submit<T>(task: @escaping () throws-> (T)) -> Future<T> {
let promise = Promise<T>()
let promise = Promise<T>(eventLoop: self, checkForPossibleDeadlock: true)
execute(task: {() -> () in
do {
@@ -39,7 +41,7 @@ extension EventLoop {
}
public func newPromise<T>(type: T.Type) -> Promise<T> {
return Promise<T>()
return Promise<T>(eventLoop: self, checkForPossibleDeadlock: true)
}
public func newFailedFuture<T>(type: T.Type, error: Error) -> Future<T> {
+280 -550
View File
File diff suppressed because it is too large. Load diff
-25
View File
@@ -1,25 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import Future
extension Future {
public func cascadeFailure<T>(promise: Promise<T>) {
self.whenFailure(callback: { err in
promise.fail(error: err)
})
}
}
-1
View File
@@ -13,7 +13,6 @@
//===----------------------------------------------------------------------===//
import Foundation
import NIO
import Future
public class EchoHandler: ChannelHandler {
+5 -4
View File
@@ -19,30 +19,31 @@
import Foundation
import XCTest
import Future
import Sockets
@testable import NIO
class ChannelWriteTests: XCTestCase {
func testWriteVIsTailRecursiveAndLoopifiedInReleaseMode() throws {
if !_isDebugAssertConfiguration() {
let eventLoop = EmbeddedEventLoop()
let numberOfWritesToSmashTheStackIfNotTurnedIntoALoop = 500_000
let allocator = ByteBufferAllocator()
let d = "Hello".data(using: .utf8)!
let head: PendingWrite? = PendingWrite(buffer: ByteBuffer(allocator: allocator, data: d, offset: 0, length: 5, maxCapacity: 5),
promise: Promise())
promise: eventLoop.newPromise(type: Void.self))
var tail: PendingWrite? = head
for i in 0..<(numberOfWritesToSmashTheStackIfNotTurnedIntoALoop) {
let len = (i % 5)+1
let new = PendingWrite(buffer: ByteBuffer(allocator: allocator, data: d, offset: 0, length: len, maxCapacity: 2*len),
promise: Promise())
promise: eventLoop.newPromise(type: Void.self))
tail!.next = new
tail = new
}
/* add a bogus one to check that the limit we pass in `count` later is respected. */
tail!.next = PendingWrite(buffer: ByteBuffer(allocator: allocator, data: "BOGUS".data(using: .utf8)!, offset: 0, length: 5, maxCapacity: 5),
promise: Promise())
promise: eventLoop.newPromise(type: Void.self))
var iovecs_count = numberOfWritesToSmashTheStackIfNotTurnedIntoALoop + 1 /* doesn't include the bogus one */
var iovecs: UnsafeMutablePointer<IOVector> = UnsafeMutablePointer.allocate(capacity: iovecs_count)
defer {