ClientInterceptorContext.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public struct ClientInterceptorContext<Request, Response> {
  19. /// The interceptor this context is for.
  20. @usableFromInline
  21. internal let interceptor: AnyClientInterceptor<Request, Response>
  22. /// The pipeline this context is associated with.
  23. private let pipeline: ClientInterceptorPipeline<Request, Response>
  24. /// The index of this context's interceptor within the pipeline.
  25. private let index: Int
  26. // The next context in the inbound direction, if one exists.
  27. private var nextInbound: ClientInterceptorContext<Request, Response>? {
  28. return self.pipeline.nextInboundContext(forIndex: self.index)
  29. }
  30. // The next context in the outbound direction, if one exists.
  31. private var nextOutbound: ClientInterceptorContext<Request, Response>? {
  32. return self.pipeline.nextOutboundContext(forIndex: self.index)
  33. }
  34. /// The `EventLoop` this interceptor pipeline is being executed on.
  35. public var eventLoop: EventLoop {
  36. return self.pipeline.eventLoop
  37. }
  38. /// A logger.
  39. public var logger: Logger {
  40. return self.pipeline.logger
  41. }
  42. /// The type of the RPC, e.g. "unary".
  43. public var type: GRPCCallType {
  44. return self.pipeline.details.type
  45. }
  46. /// The path of the RPC in the format "/Service/Method", e.g. "/echo.Echo/Get".
  47. public var path: String {
  48. return self.pipeline.details.path
  49. }
  50. /// The options used to invoke the call.
  51. public var options: CallOptions {
  52. return self.pipeline.details.options
  53. }
  54. /// Construct a `ClientInterceptorContext` for the interceptor at the given index within in
  55. /// interceptor pipeline.
  56. internal init(
  57. for interceptor: AnyClientInterceptor<Request, Response>,
  58. atIndex index: Int,
  59. in pipeline: ClientInterceptorPipeline<Request, Response>
  60. ) {
  61. self.interceptor = interceptor
  62. self.pipeline = pipeline
  63. self.index = index
  64. }
  65. /// Forwards the response part to the next inbound interceptor in the pipeline, if there is one.
  66. ///
  67. /// - Parameter part: The response part to forward.
  68. /// - Important: This *must* to be called from the `eventLoop`.
  69. public func receive(_ part: GRPCClientResponsePart<Response>) {
  70. self.eventLoop.assertInEventLoop()
  71. self.nextInbound?.invokeReceive(part)
  72. }
  73. /// Forwards the error to the next inbound interceptor in the pipeline, if there is one.
  74. ///
  75. /// - Parameter error: The error to forward.
  76. /// - Important: This *must* to be called from the `eventLoop`.
  77. public func errorCaught(_ error: Error) {
  78. self.eventLoop.assertInEventLoop()
  79. self.nextInbound?.invokeErrorCaught(error)
  80. }
  81. /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one.
  82. ///
  83. /// - Parameters:
  84. /// - part: The request part to forward.
  85. /// - promise: The promise the complete when the part has been written.
  86. /// - Important: This *must* to be called from the `eventLoop`.
  87. public func send(
  88. _ part: GRPCClientRequestPart<Request>,
  89. promise: EventLoopPromise<Void>?
  90. ) {
  91. self.eventLoop.assertInEventLoop()
  92. if let outbound = self.nextOutbound {
  93. outbound.invokeSend(part, promise: promise)
  94. } else {
  95. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  96. }
  97. }
  98. /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline.
  99. ///
  100. /// - Parameter promise: The promise to complete with the outcome of the cancellation request.
  101. /// - Important: This *must* to be called from the `eventLoop`.
  102. public func cancel(promise: EventLoopPromise<Void>?) {
  103. self.eventLoop.assertInEventLoop()
  104. if let outbound = self.nextOutbound {
  105. outbound.invokeCancel(promise: promise)
  106. } else {
  107. // The RPC has already been completed. Should cancellation fail?
  108. promise?.succeed(())
  109. }
  110. }
  111. }
  112. extension ClientInterceptorContext {
  113. internal func invokeReceive(_ part: GRPCClientResponsePart<Response>) {
  114. self.eventLoop.assertInEventLoop()
  115. self.interceptor.receive(part, context: self)
  116. }
  117. @inlinable
  118. internal func invokeSend(
  119. _ part: GRPCClientRequestPart<Request>,
  120. promise: EventLoopPromise<Void>?
  121. ) {
  122. self.eventLoop.assertInEventLoop()
  123. self.interceptor.send(part, promise: promise, context: self)
  124. }
  125. internal func invokeCancel(promise: EventLoopPromise<Void>?) {
  126. self.eventLoop.assertInEventLoop()
  127. self.interceptor.cancel(promise: promise, context: self)
  128. }
  129. internal func invokeErrorCaught(_ error: Error) {
  130. self.eventLoop.assertInEventLoop()
  131. self.interceptor.errorCaught(error, context: self)
  132. }
  133. }