ClientInterceptors.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 NIO
  17. /// A base class for client interceptors.
  18. ///
  19. /// Interceptors allow request and response parts to be observed, mutated or dropped as necessary.
  20. /// The default behaviour for this base class is to forward any events to the next interceptor.
  21. ///
  22. /// Interceptors may observe a number of different events:
  23. /// - receiving response parts with `receive(_:context:)`,
  24. /// - receiving errors with `errorCaught(_:context:)`,
  25. /// - sending request parts with `send(_:promise:context:)`, and
  26. /// - RPC cancellation with `cancel(context:)`.
  27. ///
  28. /// These events flow through a pipeline of interceptors for each RPC. Request parts sent from the
  29. /// call object (e.g. `UnaryCall`, `BidirectionalStreamingCall`) will traverse the pipeline in the
  30. /// outbound direction from its tail via `send(_:context:)` eventually reaching the head of the
  31. /// pipeline where it will be sent sent to the server.
  32. ///
  33. /// Response parts, or errors, received from the transport fill be fired in the inbound direction
  34. /// back through the interceptor pipeline via `receive(_:context:)` and `errorCaught(_:context:)`,
  35. /// respectively. Note that the `end` response part and any error received are terminal: the
  36. /// pipeline will be torn down once these parts reach the the tail and are a signal that the
  37. /// interceptor should free up any resources it may be using.
  38. ///
  39. /// Each of the interceptor functions is provided with a `context` which exposes analogous functions
  40. /// (`receive(_:)`, `errorCaught(_:)`, `send(_:promise:)`, and `cancel(promise:)`) which may be
  41. /// called to forward events to the next interceptor in the appropriate direction.
  42. ///
  43. /// ### Thread Safety
  44. ///
  45. /// Functions on `context` are not thread safe and **must** be called on the `EventLoop` found on
  46. /// the `context`. Since each interceptor is invoked on the same `EventLoop` this does not usually
  47. /// require any extra attention. However, if work is done on a `DispatchQueue` or _other_
  48. /// `EventLoop` then implementers should ensure that they use `context` from the correct
  49. /// `EventLoop`.
  50. open class ClientInterceptor<Request, Response> {
  51. public init() {}
  52. /// Called when the interceptor has received a response part to handle.
  53. /// - Parameters:
  54. /// - part: The response part which has been received from the server.
  55. /// - context: An interceptor context which may be used to forward the response part.
  56. open func receive(
  57. _ part: GRPCClientResponsePart<Response>,
  58. context: ClientInterceptorContext<Request, Response>
  59. ) {
  60. context.receive(part)
  61. }
  62. /// Called when the interceptor has received an error.
  63. /// - Parameters:
  64. /// - error: The error.
  65. /// - context: An interceptor context which may be used to forward the error.
  66. open func errorCaught(
  67. _ error: Error,
  68. context: ClientInterceptorContext<Request, Response>
  69. ) {
  70. context.errorCaught(error)
  71. }
  72. /// Called when the interceptor has received a request part to handle.
  73. /// - Parameters:
  74. /// - part: The request part which should be sent to the server.
  75. /// - promise: A promise which should be completed when the response part has been handled.
  76. /// - context: An interceptor context which may be used to forward the request part.
  77. open func send(
  78. _ part: GRPCClientRequestPart<Request>,
  79. promise: EventLoopPromise<Void>?,
  80. context: ClientInterceptorContext<Request, Response>
  81. ) {
  82. context.send(part, promise: promise)
  83. }
  84. /// Called when the interceptor has received a request to cancel the RPC.
  85. /// - Parameters:
  86. /// - promise: A promise which should be cancellation request has been handled.
  87. /// - context: An interceptor context which may be used to forward the cancellation request.
  88. open func cancel(
  89. promise: EventLoopPromise<Void>?,
  90. context: ClientInterceptorContext<Request, Response>
  91. ) {
  92. context.cancel(promise: promise)
  93. }
  94. }