ServerInterceptorContext.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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: AnyServerInterceptor<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 next context in the inbound direction, if one exists.
  29. @inlinable
  30. internal var _nextInbound: ServerInterceptorContext<Request, Response>? {
  31. return self._pipeline.nextInboundContext(forIndex: self._index)
  32. }
  33. // The next context in the outbound direction, if one exists.
  34. @inlinable
  35. internal var _nextOutbound: ServerInterceptorContext<Request, Response>? {
  36. return self._pipeline.nextOutboundContext(forIndex: self._index)
  37. }
  38. /// The `EventLoop` this interceptor pipeline is being executed on.
  39. public var eventLoop: EventLoop {
  40. return self._pipeline.eventLoop
  41. }
  42. /// A logger.
  43. public var logger: Logger {
  44. return self._pipeline.logger
  45. }
  46. /// The type of the RPC, e.g. "unary".
  47. public var type: GRPCCallType {
  48. return self._pipeline.type
  49. }
  50. /// The path of the RPC in the format "/Service/Method", e.g. "/echo.Echo/Get".
  51. public var path: String {
  52. return self._pipeline.path
  53. }
  54. /// The address of the remote peer.
  55. public var remoteAddress: SocketAddress? {
  56. return self._pipeline.remoteAddress
  57. }
  58. /// A 'UserInfo' dictionary.
  59. ///
  60. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  61. /// reference wrapped `UserInfo`. The contexts passed to the service provider share the same
  62. /// reference. As such this may be used as a mechanism to pass information between interceptors
  63. /// and service providers.
  64. /// - Important: `userInfo` *must* be accessed from the context's `eventLoop` in order to ensure
  65. /// thread-safety.
  66. public var userInfo: UserInfo {
  67. get {
  68. return self._pipeline.userInfoRef.value
  69. }
  70. nonmutating set {
  71. self._pipeline.userInfoRef.value = newValue
  72. }
  73. }
  74. /// Construct a `ServerInterceptorContext` for the interceptor at the given index within the
  75. /// interceptor pipeline.
  76. @inlinable
  77. internal init(
  78. for interceptor: AnyServerInterceptor<Request, Response>,
  79. atIndex index: Int,
  80. in pipeline: ServerInterceptorPipeline<Request, Response>
  81. ) {
  82. self.interceptor = interceptor
  83. self._pipeline = pipeline
  84. self._index = index
  85. }
  86. /// Forwards the request part to the next inbound interceptor in the pipeline, if there is one.
  87. ///
  88. /// - Parameter part: The request part to forward.
  89. /// - Important: This *must* to be called from the `eventLoop`.
  90. public func receive(_ part: GRPCServerRequestPart<Request>) {
  91. self._nextInbound?.invokeReceive(part)
  92. }
  93. /// Forwards the response part to the next outbound interceptor in the pipeline, if there is one.
  94. ///
  95. /// - Parameters:
  96. /// - part: The response part to forward.
  97. /// - promise: The promise the complete when the part has been written.
  98. /// - Important: This *must* to be called from the `eventLoop`.
  99. public func send(
  100. _ part: GRPCServerResponsePart<Response>,
  101. promise: EventLoopPromise<Void>?
  102. ) {
  103. if let outbound = self._nextOutbound {
  104. outbound.invokeSend(part, promise: promise)
  105. } else {
  106. promise?.fail(GRPCError.AlreadyComplete())
  107. }
  108. }
  109. }
  110. extension ServerInterceptorContext {
  111. @inlinable
  112. internal func invokeReceive(_ part: GRPCServerRequestPart<Request>) {
  113. self.eventLoop.assertInEventLoop()
  114. self.interceptor.receive(part, context: self)
  115. }
  116. @inlinable
  117. internal func invokeSend(
  118. _ part: GRPCServerResponsePart<Response>,
  119. promise: EventLoopPromise<Void>?
  120. ) {
  121. self.eventLoop.assertInEventLoop()
  122. self.interceptor.send(part, promise: promise, context: self)
  123. }
  124. }