ClientInterceptorContext.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: ClientInterceptor<Request, Response>
  22. /// The pipeline this context is associated with.
  23. @usableFromInline
  24. internal let _pipeline: ClientInterceptorPipeline<Request, Response>
  25. /// The index of this context's interceptor within the pipeline.
  26. @usableFromInline
  27. internal let _index: Int
  28. /// The `EventLoop` this interceptor pipeline is being executed on.
  29. public var eventLoop: EventLoop {
  30. return self._pipeline.eventLoop
  31. }
  32. /// A logger.
  33. public var logger: Logger {
  34. return self._pipeline.logger
  35. }
  36. /// The type of the RPC, e.g. "unary".
  37. public var type: GRPCCallType {
  38. return self._pipeline.details.type
  39. }
  40. /// The path of the RPC in the format "/Service/Method", e.g. "/echo.Echo/Get".
  41. public var path: String {
  42. return self._pipeline.details.path
  43. }
  44. /// The options used to invoke the call.
  45. public var options: CallOptions {
  46. return self._pipeline.details.options
  47. }
  48. /// Construct a `ClientInterceptorContext` for the interceptor at the given index within in
  49. /// interceptor pipeline.
  50. @inlinable
  51. internal init(
  52. for interceptor: ClientInterceptor<Request, Response>,
  53. atIndex index: Int,
  54. in pipeline: ClientInterceptorPipeline<Request, Response>
  55. ) {
  56. self.interceptor = interceptor
  57. self._pipeline = pipeline
  58. self._index = index
  59. }
  60. /// Forwards the response part to the next inbound interceptor in the pipeline, if there is one.
  61. ///
  62. /// - Parameter part: The response part to forward.
  63. /// - Important: This *must* to be called from the `eventLoop`.
  64. @inlinable
  65. public func receive(_ part: GRPCClientResponsePart<Response>) {
  66. self.eventLoop.assertInEventLoop()
  67. self._pipeline.invokeReceive(part, fromContextAtIndex: self._index)
  68. }
  69. /// Forwards the error to the next inbound interceptor in the pipeline, if there is one.
  70. ///
  71. /// - Parameter error: The error to forward.
  72. /// - Important: This *must* to be called from the `eventLoop`.
  73. @inlinable
  74. public func errorCaught(_ error: Error) {
  75. self.eventLoop.assertInEventLoop()
  76. self._pipeline.invokeErrorCaught(error, fromContextAtIndex: self._index)
  77. }
  78. /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one.
  79. ///
  80. /// - Parameters:
  81. /// - part: The request part to forward.
  82. /// - promise: The promise the complete when the part has been written.
  83. /// - Important: This *must* to be called from the `eventLoop`.
  84. @inlinable
  85. public func send(
  86. _ part: GRPCClientRequestPart<Request>,
  87. promise: EventLoopPromise<Void>?
  88. ) {
  89. self.eventLoop.assertInEventLoop()
  90. self._pipeline.invokeSend(part, promise: promise, fromContextAtIndex: self._index)
  91. }
  92. /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline.
  93. ///
  94. /// - Parameter promise: The promise to complete with the outcome of the cancellation request.
  95. /// - Important: This *must* to be called from the `eventLoop`.
  96. @inlinable
  97. public func cancel(promise: EventLoopPromise<Void>?) {
  98. self.eventLoop.assertInEventLoop()
  99. self._pipeline.invokeCancel(promise: promise, fromContextAtIndex: self._index)
  100. }
  101. }