mirror of
https://github.com/apple/swift-nio-extras
synced 2026-11-22 04:30:42 +00:00
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>
This commit is contained in:
1 parent
07afea89d6
commit
774c274789
8 files changed
+1163
-184
No files matched your search
+2
-1
@@ -6,4 +6,5 @@ Package.pins
|
||||
*.pem
|
||||
/docs
|
||||
Package.resolved
|
||||
.swiftpm/
|
||||
.swiftpm/
|
||||
.vscode/
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -26,4 +26,7 @@ public enum NIOExtrasErrors: Sendable {
|
||||
|
||||
/// The channel was closed before receiving a response to a request.
|
||||
public struct ClosedBeforeReceivingResponse: NIOExtrasError {}
|
||||
|
||||
/// Error indicating the response promise buffer is empty.
|
||||
public struct ResponsePromiseBufferEmpty: NIOExtrasError {}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -25,7 +25,8 @@ import NIOCore
|
||||
///
|
||||
/// ``RequestResponseHandler`` requires that the `Response`s arrive on `Channel` in the same order as the `Request`s
|
||||
/// were submitted.
|
||||
public final class RequestResponseHandler<Request, Response>: ChannelDuplexHandler {
|
||||
@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.
|
||||
@@ -35,96 +36,64 @@ public final class RequestResponseHandler<Request, Response>: ChannelDuplexHandl
|
||||
/// Type this class passes out.
|
||||
public typealias OutboundOut = Request
|
||||
|
||||
private enum State {
|
||||
case operational
|
||||
case error(Error)
|
||||
private var state: RequestResponseHandlerState<OrderedResponsePromiseBuffer<EventLoopPromise<Response>>>
|
||||
|
||||
var isOperational: Bool {
|
||||
switch self {
|
||||
case .operational:
|
||||
return true
|
||||
case .error:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var state: State = .operational
|
||||
private var promiseBuffer: CircularBuffer<EventLoopPromise<Response>>
|
||||
|
||||
/// Create a new ``RequestResponseHandler``.
|
||||
/// Create a new `RequestResponseHandler`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - initialBufferCapacity: ``RequestResponseHandler`` saves the promises for all outstanding responses in a
|
||||
/// - 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.promiseBuffer = CircularBuffer(initialCapacity: initialBufferCapacity)
|
||||
self.state = .init(initialBufferCapacity: initialBufferCapacity)
|
||||
}
|
||||
|
||||
public func channelInactive(context: ChannelHandlerContext) {
|
||||
switch self.state {
|
||||
case .error:
|
||||
// We failed any outstanding promises when we entered the error state and will fail any
|
||||
// new promises in write.
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
case .operational:
|
||||
let promiseBuffer = self.promiseBuffer
|
||||
self.promiseBuffer.removeAll()
|
||||
for promise in promiseBuffer {
|
||||
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
|
||||
}
|
||||
context.fireChannelInactive()
|
||||
}
|
||||
|
||||
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
||||
guard self.state.isOperational else {
|
||||
// we're in an error state, ignore further responses
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return
|
||||
}
|
||||
|
||||
let response = self.unwrapInboundIn(data)
|
||||
let promise = self.promiseBuffer.removeFirst()
|
||||
|
||||
// If the event loop of the promise is the same as the context then there's no
|
||||
// change in isolation. Otherwise transfer the response onto the correct event-loop
|
||||
// before succeeding the promise.
|
||||
if promise.futureResult.eventLoop === context.eventLoop {
|
||||
promise.assumeIsolatedUnsafeUnchecked().succeed(response)
|
||||
} else {
|
||||
let unsafeTransfer = UnsafeTransfer(response)
|
||||
promise.futureResult.eventLoop.execute {
|
||||
let response = unsafeTransfer.wrappedValue
|
||||
promise.assumeIsolatedUnsafeUnchecked().succeed(response)
|
||||
}
|
||||
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) {
|
||||
guard self.state.isOperational else {
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
switch self.state.caughtError(error: error) {
|
||||
case .failPromisesAndCloseContext(let promisesToFail):
|
||||
for promise in promisesToFail {
|
||||
promise.fail(error)
|
||||
}
|
||||
context.close(promise: nil)
|
||||
case .notOperational:
|
||||
return
|
||||
}
|
||||
self.state = .error(error)
|
||||
let promiseBuffer = self.promiseBuffer
|
||||
self.promiseBuffer.removeAll()
|
||||
context.close(promise: nil)
|
||||
for promise in promiseBuffer {
|
||||
promise.fail(error)
|
||||
}
|
||||
}
|
||||
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
let (request, responsePromise) = self.unwrapOutboundIn(data)
|
||||
switch self.state {
|
||||
case .error(let error):
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
switch self.state.writeRequest(id: (), responsePromise: responsePromise) {
|
||||
case .failPromise(let error):
|
||||
responsePromise.fail(error)
|
||||
promise?.fail(error)
|
||||
case .operational:
|
||||
self.promiseBuffer.append(responsePromise)
|
||||
case .writeToContext:
|
||||
context.write(self.wrapOutboundOut(request), promise: promise)
|
||||
}
|
||||
}
|
||||
@@ -132,3 +101,90 @@ public final class RequestResponseHandler<Request, Response>: ChannelDuplexHandl
|
||||
|
||||
@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 {}
|
||||
@@ -0,0 +1,243 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 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
|
||||
|
||||
// MARK: - ResponsePromiseBuffer
|
||||
|
||||
/// Protocol to expose the stored `Value` type inside `EventLoopPromise` letting
|
||||
/// generic code refer to it for additional constraints.
|
||||
protocol ResponsePromise {
|
||||
associatedtype Response
|
||||
}
|
||||
|
||||
extension EventLoopPromise: ResponsePromise {
|
||||
typealias Response = Value
|
||||
}
|
||||
|
||||
extension EventLoopPromise.Isolated: ResponsePromise {
|
||||
typealias Response = Value
|
||||
}
|
||||
|
||||
/// A storage abstraction for outstanding response promises.
|
||||
///
|
||||
/// The state-machine never assumes how the promises are stored,
|
||||
/// any type that conforms to this protocol is acceptable by the state machine.
|
||||
protocol ResponsePromiseBuffer {
|
||||
associatedtype ID
|
||||
associatedtype Promise
|
||||
|
||||
/// Number of promises that are currently in the buffer.
|
||||
var count: Int { get }
|
||||
|
||||
/// Create an empty buffer with storage allocated for `initialBufferCapacity` number of elements.
|
||||
init(initialBufferCapacity: Int)
|
||||
|
||||
/// Store `promise` under `forKey`.
|
||||
mutating func insert(_ promise: Promise, forKey: ID)
|
||||
|
||||
/// Remove and return the promise for `key` or `nil` if no promise with the given `ID` exists.
|
||||
mutating func removePromise(forKey: ID) -> Promise?
|
||||
|
||||
/// Remove all stored promises and return them so they can be failed/succeeded.
|
||||
mutating func removeAll() -> [Promise]
|
||||
}
|
||||
|
||||
/// Buffer implementation using the `CircularBuffer` type provided in NIO.
|
||||
/// `ID` is `Void` as the `CircularBuffer` is FIFO.
|
||||
struct OrderedResponsePromiseBuffer<Promise: ResponsePromise>: ResponsePromiseBuffer {
|
||||
typealias ID = Void
|
||||
typealias Promise = Promise
|
||||
|
||||
private var buffer: CircularBuffer<Promise>
|
||||
|
||||
var count: Int {
|
||||
self.buffer.count
|
||||
}
|
||||
|
||||
init(initialBufferCapacity: Int) {
|
||||
self.buffer = CircularBuffer(initialCapacity: initialBufferCapacity)
|
||||
}
|
||||
|
||||
mutating func insert(_ promise: Promise, forKey: ID = ()) {
|
||||
self.buffer.append(promise)
|
||||
}
|
||||
|
||||
mutating func removePromise(forKey: ID = ()) -> Promise? {
|
||||
self.buffer.popFirst()
|
||||
}
|
||||
|
||||
mutating func removeAll() -> [Promise] {
|
||||
defer {
|
||||
self.buffer.removeAll()
|
||||
}
|
||||
return Array(self.buffer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Buffer implementation using a `Dictionary` to store the promises.
|
||||
///
|
||||
/// The promise (`Promise`) `Value` must be `NIORequestIdentifiable` to
|
||||
/// provide the `Dictionary` a key to store the promise under.
|
||||
struct UnorderedResponsePromiseBuffer<Promise: ResponsePromise>: ResponsePromiseBuffer
|
||||
where Promise.Response: NIORequestIdentifiable {
|
||||
typealias ID = Promise.Response.RequestID
|
||||
typealias Promise = Promise
|
||||
|
||||
private var buffer: [ID: Promise]
|
||||
|
||||
var count: Int {
|
||||
self.buffer.count
|
||||
}
|
||||
|
||||
init(initialBufferCapacity: Int) {
|
||||
self.buffer = .init(minimumCapacity: initialBufferCapacity)
|
||||
}
|
||||
|
||||
mutating func insert(_ promise: Promise, forKey: ID) {
|
||||
self.buffer[forKey] = promise
|
||||
}
|
||||
|
||||
mutating func removePromise(forKey: ID) -> Promise? {
|
||||
self.buffer.removeValue(forKey: forKey)
|
||||
}
|
||||
|
||||
mutating func removeAll() -> [Promise] {
|
||||
defer {
|
||||
self.buffer.removeAll()
|
||||
}
|
||||
return Array(self.buffer.values)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - RequestResponseHandler State Machine
|
||||
|
||||
/// Enum based FSM that owns a buffer of promises and transitions between
|
||||
/// *operational*, *inactive* (handler closed gracefully) and *error* states.
|
||||
struct RequestResponseHandlerState<PromiseBuffer: ResponsePromiseBuffer> {
|
||||
private enum State {
|
||||
case operational
|
||||
case inactive
|
||||
case error(Error)
|
||||
|
||||
var isOperational: Bool {
|
||||
switch self {
|
||||
case .operational:
|
||||
return true
|
||||
case .error, .inactive:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var state: State
|
||||
private var promiseBuffer: PromiseBuffer
|
||||
|
||||
init(initialBufferCapacity: Int = 4) {
|
||||
self.state = .operational
|
||||
self.promiseBuffer = PromiseBuffer(initialBufferCapacity: initialBufferCapacity)
|
||||
}
|
||||
|
||||
enum DeactiveChannelAction {
|
||||
// fail the promises and propagate channelInactive to the context
|
||||
case failPromisesAndFireInactive([PromiseBuffer.Promise])
|
||||
case fireInactive // propagate channelInactive to the context
|
||||
case doNothing // do nothing
|
||||
}
|
||||
|
||||
/// Handle `channelInactive`. Drains the buffer if the state machine is still operational.
|
||||
mutating func deactivateChannel() -> DeactiveChannelAction {
|
||||
switch self.state {
|
||||
case .error:
|
||||
// We failed any outstanding promises when we entered the error state and will fail any
|
||||
// new promises in write.
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return .fireInactive
|
||||
case .inactive:
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
// This is weird, we shouldn't get this more than once but it's not the end of the world either. But in
|
||||
// debug we probably want to crash.
|
||||
assertionFailure("Received channelInactive on an already-inactive handler")
|
||||
return .doNothing
|
||||
case .operational:
|
||||
self.state = .inactive
|
||||
return .failPromisesAndFireInactive(self.promiseBuffer.removeAll())
|
||||
}
|
||||
}
|
||||
|
||||
enum CaughtErrorAction {
|
||||
// fail the promises and close the context as we received an error
|
||||
case failPromisesAndCloseContext([PromiseBuffer.Promise])
|
||||
case notOperational // not operational
|
||||
}
|
||||
|
||||
/// Handle `errorCaught`. Transitions to `.error` exacly once as there is no way to make the handler operational again.
|
||||
mutating func caughtError(error: Error) -> CaughtErrorAction {
|
||||
guard self.state.isOperational else {
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return .notOperational
|
||||
}
|
||||
|
||||
self.state = .error(error)
|
||||
return .failPromisesAndCloseContext(self.promiseBuffer.removeAll())
|
||||
}
|
||||
|
||||
enum ReadResponseAction {
|
||||
case succeed(PromiseBuffer.Promise) // succeed the returned promise)
|
||||
case promiseNotFound // no matching promise found
|
||||
case bufferEmpty // buffer is empty
|
||||
case notOperational // not operational
|
||||
}
|
||||
|
||||
/// Remove and return the response for `id` if it is present in the buffer.
|
||||
mutating func readResponse(id: PromiseBuffer.ID) -> ReadResponseAction {
|
||||
guard self.state.isOperational else {
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return .notOperational
|
||||
}
|
||||
|
||||
guard self.promiseBuffer.count != 0 else {
|
||||
return .bufferEmpty
|
||||
}
|
||||
|
||||
if let promise = self.promiseBuffer.removePromise(forKey: id) {
|
||||
return .succeed(promise)
|
||||
} else {
|
||||
return .promiseNotFound
|
||||
}
|
||||
}
|
||||
|
||||
enum WriteRequestAction {
|
||||
case failPromise(error: Error) // do not write, fail promise with error
|
||||
case writeToContext // write to the context
|
||||
}
|
||||
|
||||
/// Buffer the promise or fail it immediately, depending on current state.
|
||||
mutating func writeRequest(
|
||||
id: PromiseBuffer.ID,
|
||||
responsePromise: PromiseBuffer.Promise
|
||||
) -> WriteRequestAction {
|
||||
switch self.state {
|
||||
case .error(let error):
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return .failPromise(error: error)
|
||||
case .inactive:
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return .failPromise(error: ChannelError.ioOnClosedChannel)
|
||||
case .operational:
|
||||
self.promiseBuffer.insert(responsePromise, forKey: id)
|
||||
return .writeToContext
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -14,132 +14,88 @@
|
||||
|
||||
import NIOCore
|
||||
|
||||
/// `NIORequestResponseWithIDHandler` receives a `Request` alongside an `EventLoopPromise<Response>` from the
|
||||
/// ``NIORequestResponseWithIDHandler`` receives a `Request` alongside an `EventLoopPromise<Response>` from the
|
||||
/// `Channel`'s outbound side. It will fulfill the promise with the `Response` once it's received from the `Channel`'s
|
||||
/// inbound side. Requests and responses can arrive out-of-order and are matched by the virtue of being
|
||||
/// `NIORequestIdentifiable`.
|
||||
///
|
||||
/// `NIORequestResponseWithIDHandler` 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
|
||||
/// ``NIORequestResponseWithIDHandler`` does support pipelining `Request`s and it will send them pipelined further down the
|
||||
/// `Channel`. Should ``NIORequestResponseWithIDHandler`` receive an error from the `Channel`, it will fail all promises meant for
|
||||
/// the outstanding `Reponse`s and close the `Channel`. All requests enqueued after an error occured will be immediately
|
||||
/// failed with the first error the channel received.
|
||||
///
|
||||
/// `NIORequestResponseWithIDHandler` does _not_ require that the `Response`s arrive on `Channel` in the same order as
|
||||
/// ``NIORequestResponseWithIDHandler`` does _not_ require that the `Response`s arrive on `Channel` in the same order as
|
||||
/// the `Request`s were submitted. They are matched by their `requestID` property (from `NIORequestIdentifiable`).
|
||||
@preconcurrency
|
||||
public final class NIORequestResponseWithIDHandler<
|
||||
Request: NIORequestIdentifiable,
|
||||
Response: NIORequestIdentifiable
|
||||
>: ChannelDuplexHandler
|
||||
where Request.RequestID == Response.RequestID {
|
||||
where Request.RequestID == Response.RequestID, Response: Sendable {
|
||||
public typealias InboundIn = Response
|
||||
public typealias InboundOut = Never
|
||||
public typealias OutboundIn = (Request, EventLoopPromise<Response>)
|
||||
public typealias OutboundOut = Request
|
||||
|
||||
private enum State {
|
||||
case operational
|
||||
case inactive
|
||||
case error(Error)
|
||||
private var state: RequestResponseHandlerState<UnorderedResponsePromiseBuffer<EventLoopPromise<Response>>>
|
||||
|
||||
var isOperational: Bool {
|
||||
switch self {
|
||||
case .operational:
|
||||
return true
|
||||
case .error, .inactive:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var state: State = .operational
|
||||
private var promiseBuffer: [Request.RequestID: EventLoopPromise<Response>]
|
||||
|
||||
/// Create a new `RequestResponseHandler`.
|
||||
/// Create a new `NIORequestResponseWithIDHandler`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - initialBufferCapacity: `RequestResponseHandler` saves the promises for all outstanding responses in a
|
||||
/// - initialBufferCapacity: `NIORequestResponseWithIDHandler` 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.promiseBuffer = [:]
|
||||
self.promiseBuffer.reserveCapacity(initialBufferCapacity)
|
||||
self.state = .init(initialBufferCapacity: 4)
|
||||
}
|
||||
|
||||
public func channelInactive(context: ChannelHandlerContext) {
|
||||
switch self.state {
|
||||
case .error:
|
||||
// We failed any outstanding promises when we entered the error state and will fail any
|
||||
// new promises in write.
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
case .inactive:
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
// This is weird, we shouldn't get this more than once but it's not the end of the world either. But in
|
||||
// debug we probably want to crash.
|
||||
assertionFailure("Received channelInactive on an already-inactive NIORequestResponseWithIDHandler")
|
||||
case .operational:
|
||||
let promiseBuffer = self.promiseBuffer
|
||||
self.promiseBuffer.removeAll()
|
||||
self.state = .inactive
|
||||
for promise in promiseBuffer {
|
||||
promise.value.fail(NIOExtrasErrors.ClosedBeforeReceivingResponse())
|
||||
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
|
||||
}
|
||||
context.fireChannelInactive()
|
||||
}
|
||||
|
||||
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
||||
guard self.state.isOperational else {
|
||||
// we're in an error state, ignore further responses
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
return
|
||||
}
|
||||
|
||||
let response = self.unwrapInboundIn(data)
|
||||
if let promise = self.promiseBuffer.removeValue(forKey: response.requestID) {
|
||||
// If the event loop of the promise is the same as the context then there's no
|
||||
// change in isolation. Otherwise transfer the response onto the correct event-loop
|
||||
// before succeeding the promise.
|
||||
if promise.futureResult.eventLoop === context.eventLoop {
|
||||
promise.assumeIsolatedUnsafeUnchecked().succeed(response)
|
||||
} else {
|
||||
let unsafeTransfer = UnsafeTransfer(response)
|
||||
promise.futureResult.eventLoop.execute {
|
||||
let response = unsafeTransfer.wrappedValue
|
||||
promise.assumeIsolatedUnsafeUnchecked().succeed(response)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch self.state.readResponse(id: response.requestID) {
|
||||
case .succeed(let promise):
|
||||
promise.succeed(response)
|
||||
case .bufferEmpty:
|
||||
context.fireErrorCaught(NIOExtrasErrors.ResponsePromiseBufferEmpty())
|
||||
case .promiseNotFound:
|
||||
context.fireErrorCaught(NIOExtrasErrors.ResponseForInvalidRequest<Response>(requestID: response.requestID))
|
||||
case .notOperational:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
public func errorCaught(context: ChannelHandlerContext, error: Error) {
|
||||
guard self.state.isOperational else {
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
switch self.state.caughtError(error: error) {
|
||||
case .failPromisesAndCloseContext(let promisesToFail):
|
||||
for promise in promisesToFail {
|
||||
promise.fail(error)
|
||||
}
|
||||
context.close(promise: nil)
|
||||
case .notOperational:
|
||||
return
|
||||
}
|
||||
self.state = .error(error)
|
||||
let promiseBuffer = self.promiseBuffer
|
||||
self.promiseBuffer.removeAll()
|
||||
context.close(promise: nil)
|
||||
for promise in promiseBuffer {
|
||||
promise.value.fail(error)
|
||||
}
|
||||
}
|
||||
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
let (request, responsePromise) = self.unwrapOutboundIn(data)
|
||||
switch self.state {
|
||||
case .error(let error):
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
switch self.state.writeRequest(id: request.requestID, responsePromise: responsePromise) {
|
||||
case .failPromise(let error):
|
||||
responsePromise.fail(error)
|
||||
promise?.fail(error)
|
||||
case .inactive:
|
||||
assert(self.promiseBuffer.count == 0)
|
||||
promise?.fail(ChannelError.ioOnClosedChannel)
|
||||
responsePromise.fail(ChannelError.ioOnClosedChannel)
|
||||
case .operational:
|
||||
self.promiseBuffer[request.requestID] = responsePromise
|
||||
case .writeToContext:
|
||||
context.write(self.wrapOutboundOut(request), promise: promise)
|
||||
}
|
||||
}
|
||||
@@ -148,6 +104,95 @@ where Request.RequestID == Response.RequestID {
|
||||
@available(*, unavailable)
|
||||
extension NIORequestResponseWithIDHandler: Sendable {}
|
||||
|
||||
/// ``NIORequestIsolatedResponseWithIDHandler`` receives a `Request` alongside an `EventLoopPromise<Response>.Isolated` from the
|
||||
/// `Channel`'s outbound side. It will fulfill the promise with the `Response` once it's received from the `Channel`'s
|
||||
/// inbound side. Requests and responses can arrive out-of-order and are matched by the virtue of being
|
||||
/// `NIORequestIdentifiable`.
|
||||
///
|
||||
/// ``NIORequestIsolatedResponseWithIDHandler`` does support pipelining `Request`s and it will send them pipelined further down the
|
||||
/// `Channel`. Should ``NIORequestIsolatedResponseWithIDHandler`` receive an error from the `Channel`, it will fail all promises meant for
|
||||
/// the outstanding `Reponse`s and close the `Channel`. All requests enqueued after an error occured will be immediately
|
||||
/// failed with the first error the channel received.
|
||||
///
|
||||
/// ``NIORequestIsolatedResponseWithIDHandler`` does _not_ require that the `Response`s arrive on `Channel` in the same order as
|
||||
/// the `Request`s were submitted. They are matched by their `requestID` property (from `NIORequestIdentifiable`).
|
||||
public final class NIORequestIsolatedResponseWithIDHandler<
|
||||
Request: NIORequestIdentifiable,
|
||||
Response: NIORequestIdentifiable
|
||||
>: ChannelDuplexHandler
|
||||
where Request.RequestID == Response.RequestID {
|
||||
public typealias InboundIn = Response
|
||||
public typealias InboundOut = Never
|
||||
public typealias OutboundIn = (Request, EventLoopPromise<Response>.Isolated)
|
||||
public typealias OutboundOut = Request
|
||||
|
||||
private var state: RequestResponseHandlerState<UnorderedResponsePromiseBuffer<EventLoopPromise<Response>.Isolated>>
|
||||
|
||||
/// Create a new `NIORequestIsolatedResponseWithIDHandler`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - initialBufferCapacity: `NIORequestIsolatedResponseWithIDHandler` 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: 4)
|
||||
}
|
||||
|
||||
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: response.requestID) {
|
||||
case .succeed(let promise):
|
||||
promise.succeed(response)
|
||||
case .bufferEmpty:
|
||||
context.fireErrorCaught(NIOExtrasErrors.ResponsePromiseBufferEmpty())
|
||||
case .promiseNotFound:
|
||||
context.fireErrorCaught(NIOExtrasErrors.ResponseForInvalidRequest<Response>(requestID: response.requestID))
|
||||
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: request.requestID, 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 NIORequestIsolatedResponseWithIDHandler: Sendable {}
|
||||
|
||||
extension NIOExtrasErrors {
|
||||
public struct ResponseForInvalidRequest<Response: NIORequestIdentifiable>: NIOExtrasError, Equatable {
|
||||
public var requestID: Response.RequestID
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// ``UnsafeTransfer`` can be used to make non-`Sendable` values `Sendable`.
|
||||
/// As the name implies, the usage of this is unsafe because it disables the sendable checking of the compiler.
|
||||
/// It can be used similar to `@unsafe Sendable` but for values instead of types.
|
||||
@usableFromInline
|
||||
struct UnsafeTransfer<Wrapped> {
|
||||
@usableFromInline
|
||||
var wrappedValue: Wrapped
|
||||
|
||||
@inlinable
|
||||
init(_ wrappedValue: Wrapped) {
|
||||
self.wrappedValue = wrappedValue
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeTransfer: @unchecked Sendable {}
|
||||
|
||||
extension UnsafeTransfer: Equatable where Wrapped: Equatable {}
|
||||
extension UnsafeTransfer: Hashable where Wrapped: Hashable {}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -60,6 +60,18 @@ class RequestResponseHandlerTest: XCTestCase {
|
||||
XCTAssertNoThrow(XCTAssertEqual("okay", try p.futureResult.wait()))
|
||||
}
|
||||
|
||||
func testReadOnEmptyBuffer() {
|
||||
XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(RequestResponseHandler<IOData, String>()))
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
// read from the empty handler
|
||||
XCTAssertThrowsError(try self.channel.writeInbound("should error")) { error in
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ResponsePromiseBufferEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func testEnqueingMultipleRequestsWorks() throws {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(try self.channel.pipeline.syncOperations.addHandler(RequestResponseHandler<IOData, Int>()))
|
||||
@@ -163,3 +175,209 @@ class RequestResponseHandlerTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class NIORequestIsolatedResponseHandlerTest: XCTestCase {
|
||||
private var eventLoop: EmbeddedEventLoop!
|
||||
private var channel: EmbeddedChannel!
|
||||
private var buffer: ByteBuffer!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
self.eventLoop = EmbeddedEventLoop()
|
||||
self.channel = EmbeddedChannel(loop: self.eventLoop)
|
||||
self.buffer = self.channel.allocator.buffer(capacity: 16)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
self.buffer = nil
|
||||
self.eventLoop = nil
|
||||
if self.channel.isActive {
|
||||
XCTAssertNoThrow(XCTAssertTrue(try self.channel.finish().isClean))
|
||||
}
|
||||
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testSimpleRequestWorks() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<IOData, NSString>())
|
||||
)
|
||||
self.buffer.writeString("hello")
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
let p: EventLoopPromise<NSString>.Isolated = self.channel.eventLoop.makePromise().assumeIsolated()
|
||||
// write request
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((IOData.byteBuffer(self.buffer), p)))
|
||||
// write response
|
||||
XCTAssertNoThrow(try self.channel.writeInbound("okay"))
|
||||
// verify request was forwarded
|
||||
XCTAssertNoThrow(XCTAssertEqual(IOData.byteBuffer(self.buffer), try self.channel.readOutbound()))
|
||||
// verify response was not forwarded
|
||||
XCTAssertNoThrow(XCTAssertEqual(nil, try self.channel.readInbound(as: IOData.self)))
|
||||
// verify the promise got succeeded with the response
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual("okay", value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReadOnEmptyBuffer() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<IOData, NSString>())
|
||||
)
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
// read from the empty handler
|
||||
XCTAssertThrowsError(try self.channel.writeInbound("should error")) { error in
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ResponsePromiseBufferEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func testEnqueingMultipleRequestsWorks() throws {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<IOData, NSString>())
|
||||
)
|
||||
|
||||
var futures: [EventLoopFuture<NSString>.Isolated] = []
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
for reqId in 0..<5 {
|
||||
self.buffer.clear()
|
||||
self.buffer.writeString("\(reqId)")
|
||||
|
||||
let p: EventLoopPromise<NSString>.Isolated = self.channel.eventLoop.makePromise().assumeIsolated()
|
||||
futures.append(p.futureResult)
|
||||
|
||||
// write request
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((IOData.byteBuffer(self.buffer), p)))
|
||||
}
|
||||
|
||||
// let's have 3 successful responses
|
||||
for reqIdExpected in 0..<3 {
|
||||
switch try self.channel.readOutbound(as: IOData.self) {
|
||||
case .some(.byteBuffer(var buffer)):
|
||||
if let reqId = buffer.readString(length: buffer.readableBytes) {
|
||||
// write response
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(reqId))
|
||||
} else {
|
||||
XCTFail("couldn't get request id")
|
||||
}
|
||||
default:
|
||||
XCTFail("could not find request")
|
||||
}
|
||||
futures[reqIdExpected].whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(reqIdExpected, value.integerValue)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validate the Channel is active
|
||||
XCTAssertTrue(self.channel.isActive)
|
||||
self.channel.pipeline.fireErrorCaught(DummyError())
|
||||
|
||||
// after receiving an error, it should be closed
|
||||
XCTAssertFalse(self.channel.isActive)
|
||||
|
||||
for failedReqId in 3..<5 {
|
||||
futures[failedReqId].whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify no response was not forwarded
|
||||
XCTAssertNoThrow(XCTAssertEqual(nil, try self.channel.readInbound(as: IOData.self)))
|
||||
}
|
||||
|
||||
func testRequestsEnqueuedAfterErrorAreFailed() {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<IOData, Void>())
|
||||
)
|
||||
|
||||
self.channel.pipeline.fireErrorCaught(DummyError())
|
||||
|
||||
let p: EventLoopPromise<Void>.Isolated = self.eventLoop.makePromise().assumeIsolated()
|
||||
XCTAssertThrowsError(try self.channel.writeOutbound((IOData.byteBuffer(self.buffer), p))) { error in
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRequestsEnqueuedJustBeforeErrorAreFailed() {
|
||||
struct DummyError1: Error {}
|
||||
struct DummyError2: Error {}
|
||||
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<IOData, Void>())
|
||||
)
|
||||
|
||||
let p: EventLoopPromise<Void>.Isolated = self.eventLoop.makePromise().assumeIsolated()
|
||||
// right now, everything's still okay so the enqueued request won't immediately be failed
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((IOData.byteBuffer(self.buffer), p)))
|
||||
|
||||
// but whilst we're waiting for the response, an error turns up
|
||||
self.channel.pipeline.fireErrorCaught(DummyError1())
|
||||
|
||||
// we'll also fire a second error through the pipeline that shouldn't do anything
|
||||
self.channel.pipeline.fireErrorCaught(DummyError2())
|
||||
|
||||
// and just after the error, the response arrives too (but too late)
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(()))
|
||||
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testClosedConnectionFailsOutstandingPromises() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(NIORequestIsolatedResponseHandler<String, Void>())
|
||||
)
|
||||
|
||||
let promise = self.eventLoop.makePromise(of: Void.self).assumeIsolated()
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound(("Hello!", promise)))
|
||||
|
||||
XCTAssertNoThrow(try self.channel.close().wait())
|
||||
|
||||
promise.futureResult.whenComplete { result in
|
||||
switch result {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ClosedBeforeReceivingResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2025 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -74,6 +74,23 @@ class RequestResponseWithIDHandlerTest: XCTestCase {
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "okay"), try p.futureResult.wait())
|
||||
}
|
||||
|
||||
func testReadOnEmptyBuffer() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<String>>()
|
||||
)
|
||||
)
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
// read from the empty handler
|
||||
XCTAssertThrowsError(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: "should error"))) {
|
||||
error in
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ResponsePromiseBufferEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func testEnqueingMultipleRequestsWorks() throws {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(
|
||||
@@ -204,7 +221,7 @@ class RequestResponseWithIDHandlerTest: XCTestCase {
|
||||
self.channel.pipeline.fireErrorCaught(DummyError2())
|
||||
|
||||
// and just after the error, the response arrives too (but too late)
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(()))
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: ())))
|
||||
|
||||
XCTAssertThrowsError(try p.futureResult.wait()) { error in
|
||||
XCTAssertNotNil(error as? DummyError1)
|
||||
@@ -286,7 +303,6 @@ class RequestResponseWithIDHandlerTest: XCTestCase {
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "1"), try self.channel.readOutbound())
|
||||
// verify responses were not forwarded
|
||||
XCTAssertEqual(nil, try self.channel.readInbound(as: ValueWithRequestID<IOData>.self))
|
||||
// verify the promises got succeeded with the response
|
||||
}
|
||||
|
||||
func testMoreRequestsAfterChannelInactiveFail() {
|
||||
@@ -371,6 +387,435 @@ class RequestResponseWithIDHandlerTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
class RequestIsolatedResponseWithIDHandlerTest: XCTestCase {
|
||||
private var eventLoop: EmbeddedEventLoop!
|
||||
private var channel: EmbeddedChannel!
|
||||
private var buffer: ByteBuffer!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
self.eventLoop = EmbeddedEventLoop()
|
||||
self.channel = EmbeddedChannel(loop: self.eventLoop)
|
||||
self.buffer = self.channel.allocator.buffer(capacity: 16)
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
self.buffer = nil
|
||||
self.eventLoop = nil
|
||||
if self.channel.isActive {
|
||||
XCTAssertNoThrow(XCTAssertTrue(try self.channel.finish().isClean))
|
||||
}
|
||||
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testSimpleRequestWorks() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<NSString>>()
|
||||
)
|
||||
)
|
||||
self.buffer.writeString("hello")
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
let p: EventLoopPromise<ValueWithRequestID<NSString>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
// write request
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.writeOutbound(
|
||||
(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(self.buffer)),
|
||||
p
|
||||
)
|
||||
)
|
||||
)
|
||||
// write response
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID<NSString>(requestID: 1, value: "okay")))
|
||||
// verify request was forwarded
|
||||
XCTAssertEqual(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(self.buffer)),
|
||||
try self.channel.readOutbound()
|
||||
)
|
||||
// verify response was not forwarded
|
||||
XCTAssertEqual(nil, try self.channel.readInbound(as: ValueWithRequestID<IOData>.self))
|
||||
// verify the promise got succeeded with the response
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "okay"), value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReadOnEmptyBuffer() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<String>>()
|
||||
)
|
||||
)
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
// read from the empty handler
|
||||
XCTAssertThrowsError(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: "should error"))) {
|
||||
error in
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ResponsePromiseBufferEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func testEnqueingMultipleRequestsWorks() throws {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<Int>>()
|
||||
)
|
||||
)
|
||||
|
||||
var futures: [EventLoopFuture<ValueWithRequestID<Int>>.Isolated] = []
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
for reqId in 0..<5 {
|
||||
self.buffer.clear()
|
||||
self.buffer.writeString("\(reqId)")
|
||||
|
||||
let p: EventLoopPromise<ValueWithRequestID<Int>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
futures.append(p.futureResult)
|
||||
|
||||
// write request
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.writeOutbound(
|
||||
(
|
||||
ValueWithRequestID(
|
||||
requestID: reqId,
|
||||
value: IOData.byteBuffer(self.buffer)
|
||||
), p
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// let's have 3 successful responses
|
||||
for reqIdExpected in 0..<3 {
|
||||
switch try self.channel.readOutbound(as: ValueWithRequestID<IOData>.self) {
|
||||
case .some(let req):
|
||||
guard case .byteBuffer(var buffer) = req.value else {
|
||||
XCTFail("wrong type")
|
||||
return
|
||||
}
|
||||
if let reqId = buffer.readString(length: buffer.readableBytes).flatMap(Int.init) {
|
||||
// write response
|
||||
try self.channel.writeInbound(ValueWithRequestID(requestID: reqId, value: reqId))
|
||||
} else {
|
||||
XCTFail("couldn't get request id")
|
||||
}
|
||||
default:
|
||||
XCTFail("could not find request")
|
||||
}
|
||||
|
||||
futures[reqIdExpected].whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: reqIdExpected, value: reqIdExpected), value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validate the Channel is active
|
||||
XCTAssertTrue(self.channel.isActive)
|
||||
self.channel.pipeline.fireErrorCaught(DummyError())
|
||||
|
||||
// after receiving an error, it should be closed
|
||||
XCTAssertFalse(self.channel.isActive)
|
||||
|
||||
for failedReqId in 3..<5 {
|
||||
futures[failedReqId].whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify no response was not forwarded
|
||||
XCTAssertNoThrow(XCTAssertEqual(nil, try self.channel.readInbound(as: IOData.self)))
|
||||
}
|
||||
|
||||
func testRequestsEnqueuedAfterErrorAreFailed() {
|
||||
struct DummyError: Error {}
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<Void>>()
|
||||
)
|
||||
)
|
||||
|
||||
self.channel.pipeline.fireErrorCaught(DummyError())
|
||||
|
||||
let p: EventLoopPromise<ValueWithRequestID<Void>>.Isolated = self.eventLoop.makePromise().assumeIsolated()
|
||||
XCTAssertThrowsError(
|
||||
try self.channel.writeOutbound(
|
||||
(
|
||||
ValueWithRequestID(
|
||||
requestID: 1,
|
||||
value: IOData.byteBuffer(self.buffer)
|
||||
), p
|
||||
)
|
||||
)
|
||||
) { error in
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRequestsEnqueuedJustBeforeErrorAreFailed() {
|
||||
struct DummyError1: Error {}
|
||||
struct DummyError2: Error {}
|
||||
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<Void>>()
|
||||
)
|
||||
)
|
||||
|
||||
let p: EventLoopPromise<ValueWithRequestID<Void>>.Isolated = self.eventLoop.makePromise().assumeIsolated()
|
||||
// right now, everything's still okay so the enqueued request won't immediately be failed
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.writeOutbound(
|
||||
(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(self.buffer)),
|
||||
p
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
// but whilst we're waiting for the response, an error turns up
|
||||
self.channel.pipeline.fireErrorCaught(DummyError1())
|
||||
|
||||
// we'll also fire a second error through the pipeline that shouldn't do anything
|
||||
self.channel.pipeline.fireErrorCaught(DummyError2())
|
||||
|
||||
// and just after the error, the response arrives too (but too late)
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: ())))
|
||||
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertNotNil(error as? DummyError1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testClosedConnectionFailsOutstandingPromises() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<String>, ValueWithRequestID<Void>>()
|
||||
)
|
||||
)
|
||||
|
||||
let promise = self.eventLoop.makePromise(of: ValueWithRequestID<Void>.self).assumeIsolated()
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((ValueWithRequestID(requestID: 1, value: "Hello!"), promise)))
|
||||
|
||||
XCTAssertNoThrow(try self.channel.close().wait())
|
||||
promise.futureResult.whenComplete { result in
|
||||
switch result {
|
||||
case .success(_):
|
||||
XCTFail()
|
||||
case .failure(let error):
|
||||
XCTAssertTrue(error is NIOExtrasErrors.ClosedBeforeReceivingResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testOutOfOrderResponsesWork() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<String>, ValueWithRequestID<String>>()
|
||||
)
|
||||
)
|
||||
self.buffer.writeString("hello")
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
let p1: EventLoopPromise<ValueWithRequestID<String>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
let p2: EventLoopPromise<ValueWithRequestID<String>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
|
||||
// write requests
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((ValueWithRequestID(requestID: 1, value: "1"), p1)))
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((ValueWithRequestID(requestID: 2, value: "2"), p2)))
|
||||
// write responses but out of order
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID(requestID: 2, value: "okay 2")))
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: "okay 1")))
|
||||
// verify requests was forwarded
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "1"), try self.channel.readOutbound())
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 2, value: "2"), try self.channel.readOutbound())
|
||||
// verify responses were not forwarded
|
||||
XCTAssertEqual(nil, try self.channel.readInbound(as: ValueWithRequestID<IOData>.self))
|
||||
// verify the promises got succeeded with the response
|
||||
p1.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "okay 1"), value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
p2.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 2, value: "okay 2"), value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorOnResponseForNonExistantRequest() {
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandler(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<String>, ValueWithRequestID<String>>()
|
||||
)
|
||||
)
|
||||
self.buffer.writeString("hello")
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
let p1: EventLoopPromise<ValueWithRequestID<String>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
|
||||
// write request
|
||||
XCTAssertNoThrow(try self.channel.writeOutbound((ValueWithRequestID(requestID: 1, value: "1"), p1)))
|
||||
// write wrong response
|
||||
XCTAssertThrowsError(try self.channel.writeInbound(ValueWithRequestID(requestID: 2, value: "okay 2"))) {
|
||||
error in
|
||||
guard let error = error as? NIOExtrasErrors.ResponseForInvalidRequest<ValueWithRequestID<String>> else {
|
||||
XCTFail("wrong error")
|
||||
return
|
||||
}
|
||||
XCTAssertEqual(2, error.requestID)
|
||||
|
||||
}
|
||||
// verify requests was forwarded
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "1"), try self.channel.readOutbound())
|
||||
// verify responses were not forwarded
|
||||
XCTAssertEqual(nil, try self.channel.readInbound(as: ValueWithRequestID<IOData>.self))
|
||||
}
|
||||
|
||||
func testMoreRequestsAfterChannelInactiveFail() {
|
||||
final class EmitRequestOnInactiveHandler: ChannelDuplexHandler {
|
||||
typealias InboundIn = Never
|
||||
typealias OutboundIn = (ValueWithRequestID<IOData>, EventLoopPromise<ValueWithRequestID<String>>.Isolated)
|
||||
typealias OutboundOut = (ValueWithRequestID<IOData>, EventLoopPromise<ValueWithRequestID<String>>.Isolated)
|
||||
|
||||
func channelInactive(context: ChannelHandlerContext) {
|
||||
let responsePromise = context.eventLoop.makePromise(of: ValueWithRequestID<String>.self)
|
||||
.assumeIsolated()
|
||||
let writePromise = context.eventLoop.makePromise(of: Void.self)
|
||||
context.writeAndFlush(
|
||||
self.wrapOutboundOut(
|
||||
(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(ByteBuffer(string: "hi"))),
|
||||
responsePromise
|
||||
)
|
||||
),
|
||||
promise: writePromise
|
||||
)
|
||||
var writePromiseCompleted = false
|
||||
defer {
|
||||
XCTAssertTrue(writePromiseCompleted)
|
||||
}
|
||||
var responsePromiseCompleted = false
|
||||
defer {
|
||||
XCTAssertTrue(responsePromiseCompleted)
|
||||
}
|
||||
writePromise.futureResult.assumeIsolated().whenComplete { result in
|
||||
writePromiseCompleted = true
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("shouldn't succeed")
|
||||
case .failure(let error):
|
||||
XCTAssertEqual(.ioOnClosedChannel, error as? ChannelError)
|
||||
}
|
||||
}
|
||||
responsePromise.futureResult.whenComplete { result in
|
||||
responsePromiseCompleted = true
|
||||
switch result {
|
||||
case .success:
|
||||
XCTFail("shouldn't succeed")
|
||||
case .failure(let error):
|
||||
XCTAssertEqual(.ioOnClosedChannel, error as? ChannelError)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.pipeline.syncOperations.addHandlers(
|
||||
NIORequestIsolatedResponseWithIDHandler<ValueWithRequestID<IOData>, ValueWithRequestID<String>>(),
|
||||
EmitRequestOnInactiveHandler()
|
||||
)
|
||||
)
|
||||
self.buffer.writeString("hello")
|
||||
|
||||
// pretend to connect to the EmbeddedChannel knows it's supposed to be active
|
||||
XCTAssertNoThrow(try self.channel.connect(to: .init(ipAddress: "1.2.3.4", port: 5)).wait())
|
||||
|
||||
let p: EventLoopPromise<ValueWithRequestID<String>>.Isolated = self.channel.eventLoop.makePromise()
|
||||
.assumeIsolated()
|
||||
// write request
|
||||
XCTAssertNoThrow(
|
||||
try self.channel.writeOutbound(
|
||||
(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(self.buffer)),
|
||||
p
|
||||
)
|
||||
)
|
||||
)
|
||||
// write response
|
||||
XCTAssertNoThrow(try self.channel.writeInbound(ValueWithRequestID(requestID: 1, value: "okay")))
|
||||
|
||||
// verify request was forwarded
|
||||
XCTAssertEqual(
|
||||
ValueWithRequestID(requestID: 1, value: IOData.byteBuffer(self.buffer)),
|
||||
try self.channel.readOutbound()
|
||||
)
|
||||
|
||||
// verify the promise got succeeded with the response
|
||||
p.futureResult.whenComplete { res in
|
||||
switch res {
|
||||
case .success(let value):
|
||||
XCTAssertEqual(ValueWithRequestID(requestID: 1, value: "okay"), value)
|
||||
case .failure(_):
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ValueWithRequestID<T>: NIORequestIdentifiable {
|
||||
typealias RequestID = Int
|
||||
|
||||
|
||||
Reference in new issue
Block a user