ServerInterceptors.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 server interceptors.
  18. ///
  19. /// Interceptors allow request and response and response parts to be observed, mutated or dropped
  20. /// as necessary. The default behaviour for this base class is to forward any events to the next
  21. /// interceptor.
  22. ///
  23. /// Interceptors may observe two different types of event:
  24. /// - receiving request parts with `receive(_:context:)`,
  25. /// - sending response parts with `send(_:promise:context:)`.
  26. ///
  27. /// These events flow through a pipeline of interceptors for each RPC. Request parts will enter
  28. /// the head of the interceptor pipeline once the request router has determined that there is a
  29. /// service provider which is able to handle the request stream. Response parts from the service
  30. /// provider enter the tail of the interceptor pipeline and will be sent to the client after
  31. /// traversing the pipeline through to the head.
  32. ///
  33. /// Each of the interceptor functions is provided with a `context` which exposes analogous functions
  34. /// (`receive(_:)` and `send(_:promise:)`) which may be called to forward events to the next
  35. /// interceptor.
  36. ///
  37. /// ### Thread Safety
  38. ///
  39. /// Functions on `context` are not thread safe and **must** be called on the `EventLoop` found on
  40. /// the `context`. Since each interceptor is invoked on the same `EventLoop` this does not usually
  41. /// require any extra attention. However, if work is done on a `DispatchQueue` or _other_
  42. /// `EventLoop` then implementers should ensure that they use `context` from the correct
  43. /// `EventLoop`.
  44. open class ServerInterceptor<Request, Response> {
  45. public init() {}
  46. /// Called when the interceptor has received a request part to handle.
  47. /// - Parameters:
  48. /// - part: The request part which has been received from the client.
  49. /// - context: An interceptor context which may be used to forward the response part.
  50. open func receive(
  51. _ part: ServerRequestPart<Request>,
  52. context: ServerInterceptorContext<Request, Response>
  53. ) {
  54. context.receive(part)
  55. }
  56. /// Called when the interceptor has received a response part to handle.
  57. /// - Parameters:
  58. /// - part: The request part which should be sent to the client.
  59. /// - promise: A promise which should be completed when the response part has been written.
  60. /// - context: An interceptor context which may be used to forward the request part.
  61. open func send(
  62. _ part: ServerResponsePart<Response>,
  63. promise: EventLoopPromise<Void>?,
  64. context: ServerInterceptorContext<Request, Response>
  65. ) {
  66. context.send(part, promise: promise)
  67. }
  68. }
  69. // MARK: - Any Interceptor
  70. /// A wrapping interceptor which delegates to the implementation of an underlying interceptor.
  71. internal struct AnyServerInterceptor<Request, Response> {
  72. internal enum Implementation {
  73. case base(ServerInterceptor<Request, Response>)
  74. }
  75. /// The underlying interceptor implementation.
  76. internal let _implementation: Implementation
  77. /// A user provided interceptor.
  78. /// - Parameter interceptor: The interceptor to wrap.
  79. /// - Returns: An `AnyServerInterceptor` which wraps `interceptor`.
  80. internal static func userProvided(
  81. _ interceptor: ServerInterceptor<Request, Response>
  82. ) -> AnyServerInterceptor<Request, Response> {
  83. return .init(.base(interceptor))
  84. }
  85. private init(_ implementation: Implementation) {
  86. self._implementation = implementation
  87. }
  88. internal func receive(
  89. _ part: ServerRequestPart<Request>,
  90. context: ServerInterceptorContext<Request, Response>
  91. ) {
  92. switch self._implementation {
  93. case let .base(handler):
  94. handler.receive(part, context: context)
  95. }
  96. }
  97. internal func send(
  98. _ part: ServerResponsePart<Response>,
  99. promise: EventLoopPromise<Void>?,
  100. context: ServerInterceptorContext<Request, Response>
  101. ) {
  102. switch self._implementation {
  103. case let .base(handler):
  104. handler.send(part, promise: promise, context: context)
  105. }
  106. }
  107. }