ServerInterceptorContext.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /// A 'UserInfo' dictionary.
  55. ///
  56. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  57. /// reference wrapped `UserInfo`. The contexts passed to the service provider share the same
  58. /// reference. As such this may be used as a mechanism to pass information between interceptors
  59. /// and service providers.
  60. /// - Important: `userInfo` *must* be accessed from the context's `eventLoop` in order to ensure
  61. /// thread-safety.
  62. public var userInfo: UserInfo {
  63. get {
  64. return self._pipeline.userInfoRef.value
  65. }
  66. nonmutating set {
  67. self._pipeline.userInfoRef.value = newValue
  68. }
  69. }
  70. /// Construct a `ServerInterceptorContext` for the interceptor at the given index within the
  71. /// interceptor pipeline.
  72. @inlinable
  73. internal init(
  74. for interceptor: AnyServerInterceptor<Request, Response>,
  75. atIndex index: Int,
  76. in pipeline: ServerInterceptorPipeline<Request, Response>
  77. ) {
  78. self.interceptor = interceptor
  79. self._pipeline = pipeline
  80. self._index = index
  81. }
  82. /// Forwards the request part to the next inbound interceptor in the pipeline, if there is one.
  83. ///
  84. /// - Parameter part: The request part to forward.
  85. /// - Important: This *must* to be called from the `eventLoop`.
  86. public func receive(_ part: GRPCServerRequestPart<Request>) {
  87. self._nextInbound?.invokeReceive(part)
  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. public func send(
  96. _ part: GRPCServerResponsePart<Response>,
  97. promise: EventLoopPromise<Void>?
  98. ) {
  99. if let outbound = self._nextOutbound {
  100. outbound.invokeSend(part, promise: promise)
  101. } else {
  102. promise?.fail(GRPCError.AlreadyComplete())
  103. }
  104. }
  105. }
  106. extension ServerInterceptorContext {
  107. @inlinable
  108. internal func invokeReceive(_ part: GRPCServerRequestPart<Request>) {
  109. self.eventLoop.assertInEventLoop()
  110. self.interceptor.receive(part, context: self)
  111. }
  112. @inlinable
  113. internal func invokeSend(
  114. _ part: GRPCServerResponsePart<Response>,
  115. promise: EventLoopPromise<Void>?
  116. ) {
  117. self.eventLoop.assertInEventLoop()
  118. self.interceptor.send(part, promise: promise, context: self)
  119. }
  120. }