Skip to content

Commit

Permalink
Add async implementation of EventLoopGroup.shutdownGracefully to _NIO…
Browse files Browse the repository at this point in the history
…Concurrency (#1879)

* Add async implementation of EventLoopGroup.shutdownGracefully to _NIOConcurrency

Signed-off-by: Si Beaumont <[email protected]>

* fixup: Move elg lifecycle into async function in async demo

* fixup: Ensure continuation is run exactly once

* fixup: Shutdown event loop group in catch in async demo
  • Loading branch information
simonjbeaumont authored Jun 14, 2021
1 parent a1f4052 commit 7f3a2f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
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

0 comments on commit 7f3a2f5

Please sign in to comment.