ServerInterceptors.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 NIOCore
  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(_:context:)`` and ``send(_:promise:context:)``) 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>: @unchecked Sendable {
  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: GRPCServerRequestPart<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: GRPCServerResponsePart<Response>,
  63. promise: EventLoopPromise<Void>?,
  64. context: ServerInterceptorContext<Request, Response>
  65. ) {
  66. context.send(part, promise: promise)
  67. }
  68. }