New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix EventLoops that Bootstrap channel initialiser's call out on #424
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,18 +205,22 @@ public final class ServerBootstrap { | |
return eventLoop.newFailedFuture(error: error) | ||
} | ||
|
||
return serverChannelInit(serverChannel).then { | ||
serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit, | ||
childChannelOptions: childChannelOptions)) | ||
}.then { | ||
serverChannelOptions.applyAll(channel: serverChannel) | ||
return eventLoop.submit { | ||
return serverChannelInit(serverChannel).then { | ||
serverChannel.pipeline.add(handler: AcceptHandler(childChannelInitializer: childChannelInit, | ||
childChannelOptions: childChannelOptions)) | ||
}.then { | ||
serverChannelOptions.applyAll(channel: serverChannel) | ||
}.then { | ||
register(eventLoop, serverChannel) | ||
}.map { | ||
serverChannel as Channel | ||
}.thenIfError { error in | ||
serverChannel.close0(error: error, mode: .all, promise: nil) | ||
return eventLoop.newFailedFuture(error: error) | ||
} | ||
}.then { | ||
register(eventLoop, serverChannel) | ||
}.map { | ||
serverChannel | ||
}.thenIfError { error in | ||
serverChannel.close0(error: error, mode: .all, promise: nil) | ||
return eventLoop.newFailedFuture(error: error) | ||
$0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is annoying: see #426. Fixing it is not part of this PR so I won't block on this. |
||
} | ||
} | ||
|
||
|
@@ -240,23 +244,42 @@ public final class ServerBootstrap { | |
|
||
func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { | ||
let accepted = self.unwrapInboundIn(data) | ||
let childChannelInit = self.childChannelInit ?? { (_: Channel) in ctx.eventLoop.newSucceededFuture(result: ()) } | ||
|
||
self.childChannelOptions.applyAll(channel: accepted).hopTo(eventLoop: ctx.eventLoop).then { | ||
assert(ctx.eventLoop.inEventLoop) | ||
return childChannelInit(accepted) | ||
}.then { () -> EventLoopFuture<Void> in | ||
assert(ctx.eventLoop.inEventLoop) | ||
guard !ctx.pipeline.destroyed else { | ||
return accepted.close().thenThrowing { | ||
throw ChannelError.ioOnClosedChannel | ||
let ctxEventLoop = ctx.eventLoop | ||
let childEventLoop = accepted.eventLoop | ||
let childChannelInit = self.childChannelInit ?? { (_: Channel) in childEventLoop.newSucceededFuture(result: ()) } | ||
|
||
@inline(__always) | ||
func setupChildChannel() -> EventLoopFuture<Void> { | ||
return self.childChannelOptions.applyAll(channel: accepted).then { () -> EventLoopFuture<Void> in | ||
assert(childEventLoop.inEventLoop) | ||
return childChannelInit(accepted) | ||
} | ||
} | ||
|
||
@inline(__always) | ||
func fireThroughPipeline(_ future: EventLoopFuture<Void>) { | ||
assert(ctxEventLoop.inEventLoop) | ||
future.then { (_) -> EventLoopFuture<Void> in | ||
assert(ctxEventLoop.inEventLoop) | ||
guard !ctx.pipeline.destroyed else { | ||
return accepted.close().thenThrowing { | ||
throw ChannelError.ioOnClosedChannel | ||
} | ||
} | ||
ctx.fireChannelRead(data) | ||
return ctx.eventLoop.newSucceededFuture(result: ()) | ||
}.whenFailure { error in | ||
assert(ctx.eventLoop.inEventLoop) | ||
self.closeAndFire(ctx: ctx, accepted: accepted, err: error) | ||
} | ||
ctx.fireChannelRead(data) | ||
return ctx.eventLoop.newSucceededFuture(result: ()) | ||
}.whenFailure { error in | ||
assert(ctx.eventLoop.inEventLoop) | ||
self.closeAndFire(ctx: ctx, accepted: accepted, err: error) | ||
} | ||
|
||
if childEventLoop === ctxEventLoop { | ||
fireThroughPipeline(setupChildChannel()) | ||
} else { | ||
fireThroughPipeline(childEventLoop.submit { | ||
return setupChildChannel() | ||
}.then { $0 }.hopTo(eventLoop: ctxEventLoop)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, see #426. |
||
} | ||
} | ||
|
||
|
@@ -463,18 +486,27 @@ public final class ClientBootstrap { | |
return promise.futureResult | ||
} | ||
|
||
channelInitializer(channel).then { | ||
channelOptions.applyAll(channel: channel) | ||
}.then { | ||
channel.registerAndDoSynchronously(body) | ||
}.map { | ||
channel | ||
}.thenIfError { error in | ||
channel.close0(error: error, mode: .all, promise: nil) | ||
return channel.eventLoop.newFailedFuture(error: error) | ||
}.cascade(promise: promise) | ||
@inline(__always) | ||
func setupChannel() -> EventLoopFuture<Channel> { | ||
assert(eventLoop.inEventLoop) | ||
channelInitializer(channel).then { | ||
channelOptions.applyAll(channel: channel) | ||
}.then { | ||
channel.registerAndDoSynchronously(body) | ||
}.map { | ||
channel | ||
}.thenIfError { error in | ||
channel.close0(error: error, mode: .all, promise: nil) | ||
return channel.eventLoop.newFailedFuture(error: error) | ||
}.cascade(promise: promise) | ||
return promise.futureResult | ||
} | ||
|
||
return promise.futureResult | ||
if eventLoop.inEventLoop { | ||
return setupChannel() | ||
} else { | ||
return eventLoop.submit(setupChannel).then { $0 } | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// BootstrapTest+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension BootstrapTest { | ||
|
||
static var allTests : [(String, (BootstrapTest) -> () throws -> Void)] { | ||
return [ | ||
("testBootstrapsCallInitializersOnCorrectEventLoop", testBootstrapsCallInitializersOnCorrectEventLoop), | ||
] | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 NIO | ||
import XCTest | ||
|
||
class BootstrapTest: XCTestCase { | ||
func testBootstrapsCallInitializersOnCorrectEventLoop() throws { | ||
for numThreads in [1 /* everything on one event loop */, | ||
2 /* some stuff has shared event loops */, | ||
5 /* everything on a different event loop */] { | ||
let group = MultiThreadedEventLoopGroup(numThreads: numThreads) | ||
defer { | ||
XCTAssertNoThrow(try group.syncShutdownGracefully()) | ||
} | ||
|
||
let childChannelDone: EventLoopPromise<Void> = group.next().newPromise() | ||
let serverChannelDone: EventLoopPromise<Void> = group.next().newPromise() | ||
let serverChannel = try ServerBootstrap(group: group) | ||
.childChannelInitializer { channel in | ||
XCTAssert(channel.eventLoop.inEventLoop) | ||
childChannelDone.succeed(result: ()) | ||
return channel.eventLoop.newSucceededFuture(result: ()) | ||
} | ||
.serverChannelInitializer { channel in | ||
XCTAssert(channel.eventLoop.inEventLoop) | ||
serverChannelDone.succeed(result: ()) | ||
return channel.eventLoop.newSucceededFuture(result: ()) | ||
} | ||
.bind(host: "localhost", port: 0) | ||
.wait() | ||
defer { | ||
XCTAssertNoThrow(try serverChannel.close().wait()) | ||
} | ||
|
||
let client = try ClientBootstrap(group: group) | ||
.channelInitializer { channel in | ||
XCTAssert(channel.eventLoop.inEventLoop) | ||
return channel.eventLoop.newSucceededFuture(result: ()) | ||
} | ||
.connect(to: serverChannel.localAddress!) | ||
.wait() | ||
defer { | ||
XCTAssertNoThrow(try client.syncCloseAcceptingAlreadyClosed()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@weissi why we need this ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@normanmaurer because otherwise the compiler complains can't do
EventLoopFuture<ServerSocketChannel>
asEventLoopFuture<Channel>