ServerInterceptorContext.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 ServerInterceptorContext<Request, Response> {
  19. /// The interceptor this context is for.
  20. @usableFromInline
  21. internal let interceptor: ServerInterceptor<Request, Response>
  22. /// The pipeline this context is associated with.
  23. @usableFromInline
  24. internal let _pipeline: ServerInterceptorPipeline<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.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.path
  43. }
  44. /// The address of the remote peer.
  45. public var remoteAddress: SocketAddress? {
  46. return self._pipeline.remoteAddress
  47. }
  48. /// A 'UserInfo' dictionary.
  49. ///
  50. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  51. /// reference wrapped `UserInfo`. The contexts passed to the service provider share the same
  52. /// reference. As such this may be used as a mechanism to pass information between interceptors
  53. /// and service providers.
  54. /// - Important: `userInfo` *must* be accessed from the context's `eventLoop` in order to ensure
  55. /// thread-safety.
  56. public var userInfo: UserInfo {
  57. get {
  58. return self._pipeline.userInfoRef.value
  59. }
  60. nonmutating set {
  61. self._pipeline.userInfoRef.value = newValue
  62. }
  63. }
  64. /// Construct a `ServerInterceptorContext` for the interceptor at the given index within the
  65. /// interceptor pipeline.
  66. @inlinable
  67. internal init(
  68. for interceptor: ServerInterceptor<Request, Response>,
  69. atIndex index: Int,
  70. in pipeline: ServerInterceptorPipeline<Request, Response>
  71. ) {
  72. self.interceptor = interceptor
  73. self._pipeline = pipeline
  74. self._index = index
  75. }
  76. /// Forwards the request part to the next inbound interceptor in the pipeline, if there is one.
  77. ///
  78. /// - Parameter part: The request part to forward.
  79. /// - Important: This *must* to be called from the `eventLoop`.
  80. @inlinable
  81. public func receive(_ part: GRPCServerRequestPart<Request>) {
  82. self._pipeline.invokeReceive(part, fromContextAtIndex: self._index)
  83. }
  84. /// Forwards the response part to the next outbound interceptor in the pipeline, if there is one.
  85. ///
  86. /// - Parameters:
  87. /// - part: The response part to forward.
  88. /// - promise: The promise the complete when the part has been written.
  89. /// - Important: This *must* to be called from the `eventLoop`.
  90. @inlinable
  91. public func send(
  92. _ part: GRPCServerResponsePart<Response>,
  93. promise: EventLoopPromise<Void>?
  94. ) {
  95. self._pipeline.invokeSend(part, promise: promise, fromContextAtIndex: self._index)
  96. }
  97. }