ServerCallContext.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. import Logging
  18. import NIOCore
  19. import NIOHPACK
  20. import NIOHTTP1
  21. import SwiftProtobuf
  22. /// Protocol declaring a minimum set of properties exposed by *all* types of call contexts.
  23. public protocol ServerCallContext: AnyObject {
  24. /// The event loop this call is served on.
  25. var eventLoop: EventLoop { get }
  26. /// Request headers for this request.
  27. var headers: HPACKHeaders { get }
  28. /// A 'UserInfo' dictionary which is shared with the interceptor contexts for this RPC.
  29. var userInfo: UserInfo { get set }
  30. /// The logger used for this call.
  31. var logger: Logger { get }
  32. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  33. /// this value to take effect compression must have been enabled on the server and a compression
  34. /// algorithm must have been negotiated with the client.
  35. var compressionEnabled: Bool { get set }
  36. /// A future which completes when the call closes. This may be used to register callbacks which
  37. /// free up resources used by the RPC.
  38. var closeFuture: EventLoopFuture<Void> { get }
  39. }
  40. extension ServerCallContext {
  41. // Default implementation to avoid breaking API.
  42. public var closeFuture: EventLoopFuture<Void> {
  43. return self.eventLoop.makeFailedFuture(GRPCStatus.closeFutureNotImplemented)
  44. }
  45. }
  46. extension GRPCStatus {
  47. internal static let closeFutureNotImplemented = GRPCStatus(
  48. code: .unimplemented,
  49. message: "This context type has not implemented support for a 'closeFuture'"
  50. )
  51. }
  52. /// Base class providing data provided to the framework user for all server calls.
  53. open class ServerCallContextBase: ServerCallContext {
  54. /// The event loop this call is served on.
  55. public let eventLoop: EventLoop
  56. /// Request headers for this request.
  57. public let headers: HPACKHeaders
  58. /// The logger used for this call.
  59. public let logger: Logger
  60. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  61. /// this value to take effect compression must have been enabled on the server and a compression
  62. /// algorithm must have been negotiated with the client.
  63. ///
  64. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  65. /// thread-safety.
  66. public var compressionEnabled: Bool {
  67. get {
  68. self.eventLoop.assertInEventLoop()
  69. return self._compressionEnabled
  70. }
  71. set {
  72. self.eventLoop.assertInEventLoop()
  73. self._compressionEnabled = newValue
  74. }
  75. }
  76. private var _compressionEnabled: Bool = true
  77. /// A `UserInfo` dictionary which is shared with the interceptor contexts for this RPC.
  78. ///
  79. /// - Important: While ``UserInfo`` has value-semantics, this property retrieves from, and sets a
  80. /// reference wrapped ``UserInfo``. The contexts passed to interceptors provide the same
  81. /// reference. As such this may be used as a mechanism to pass information between interceptors
  82. /// and service providers.
  83. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  84. /// thread-safety.
  85. public var userInfo: UserInfo {
  86. get {
  87. self.eventLoop.assertInEventLoop()
  88. return self.userInfoRef.value
  89. }
  90. set {
  91. self.eventLoop.assertInEventLoop()
  92. self.userInfoRef.value = newValue
  93. }
  94. }
  95. /// A reference to an underlying ``UserInfo``. We share this with the interceptors.
  96. @usableFromInline
  97. internal let userInfoRef: Ref<UserInfo>
  98. /// Metadata to return at the end of the RPC. If this is required it should be updated before
  99. /// the `responsePromise` or `statusPromise` is fulfilled.
  100. ///
  101. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  102. /// thread-safety.
  103. public var trailers: HPACKHeaders {
  104. get {
  105. self.eventLoop.assertInEventLoop()
  106. return self._trailers
  107. }
  108. set {
  109. self.eventLoop.assertInEventLoop()
  110. self._trailers = newValue
  111. }
  112. }
  113. private var _trailers: HPACKHeaders = [:]
  114. /// A future which completes when the call closes. This may be used to register callbacks which
  115. /// free up resources used by the RPC.
  116. public let closeFuture: EventLoopFuture<Void>
  117. @available(*, deprecated, renamed: "init(eventLoop:headers:logger:userInfo:closeFuture:)")
  118. public convenience init(
  119. eventLoop: EventLoop,
  120. headers: HPACKHeaders,
  121. logger: Logger,
  122. userInfo: UserInfo = UserInfo()
  123. ) {
  124. self.init(
  125. eventLoop: eventLoop,
  126. headers: headers,
  127. logger: logger,
  128. userInfoRef: .init(userInfo),
  129. closeFuture: eventLoop.makeFailedFuture(GRPCStatus.closeFutureNotImplemented)
  130. )
  131. }
  132. public convenience init(
  133. eventLoop: EventLoop,
  134. headers: HPACKHeaders,
  135. logger: Logger,
  136. userInfo: UserInfo = UserInfo(),
  137. closeFuture: EventLoopFuture<Void>
  138. ) {
  139. self.init(
  140. eventLoop: eventLoop,
  141. headers: headers,
  142. logger: logger,
  143. userInfoRef: .init(userInfo),
  144. closeFuture: closeFuture
  145. )
  146. }
  147. @inlinable
  148. internal init(
  149. eventLoop: EventLoop,
  150. headers: HPACKHeaders,
  151. logger: Logger,
  152. userInfoRef: Ref<UserInfo>,
  153. closeFuture: EventLoopFuture<Void>
  154. ) {
  155. self.eventLoop = eventLoop
  156. self.headers = headers
  157. self.userInfoRef = userInfoRef
  158. self.logger = logger
  159. self.closeFuture = closeFuture
  160. }
  161. }