mirror of
https://github.com/apple/swift-nio
synced 2026-11-26 04:57:49 +00:00
* Land `NIOAsyncChannel` as SPI # Motivation We want to provide bridges from NIO `Channel`s to Swift Concurrency. In previous PRs, we already landed the building blocks namely `NIOAsyncSequenceProducer` and `NIOAsyncWriter`. These two types are highly performant bridges between synchronous and asynchronous code that respect back-pressure. The next step is to build convenience methods that wrap a `Channel` with these two types. # Modification This PR adds a new type called `NIOAsyncChannel` that is capable of wrapping a `Channel`. This is done by adding two handlers to the channel pipeline that are bridging to the `NIOAsyncSequenceProducer` and `NIOAsyncWriter`. The new `NIOAsyncChannel` type exposes three properties. The underlying `Channel`, a `NIOAsyncChannelInboundStream` and a `NIOAsyncChannelOutboundWriter`. Using these three types the user a able to read/write into the channel using `async` methods. Importantly, we are landing all of this behind the `@_spi(AsyncChannel`. This allows us to merge PRs while we are still working on the remaining parts such as protocol negotiation. # Result We have the first part necessary for our async bridges. Follow up PRs will include the following things: 1. Bootstrap support 2. Protocol negotiation support 3. Example with documentation * Add AsyncSequence bridge to NIOAsyncChannelOutboundWriter * Code review * Prefix temporary spi public method * Rename writeAndFlush to write
253 lines
7.0 KiB
Swift
253 lines
7.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2022-2023 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#if swift(>=5.6)
|
|
/// A ``ChannelHandler`` that is used to transform the inbound portion of a NIO
|
|
/// ``Channel`` into an asynchronous sequence that supports back-pressure.
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
@usableFromInline
|
|
internal final class NIOAsyncChannelInboundStreamChannelHandler<InboundIn: Sendable>: ChannelDuplexHandler {
|
|
@usableFromInline
|
|
enum _ProducingState {
|
|
// Not .stopProducing
|
|
case keepProducing
|
|
|
|
// .stopProducing but not read()
|
|
case producingPaused
|
|
|
|
// .stopProducing and read()
|
|
case producingPausedWithOutstandingRead
|
|
}
|
|
|
|
@usableFromInline
|
|
typealias OutboundIn = Any
|
|
|
|
@usableFromInline
|
|
typealias OutboundOut = Any
|
|
|
|
@usableFromInline
|
|
typealias Source = NIOThrowingAsyncSequenceProducer<
|
|
InboundIn,
|
|
Error,
|
|
NIOAsyncSequenceProducerBackPressureStrategies.HighLowWatermark,
|
|
NIOAsyncChannelInboundStreamChannelHandler<InboundIn>.Delegate
|
|
>.Source
|
|
|
|
/// The source of the asynchronous sequence.
|
|
@usableFromInline
|
|
var source: Source?
|
|
|
|
/// The channel handler's context.
|
|
@usableFromInline
|
|
var context: ChannelHandlerContext?
|
|
|
|
/// An array of reads which will be yielded to the source with the next channel read complete.
|
|
@usableFromInline
|
|
var buffer: [InboundIn] = []
|
|
|
|
/// The current producing state.
|
|
@usableFromInline
|
|
var producingState: _ProducingState = .keepProducing
|
|
|
|
/// The event loop.
|
|
@usableFromInline
|
|
let eventLoop: EventLoop
|
|
|
|
/// The shared `CloseRatchet` between this handler and the writer handler.
|
|
@usableFromInline
|
|
let closeRatchet: CloseRatchet
|
|
|
|
@inlinable
|
|
init(eventLoop: EventLoop, closeRatchet: CloseRatchet) {
|
|
self.eventLoop = eventLoop
|
|
self.closeRatchet = closeRatchet
|
|
}
|
|
|
|
@inlinable
|
|
func handlerAdded(context: ChannelHandlerContext) {
|
|
self.context = context
|
|
}
|
|
|
|
@inlinable
|
|
func handlerRemoved(context: ChannelHandlerContext) {
|
|
self._finishSource(context: context)
|
|
self.context = nil
|
|
}
|
|
|
|
@inlinable
|
|
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
self.buffer.append(self.unwrapInboundIn(data))
|
|
|
|
// We forward on reads here to enable better channel composition.
|
|
context.fireChannelRead(data)
|
|
}
|
|
|
|
@inlinable
|
|
func channelReadComplete(context: ChannelHandlerContext) {
|
|
self._deliverReads(context: context)
|
|
context.fireChannelReadComplete()
|
|
}
|
|
|
|
@inlinable
|
|
func channelInactive(context: ChannelHandlerContext) {
|
|
self._finishSource(context: context)
|
|
context.fireChannelInactive()
|
|
}
|
|
|
|
@inlinable
|
|
func errorCaught(context: ChannelHandlerContext, error: Error) {
|
|
self._finishSource(with: error, context: context)
|
|
context.fireErrorCaught(error)
|
|
}
|
|
|
|
@inlinable
|
|
func read(context: ChannelHandlerContext) {
|
|
switch self.producingState {
|
|
case .keepProducing:
|
|
context.read()
|
|
case .producingPaused:
|
|
self.producingState = .producingPausedWithOutstandingRead
|
|
case .producingPausedWithOutstandingRead:
|
|
break
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
|
|
switch event {
|
|
case ChannelEvent.inputClosed:
|
|
self._finishSource(context: context)
|
|
default:
|
|
break
|
|
}
|
|
|
|
context.fireUserInboundEventTriggered(event)
|
|
}
|
|
|
|
@inlinable
|
|
func _finishSource(with error: Error? = nil, context: ChannelHandlerContext) {
|
|
guard let source = self.source else {
|
|
return
|
|
}
|
|
|
|
// We need to deliver the reads first to buffer them in the source.
|
|
self._deliverReads(context: context)
|
|
|
|
if let error = error {
|
|
source.finish(error)
|
|
} else {
|
|
source.finish()
|
|
}
|
|
|
|
// We can nil the source here, as we're no longer going to use it.
|
|
self.source = nil
|
|
}
|
|
|
|
@inlinable
|
|
func _deliverReads(context: ChannelHandlerContext) {
|
|
if self.buffer.isEmpty {
|
|
return
|
|
}
|
|
|
|
guard let source = self.source else {
|
|
self.buffer.removeAll()
|
|
return
|
|
}
|
|
|
|
let result = source.yield(contentsOf: self.buffer)
|
|
switch result {
|
|
case .produceMore, .dropped:
|
|
break
|
|
case .stopProducing:
|
|
if self.producingState != .producingPausedWithOutstandingRead {
|
|
self.producingState = .producingPaused
|
|
}
|
|
}
|
|
self.buffer.removeAll(keepingCapacity: true)
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
extension NIOAsyncChannelInboundStreamChannelHandler {
|
|
@inlinable
|
|
func _didTerminate() {
|
|
self.eventLoop.preconditionInEventLoop()
|
|
self.source = nil
|
|
|
|
// Wedges the read open forever, we'll never read again.
|
|
self.producingState = .producingPausedWithOutstandingRead
|
|
|
|
switch self.closeRatchet.closeRead() {
|
|
case .nothing:
|
|
break
|
|
|
|
case .close:
|
|
self.context?.close(promise: nil)
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
func _produceMore() {
|
|
self.eventLoop.preconditionInEventLoop()
|
|
|
|
switch self.producingState {
|
|
case .producingPaused:
|
|
self.producingState = .keepProducing
|
|
|
|
case .producingPausedWithOutstandingRead:
|
|
self.producingState = .keepProducing
|
|
self.context?.read()
|
|
|
|
case .keepProducing:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
extension NIOAsyncChannelInboundStreamChannelHandler {
|
|
@usableFromInline
|
|
struct Delegate: @unchecked Sendable, NIOAsyncSequenceProducerDelegate {
|
|
@usableFromInline
|
|
let eventLoop: EventLoop
|
|
|
|
@usableFromInline
|
|
let handler: NIOAsyncChannelInboundStreamChannelHandler<InboundIn>
|
|
|
|
@inlinable
|
|
init(handler: NIOAsyncChannelInboundStreamChannelHandler<InboundIn>) {
|
|
self.eventLoop = handler.eventLoop
|
|
self.handler = handler
|
|
}
|
|
|
|
@inlinable
|
|
func didTerminate() {
|
|
self.eventLoop.execute {
|
|
self.handler._didTerminate()
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
func produceMore() {
|
|
self.eventLoop.execute {
|
|
self.handler._produceMore()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
@available(*, unavailable)
|
|
extension NIOAsyncChannelInboundStreamChannelHandler: Sendable {}
|
|
#endif
|