ClientInterceptorContext.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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: ClientResponsePart<Response>) {
  70. self.eventLoop.assertInEventLoop()
  71. self.nextInbound?.invokeReceive(part)
  72. }
  73. /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one.
  74. ///
  75. /// - Parameters:
  76. /// - part: The request part to forward.
  77. /// - promise: The promise the complete when the part has been written.
  78. /// - Important: This *must* to be called from the `eventLoop`.
  79. public func send(
  80. _ part: ClientRequestPart<Request>,
  81. promise: EventLoopPromise<Void>?
  82. ) {
  83. self.eventLoop.assertInEventLoop()
  84. if let outbound = self.nextOutbound {
  85. outbound.invokeSend(part, promise: promise)
  86. } else {
  87. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  88. }
  89. }
  90. /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline.
  91. ///
  92. /// - Parameter promise: The promise to complete with the outcome of the cancellation request.
  93. /// - Important: This *must* to be called from the `eventLoop`.
  94. public func cancel(promise: EventLoopPromise<Void>?) {
  95. self.eventLoop.assertInEventLoop()
  96. if let outbound = self.nextOutbound {
  97. outbound.invokeCancel(promise: promise)
  98. } else {
  99. // The RPC has already been completed. Should cancellation fail?
  100. promise?.succeed(())
  101. }
  102. }
  103. }
  104. extension ClientInterceptorContext {
  105. internal func invokeReceive(_ part: ClientResponsePart<Response>) {
  106. self.eventLoop.assertInEventLoop()
  107. self.interceptor.receive(part, context: self)
  108. }
  109. @inlinable
  110. internal func invokeSend(_ part: ClientRequestPart<Request>, promise: EventLoopPromise<Void>?) {
  111. self.eventLoop.assertInEventLoop()
  112. self.interceptor.send(part, promise: promise, context: self)
  113. }
  114. internal func invokeCancel(promise: EventLoopPromise<Void>?) {
  115. self.eventLoop.assertInEventLoop()
  116. self.interceptor.cancel(promise: promise, context: self)
  117. }
  118. }