ClientInterceptorPipeline.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Logging
  17. import NIO
  18. import NIOHPACK
  19. import NIOHTTP2
  20. /// A pipeline for intercepting client request and response streams.
  21. ///
  22. /// The interceptor pipeline lies between the call object (`UnaryCall`, `ClientStreamingCall`, etc.)
  23. /// and the transport used to send and receive messages from the server (a `NIO.Channel`). It holds
  24. /// a collection of interceptors which may be used to observe or alter messages as the travel
  25. /// through the pipeline.
  26. ///
  27. /// ```
  28. /// ┌───────────────────────────────────────────────────────────────────┐
  29. /// │ Call │
  30. /// └────────────────────────────────────────────────────────┬──────────┘
  31. /// │ write(_:promise) /
  32. /// │ cancel(promise:)
  33. /// ┌────────────────────────────────────────────────────────▼──────────┐
  34. /// │ InterceptorPipeline ╎ │
  35. /// │ ╎ │
  36. /// │ ┌──────────────────────────────────────────────────────▼────────┐ │
  37. /// │ │ Tail Interceptor (hands response parts to a callback) │ │
  38. /// │ └────────▲─────────────────────────────────────────────┬────────┘ │
  39. /// │ ╎ ╎ │
  40. /// │ ╎ (More interceptors) ╎ │
  41. /// │ ╎ ╎ │
  42. /// │ ┌────────┴─────────────────────────────────────────────▼────────┐ │
  43. /// │ │ Interceptor 2 │ │
  44. /// │ └────────▲─────────────────────────────────────────────┬────────┘ │
  45. /// │ ┌────────┴─────────────────────────────────────────────▼────────┐ │
  46. /// │ │ Interceptor 1 │ │
  47. /// │ └────────▲─────────────────────────────────────────────┬────────┘ │
  48. /// │ ┌────────┴─────────────────────────────────────────────▼────────┐ │
  49. /// │ │ Head Interceptor (interacts with transport) │ │
  50. /// │ └────────▲─────────────────────────────────────────────┬────────┘ │
  51. /// │ read(_:)╎ │ │
  52. /// └──────────▲─────────────────────────────────────────────┼──────────┘
  53. /// read(_:)│ │ write(_:promise:) /
  54. /// │ │ cancel(promise:)
  55. /// ┌──────────┴─────────────────────────────────────────────▼──────────┐
  56. /// │ ClientTransport │
  57. /// │ (a NIO.ChannelHandler) │
  58. /// ```
  59. internal final class ClientInterceptorPipeline<Request, Response> {
  60. /// A logger.
  61. internal let logger: Logger
  62. /// The `EventLoop` this RPC is being executed on.
  63. internal let eventLoop: EventLoop
  64. /// The contexts associated with the interceptors stored in this pipeline. Context will be removed
  65. /// once the RPC has completed.
  66. private var contexts: [ClientInterceptorContext<Request, Response>]
  67. /// Returns the context for the given index, if one exists.
  68. /// - Parameter index: The index of the `ClientInterceptorContext` to return.
  69. /// - Returns: The `ClientInterceptorContext` or `nil` if one does not exist for the given index.
  70. internal func context(atIndex index: Int) -> ClientInterceptorContext<Request, Response>? {
  71. return self.contexts[checked: index]
  72. }
  73. /// The context closest to the `NIO.Channel`, i.e. where inbound events originate. This will be
  74. /// `nil` once the RPC has completed.
  75. private var head: ClientInterceptorContext<Request, Response>? {
  76. return self.contexts.first
  77. }
  78. /// The context closest to the application, i.e. where outbound events originate. This will be
  79. /// `nil` once the RPC has completed.
  80. private var tail: ClientInterceptorContext<Request, Response>? {
  81. return self.contexts.last
  82. }
  83. internal init() {
  84. fatalError("Not yet implemented.")
  85. }
  86. /// Emit a response part message into the interceptor pipeline.
  87. ///
  88. /// This should be called by the transport layer when receiving a response part from the server.
  89. ///
  90. /// - Parameter part: The part to emit into the pipeline.
  91. /// - Important: This *must* to be called from the `eventLoop`.
  92. internal func read(_ part: ClientResponsePart<Response>) {
  93. self.eventLoop.assertInEventLoop()
  94. self.head?.invokeRead(part)
  95. }
  96. /// Writes a request message into the interceptor pipeline.
  97. ///
  98. /// This should be called by the call object to send requests parts to the transport.
  99. ///
  100. /// - Parameters:
  101. /// - part: The request part to write.
  102. /// - promise: A promise to complete when the request part has been successfully written.
  103. /// - Important: This *must* to be called from the `eventLoop`.
  104. internal func write(_ part: ClientRequestPart<Request>, promise: EventLoopPromise<Void>?) {
  105. self.eventLoop.assertInEventLoop()
  106. if let tail = self.tail {
  107. tail.invokeWrite(part, promise: promise)
  108. } else {
  109. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  110. }
  111. }
  112. /// Send a request to cancel the RPC through the interceptor pipeline.
  113. ///
  114. /// This should be called by the call object when attempting to cancel the RPC.
  115. ///
  116. /// - Parameter promise: A promise to complete when the cancellation request has been handled.
  117. /// - Important: This *must* to be called from the `eventLoop`.
  118. internal func cancel(promise: EventLoopPromise<Void>?) {
  119. self.eventLoop.assertInEventLoop()
  120. if let tail = self.tail {
  121. tail.invokeCancel(promise: promise)
  122. } else {
  123. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  124. }
  125. }
  126. }