ClientInterceptorContext.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 pipeline this context is associated with.
  20. private let pipeline: ClientInterceptorPipeline<Request, Response>
  21. /// The index of this context's interceptor within the pipeline.
  22. private let index: Int
  23. // The next context in the inbound direction, if one exists.
  24. private var nextInbound: ClientInterceptorContext<Request, Response>? {
  25. return self.pipeline.context(atIndex: self.index + 1)
  26. }
  27. // The next context in the outbound direction, if one exists.
  28. private var nextOutbound: ClientInterceptorContext<Request, Response>? {
  29. return self.pipeline.context(atIndex: self.index - 1)
  30. }
  31. /// The `EventLoop` this interceptor pipeline is being executed on.
  32. public var eventLoop: EventLoop {
  33. return self.pipeline.eventLoop
  34. }
  35. /// A logger.
  36. public var logger: Logger {
  37. return self.pipeline.logger
  38. }
  39. /// Construct a `ClientInterceptorContext` for the interceptor at the given index within in
  40. /// interceptor pipeline.
  41. internal init(pipeline: ClientInterceptorPipeline<Request, Response>, index: Int) {
  42. self.pipeline = pipeline
  43. self.index = index
  44. }
  45. /// Forwards the response part to the next inbound interceptor in the pipeline, if there is one.
  46. ///
  47. /// - Parameter part: The response part to forward.
  48. /// - Important: This *must* to be called from the `eventLoop`.
  49. public func read(_ part: ClientResponsePart<Response>) {
  50. self._read(part)
  51. }
  52. /// Forwards the request part to the next outbound interceptor in the pipeline, if there is one.
  53. ///
  54. /// - Parameters:
  55. /// - part: The request part to forward.
  56. /// - promise: The promise the complete when the part has been written.
  57. /// - Important: This *must* to be called from the `eventLoop`.
  58. public func write(
  59. _ part: ClientRequestPart<Request>,
  60. promise: EventLoopPromise<Void>?
  61. ) {
  62. self._write(part, promise: promise)
  63. }
  64. /// Forwards a request to cancel the RPC to the next outbound interceptor in the pipeline.
  65. ///
  66. /// - Parameter promise: The promise to complete with the outcome of the cancellation request.
  67. /// - Important: This *must* to be called from the `eventLoop`.
  68. public func cancel(promise: EventLoopPromise<Void>?) {
  69. self._cancel(promise: promise)
  70. }
  71. }
  72. extension ClientInterceptorContext {
  73. private func _read(_ part: ClientResponsePart<Response>) {
  74. self.eventLoop.assertInEventLoop()
  75. self.nextInbound?.invokeRead(part)
  76. }
  77. private func _write(
  78. _ part: ClientRequestPart<Request>,
  79. promise: EventLoopPromise<Void>?
  80. ) {
  81. self.eventLoop.assertInEventLoop()
  82. if let outbound = self.nextOutbound {
  83. outbound.invokeWrite(part, promise: promise)
  84. } else {
  85. promise?.fail(GRPCStatus(code: .unavailable, message: "The RPC has already completed"))
  86. }
  87. }
  88. private func _cancel(promise: EventLoopPromise<Void>?) {
  89. self.eventLoop.assertInEventLoop()
  90. if let outbound = self.nextOutbound {
  91. outbound.invokeCancel(promise: promise)
  92. } else {
  93. // The RPC has already been completed. Should cancellation fail?
  94. promise?.succeed(())
  95. }
  96. }
  97. internal func invokeRead(_ part: ClientResponsePart<Response>) {
  98. self.eventLoop.assertInEventLoop()
  99. fatalError("TODO: call the interceptor")
  100. }
  101. internal func invokeWrite(_ part: ClientRequestPart<Request>, promise: EventLoopPromise<Void>?) {
  102. self.eventLoop.assertInEventLoop()
  103. fatalError("TODO: call the interceptor")
  104. }
  105. internal func invokeCancel(promise: EventLoopPromise<Void>?) {
  106. self.eventLoop.assertInEventLoop()
  107. fatalError("TODO: call the interceptor")
  108. }
  109. }