Skip to content
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

Add async implementation of EventLoopGroup.shutdownGracefully to _NIOConcurrency #1879

Merged
merged 4 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Sources/NIOAsyncAwaitDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ import Dispatch

import _Concurrency

let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
func makeHTTPChannel(host: String, port: Int) async throws -> AsyncChannelIO<HTTPRequestHead, NIOHTTPClientResponseFull> {
func makeHTTPChannel(host: String, port: Int, group: EventLoopGroup) async throws -> AsyncChannelIO<HTTPRequestHead, NIOHTTPClientResponseFull> {
let channel = try await ClientBootstrap(group: group).connect(host: host, port: port).get()
try await channel.pipeline.addHTTPClientHandlers().get()
try await channel.pipeline.addHandler(NIOHTTPClientResponseAggregator(maxContentLength: 1_000_000))
Expand All @@ -38,8 +33,9 @@ func makeHTTPChannel(host: String, port: Int) async throws -> AsyncChannelIO<HTT

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
func main() async {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
do {
let channel = try await makeHTTPChannel(host: "httpbin.org", port: 80)
let channel = try await makeHTTPChannel(host: "httpbin.org", port: 80, group: group)
print("OK, connected to \(channel)")

print("Sending request 1", terminator: "")
Expand All @@ -57,9 +53,19 @@ func main() async {
print(", response:", String(buffer: response2.body ?? ByteBuffer()))

try await channel.close()

print("Shutting down event loop group...")
try await group.shutdownGracefully()

print("all, done")
} catch {
print("ERROR: \(error)")
print("Shutting down event loop group (possibly for a second time)...")
do {
try await group.shutdownGracefully()
} catch {
print("Error shutting down event loop group: \(error)")
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions Sources/_NIOConcurrency/AsyncAwaitSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ extension EventLoopFuture {
}
}

extension EventLoopGroup {
/// Shuts down the event loop gracefully.
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
@inlinable
public func shutdownGracefully() async throws {
return try await withCheckedThrowingContinuation { cont in
self.shutdownGracefully { error in
if let error = error {
cont.resume(throwing: error)
} else {
cont.resume()
}
}
}
}
}

extension EventLoopPromise {
/// Complete a future with the result (or error) of the `async` function `body`.
///
Expand Down