ServerInterceptorContext.swift 3.8 KB

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