ServerInterceptorContext.swift 4.2 KB

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