|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2019 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | + |
| 16 | +// THIS FILE IS MOSTLY COPIED FROM swift-nio-extras |
| 17 | + |
| 18 | +import NIO |
| 19 | +import NIOHTTP1 |
| 20 | + |
| 21 | +public final class MakeFullRequestHandler: ChannelOutboundHandler { |
| 22 | + public typealias OutboundOut = HTTPClientRequestPart |
| 23 | + public typealias OutboundIn = HTTPRequestHead |
| 24 | + |
| 25 | + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { |
| 26 | + let req = self.unwrapOutboundIn(data) |
| 27 | + |
| 28 | + context.write(self.wrapOutboundOut(.head(req)), promise: nil) |
| 29 | + context.write(self.wrapOutboundOut(.end(nil)), promise: promise) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// `RequestResponseHandler` receives a `Request` alongside an `EventLoopPromise<Response>` from the `Channel`'s |
| 34 | +/// outbound side. It will fulfill the promise with the `Response` once it's received from the `Channel`'s inbound |
| 35 | +/// side. |
| 36 | +/// |
| 37 | +/// `RequestResponseHandler` does support pipelining `Request`s and it will send them pipelined further down the |
| 38 | +/// `Channel`. Should `RequestResponseHandler` receive an error from the `Channel`, it will fail all promises meant for |
| 39 | +/// the outstanding `Reponse`s and close the `Channel`. All requests enqueued after an error occured will be immediately |
| 40 | +/// failed with the first error the channel received. |
| 41 | +/// |
| 42 | +/// `RequestResponseHandler` requires that the `Response`s arrive on `Channel` in the same order as the `Request`s |
| 43 | +/// were submitted. |
| 44 | +public final class RequestResponseHandler<Request, Response>: ChannelDuplexHandler { |
| 45 | + public typealias InboundIn = Response |
| 46 | + public typealias InboundOut = Never |
| 47 | + public typealias OutboundIn = (Request, EventLoopPromise<Response>) |
| 48 | + public typealias OutboundOut = Request |
| 49 | + |
| 50 | + private enum State { |
| 51 | + case operational |
| 52 | + case error(Error) |
| 53 | + |
| 54 | + var isOperational: Bool { |
| 55 | + switch self { |
| 56 | + case .operational: |
| 57 | + return true |
| 58 | + case .error: |
| 59 | + return false |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private var state: State = .operational |
| 65 | + private var promiseBuffer: CircularBuffer<EventLoopPromise<Response>> |
| 66 | + |
| 67 | + |
| 68 | + /// Create a new `RequestResponseHandler`. |
| 69 | + /// |
| 70 | + /// - parameters: |
| 71 | + /// - initialBufferCapacity: `RequestResponseHandler` saves the promises for all outstanding responses in a |
| 72 | + /// buffer. `initialBufferCapacity` is the initial capacity for this buffer. You usually do not need to set |
| 73 | + /// this parameter unless you intend to pipeline very deeply and don't want the buffer to resize. |
| 74 | + public init(initialBufferCapacity: Int = 4) { |
| 75 | + self.promiseBuffer = CircularBuffer(initialCapacity: initialBufferCapacity) |
| 76 | + } |
| 77 | + |
| 78 | + public func channelInactive(context: ChannelHandlerContext) { |
| 79 | + switch self.state { |
| 80 | + case .error: |
| 81 | + // We failed any outstanding promises when we entered the error state and will fail any |
| 82 | + // new promises in write. |
| 83 | + assert(self.promiseBuffer.count == 0) |
| 84 | + case .operational: |
| 85 | + let promiseBuffer = self.promiseBuffer |
| 86 | + self.promiseBuffer.removeAll() |
| 87 | + promiseBuffer.forEach { promise in |
| 88 | + promise.fail(ChannelError.eof) |
| 89 | + } |
| 90 | + } |
| 91 | + context.fireChannelInactive() |
| 92 | + } |
| 93 | + |
| 94 | + public func channelRead(context: ChannelHandlerContext, data: NIOAny) { |
| 95 | + guard self.state.isOperational else { |
| 96 | + // we're in an error state, ignore further responses |
| 97 | + assert(self.promiseBuffer.count == 0) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + let response = self.unwrapInboundIn(data) |
| 102 | + let promise = self.promiseBuffer.removeFirst() |
| 103 | + |
| 104 | + promise.succeed(response) |
| 105 | + } |
| 106 | + |
| 107 | + public func errorCaught(context: ChannelHandlerContext, error: Error) { |
| 108 | + guard self.state.isOperational else { |
| 109 | + assert(self.promiseBuffer.count == 0) |
| 110 | + return |
| 111 | + } |
| 112 | + self.state = .error(error) |
| 113 | + let promiseBuffer = self.promiseBuffer |
| 114 | + self.promiseBuffer.removeAll() |
| 115 | + context.close(promise: nil) |
| 116 | + promiseBuffer.forEach { |
| 117 | + $0.fail(error) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { |
| 122 | + let (request, responsePromise) = self.unwrapOutboundIn(data) |
| 123 | + switch self.state { |
| 124 | + case .error(let error): |
| 125 | + assert(self.promiseBuffer.count == 0) |
| 126 | + responsePromise.fail(error) |
| 127 | + promise?.fail(error) |
| 128 | + case .operational: |
| 129 | + self.promiseBuffer.append(responsePromise) |
| 130 | + context.write(self.wrapOutboundOut(request), promise: promise) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments