Files
swift-nio-extras/Sources/NIOExtras/RequestResponseHandler.swift
Orhan Yigit YazicilarandGeorge Barnett 774c274789 Split RequestResponseHandlers into Sendable and non-Sendable variants (#279)
Motivation:
`RequestResponseHandler` and `RequestResponseWithIDHandler` previously
allowed non-`Sendable` Requests to move across event loops by wrapping
it in `UnsafeTransfer`, which disabled the compiler's sendability
checking. Requiring every Response to be `Sendable` would solve the
safety issue but it limits how the handlers can be used.

Modifications:
* Extracted the common logic of both handlers into a single state
machine.
* Modified the original implementations to have Request conform to
`Sendable`
* Introduced two new variants that don't care if the Request is
`Sendable` as promises moving to different event loops would be failed
with an error.
* Removed the UnsafeTransfer type as it is not used anymore.
* Added unit tests to confirm that the Sendable variants allow for
promises for different event loops and non-Sendable variants fail the
promises with an Error.

Result:
* Compiler's sendability checks are restored for the handlers.

---------

Co-authored-by: George Barnett <gbarnett@apple.com>
2025-07-03 13:00:21 +01:00

191 lines
8.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2025 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 NIOCore
/// ``RequestResponseHandler`` receives a `Request` alongside an `EventLoopPromise<Response>` from the `Channel`'s
/// outbound side. It will fulfil the promise with the `Response` once it's received from the `Channel`'s inbound
/// side.
///
/// ``RequestResponseHandler`` does support pipelining `Request`s and it will send them pipelined further down the
/// `Channel`. Should ``RequestResponseHandler`` receive an error from the `Channel`, it will fail all promises meant for
/// the outstanding `Response`s and close the `Channel`. All requests enqueued after an error occurred will be immediately
/// failed with the first error the channel received.
///
/// ``RequestResponseHandler`` requires that the `Response`s arrive on `Channel` in the same order as the `Request`s
/// were submitted.
@preconcurrency
public final class RequestResponseHandler<Request, Response: Sendable>: ChannelDuplexHandler {
/// `Response` is the type this class expects to receive inbound.
public typealias InboundIn = Response
/// Don't expect to pass anything on in-bound.
public typealias InboundOut = Never
/// Type this class expect to receive in an outbound direction.
public typealias OutboundIn = (Request, EventLoopPromise<Response>)
/// Type this class passes out.
public typealias OutboundOut = Request
private var state: RequestResponseHandlerState<OrderedResponsePromiseBuffer<EventLoopPromise<Response>>>
/// Create a new `RequestResponseHandler`.
///
/// - parameters:
/// - initialBufferCapacity: `RequestResponseHandler` saves the promises for all outstanding responses in a
/// buffer. `initialBufferCapacity` is the initial capacity for this buffer. You usually do not need to set
/// this parameter unless you intend to pipeline very deeply and don't want the buffer to resize.
public init(initialBufferCapacity: Int = 4) {
self.state = .init(initialBufferCapacity: initialBufferCapacity)
}
public func channelInactive(context: ChannelHandlerContext) {
switch self.state.deactivateChannel() {
case .failPromisesAndFireInactive(let promisesToFail):
for promise in promisesToFail {
promise.fail(NIOExtrasErrors.ClosedBeforeReceivingResponse())
}
context.fireChannelInactive()
case .fireInactive:
context.fireChannelInactive()
case .doNothing:
return
}
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let response = self.unwrapInboundIn(data)
switch self.state.readResponse(id: ()) {
case .succeed(let promise):
promise.succeed(response)
// Matching promiseNotFound here as it should never be received from an CircularBuffer as the key is Void.
case .bufferEmpty, .promiseNotFound:
context.fireErrorCaught(NIOExtrasErrors.ResponsePromiseBufferEmpty())
case .notOperational:
return
}
}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
switch self.state.caughtError(error: error) {
case .failPromisesAndCloseContext(let promisesToFail):
for promise in promisesToFail {
promise.fail(error)
}
context.close(promise: nil)
case .notOperational:
return
}
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let (request, responsePromise) = self.unwrapOutboundIn(data)
switch self.state.writeRequest(id: (), responsePromise: responsePromise) {
case .failPromise(let error):
responsePromise.fail(error)
promise?.fail(error)
case .writeToContext:
context.write(self.wrapOutboundOut(request), promise: promise)
}
}
}
@available(*, unavailable)
extension RequestResponseHandler: Sendable {}
/// ``NIORequestIsolatedResponseHandler`` receives a `Request` alongside an `EventLoopPromise<Response>.Isolated` from the `Channel`'s
/// outbound side. It will fulfil the promise with the `Response` once it's received from the `Channel`'s inbound
/// side.
///
/// ``NIORequestIsolatedResponseHandler`` does support pipelining `Request`s and it will send them pipelined further down the
/// `Channel`. Should ``NIORequestIsolatedResponseHandler`` receive an error from the `Channel`, it will fail all promises meant for
/// the outstanding `Response`s and close the `Channel`. All requests enqueued after an error occurred will be immediately
/// failed with the first error the channel received.
///
/// ``NIORequestIsolatedResponseHandler`` requires that the `Response`s arrive on `Channel` in the same order as the `Request`s
/// were submitted.
public final class NIORequestIsolatedResponseHandler<Request, Response>: ChannelDuplexHandler {
/// `Response` is the type this class expects to receive inbound.
public typealias InboundIn = Response
/// Don't expect to pass anything on in-bound.
public typealias InboundOut = Never
/// Type this class expect to receive in an outbound direction.
public typealias OutboundIn = (Request, EventLoopPromise<Response>.Isolated)
/// Type this class passes out.
public typealias OutboundOut = Request
private var state: RequestResponseHandlerState<OrderedResponsePromiseBuffer<EventLoopPromise<Response>.Isolated>>
/// Create a new `NIORequestIsolatedResponseHandler`.
///
/// - parameters:
/// - initialBufferCapacity: `NIORequestIsolatedResponseHandler` saves the promises for all outstanding responses in a
/// buffer. `initialBufferCapacity` is the initial capacity for this buffer. You usually do not need to set
/// this parameter unless you intend to pipeline very deeply and don't want the buffer to resize.
public init(initialBufferCapacity: Int = 4) {
self.state = .init(initialBufferCapacity: initialBufferCapacity)
}
public func channelInactive(context: ChannelHandlerContext) {
switch self.state.deactivateChannel() {
case .failPromisesAndFireInactive(let promisesToFail):
for promise in promisesToFail {
promise.nonisolated().fail(NIOExtrasErrors.ClosedBeforeReceivingResponse())
}
context.fireChannelInactive()
case .fireInactive:
context.fireChannelInactive()
case .doNothing:
return
}
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let response = self.unwrapInboundIn(data)
switch self.state.readResponse(id: ()) {
case .succeed(let promise):
promise.succeed(response)
// Matching promiseNotFound here as it should never be received from an CircularBuffer as the key is Void
case .bufferEmpty, .promiseNotFound:
context.fireErrorCaught(NIOExtrasErrors.ResponsePromiseBufferEmpty())
case .notOperational:
return
}
}
public func errorCaught(context: ChannelHandlerContext, error: Error) {
switch self.state.caughtError(error: error) {
case .failPromisesAndCloseContext(let promisesToFail):
for promise in promisesToFail {
promise.nonisolated().fail(error)
}
context.close(promise: nil)
case .notOperational:
return
}
}
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
let (request, responsePromise) = self.unwrapOutboundIn(data)
switch self.state.writeRequest(id: (), responsePromise: responsePromise) {
case .failPromise(let error):
responsePromise.nonisolated().fail(error)
promise?.fail(error)
case .writeToContext:
context.write(self.wrapOutboundOut(request), promise: promise)
}
}
}
@available(*, unavailable)
extension NIORequestIsolatedResponseHandler: Sendable {}