ClientInterceptorContext.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /// Construct a `ClientInterceptorContext` for the interceptor at the given index within in
  51. /// interceptor pipeline.
  52. internal init(
  53. for interceptor: AnyClientInterceptor<Request, Response>,
  54. atIndex index: Int,
  55. in pipeline: ClientInterceptorPipeline<Request, Response>
  56. ) {
  57. self.interceptor = interceptor
  58. self.pipeline = pipeline
  59. self.index = index
  60. }
  61. /// Forwards the response part to the next inbound interceptor in the pipeline, if there is one.
  62. ///
  63. /// - Parameter part: The response part to forward.
  64. /// - Important: This *must* to be called from the `eventLoop`.
  65. public func read(_ part: ClientResponsePart<Response>) {
  66. self._read(part)
  67. }
  68. /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one.
  69. ///
  70. /// - Parameters:
  71. /// - part: The request part to forward.
  72. /// - promise: The promise the complete when the part has been written.
  73. /// - Important: This *must* to be called from the `eventLoop`.
  74. public func write(
  75. _ part: ClientRequestPart<Request>,
  76. promise: EventLoopPromise<Void>?
  77. ) {
  78. self._write(part, promise: promise)
  79. }
  80. /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline.
  81. ///
  82. /// - Parameter promise: The promise to complete with the outcome of the cancellation request.
  83. /// - Important: This *must* to be called from the `eventLoop`.
  84. public func cancel(promise: EventLoopPromise<Void>?) {
  85. self._cancel(promise: promise)
  86. }
  87. }
  88. extension ClientInterceptorContext {
  89. private func _read(_ part: ClientResponsePart<Response>) {
  90. self.eventLoop.assertInEventLoop()
  91. self.nextInbound?.invokeRead(part)
  92. }
  93. private func _write(
  94. _ part: ClientRequestPart<Request>,
  95. promise: EventLoopPromise<Void>?
  96. ) {
  97. self.eventLoop.assertInEventLoop()
  98. if let outbound = self.nextOutbound {
  99. outbound.invokeWrite(part, promise: promise)
  100. } else {
  101. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  102. }
  103. }
  104. private func _cancel(promise: EventLoopPromise<Void>?) {
  105. self.eventLoop.assertInEventLoop()
  106. if let outbound = self.nextOutbound {
  107. outbound.invokeCancel(promise: promise)
  108. } else {
  109. // The RPC has already been completed. Should cancellation fail?
  110. promise?.succeed(())
  111. }
  112. }
  113. internal func invokeRead(_ part: ClientResponsePart<Response>) {
  114. self.eventLoop.assertInEventLoop()
  115. self.interceptor.read(part, context: self)
  116. }
  117. @inlinable
  118. internal func invokeWrite(_ part: ClientRequestPart<Request>, promise: EventLoopPromise<Void>?) {
  119. self.eventLoop.assertInEventLoop()
  120. self.interceptor.write(part, promise: promise, context: self)
  121. }
  122. internal func invokeCancel(promise: EventLoopPromise<Void>?) {
  123. self.eventLoop.assertInEventLoop()
  124. self.interceptor.cancel(promise: promise, context: self)
  125. }
  126. }