ServerInterceptorContext.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// Construct a `ServerInterceptorContext` for the interceptor at the given index within the
  50. /// interceptor pipeline.
  51. internal init(
  52. for interceptor: AnyServerInterceptor<Request, Response>,
  53. atIndex index: Int,
  54. in pipeline: ServerInterceptorPipeline<Request, Response>
  55. ) {
  56. self.interceptor = interceptor
  57. self.pipeline = pipeline
  58. self.index = index
  59. }
  60. /// Forwards the request part to the next inbound interceptor in the pipeline, if there is one.
  61. ///
  62. /// - Parameter part: The request part to forward.
  63. /// - Important: This *must* to be called from the `eventLoop`.
  64. public func receive(_ part: ServerRequestPart<Request>) {
  65. self.nextInbound?.invokeReceive(part)
  66. }
  67. /// Forwards the response part to the next outbound interceptor in the pipeline, if there is one.
  68. ///
  69. /// - Parameters:
  70. /// - part: The response part to forward.
  71. /// - promise: The promise the complete when the part has been written.
  72. /// - Important: This *must* to be called from the `eventLoop`.
  73. public func send(
  74. _ part: ServerResponsePart<Response>,
  75. promise: EventLoopPromise<Void>?
  76. ) {
  77. if let outbound = self.nextOutbound {
  78. outbound.invokeSend(part, promise: promise)
  79. } else {
  80. promise?.fail(GRPCError.AlreadyComplete())
  81. }
  82. }
  83. }
  84. extension ServerInterceptorContext {
  85. internal func invokeReceive(_ part: ServerRequestPart<Request>) {
  86. self.eventLoop.assertInEventLoop()
  87. self.interceptor.receive(part, context: self)
  88. }
  89. internal func invokeSend(_ part: ServerResponsePart<Response>, promise: EventLoopPromise<Void>?) {
  90. self.eventLoop.assertInEventLoop()
  91. self.interceptor.send(part, promise: promise, context: self)
  92. }
  93. }