ServerCallContext.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 NIO
  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. }
  37. /// Base class providing data provided to the framework user for all server calls.
  38. open class ServerCallContextBase: ServerCallContext {
  39. /// The event loop this call is served on.
  40. public let eventLoop: EventLoop
  41. /// Request headers for this request.
  42. public let headers: HPACKHeaders
  43. /// The logger used for this call.
  44. public let logger: Logger
  45. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  46. /// this value to take effect compression must have been enabled on the server and a compression
  47. /// algorithm must have been negotiated with the client.
  48. ///
  49. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  50. /// thread-safety.
  51. public var compressionEnabled: Bool {
  52. get {
  53. self.eventLoop.assertInEventLoop()
  54. return self._compressionEnabled
  55. }
  56. set {
  57. self.eventLoop.assertInEventLoop()
  58. self._compressionEnabled = newValue
  59. }
  60. }
  61. private var _compressionEnabled: Bool = true
  62. /// A `UserInfo` dictionary which is shared with the interceptor contexts for this RPC.
  63. ///
  64. /// - Important: While `UserInfo` has value-semantics, this property retrieves from, and sets a
  65. /// reference wrapped `UserInfo`. The contexts passed to interceptors provide the same
  66. /// reference. As such this may be used as a mechanism to pass information between interceptors
  67. /// and service providers.
  68. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  69. /// thread-safety.
  70. public var userInfo: UserInfo {
  71. get {
  72. self.eventLoop.assertInEventLoop()
  73. return self.userInfoRef.value
  74. }
  75. set {
  76. self.eventLoop.assertInEventLoop()
  77. self.userInfoRef.value = newValue
  78. }
  79. }
  80. /// A reference to an underlying `UserInfo`. We share this with the interceptors.
  81. @usableFromInline
  82. internal let userInfoRef: Ref<UserInfo>
  83. /// Metadata to return at the end of the RPC. If this is required it should be updated before
  84. /// the `responsePromise` or `statusPromise` is fulfilled.
  85. ///
  86. /// - Important: This *must* be accessed from the context's `eventLoop` in order to ensure
  87. /// thread-safety.
  88. public var trailers: HPACKHeaders {
  89. get {
  90. self.eventLoop.assertInEventLoop()
  91. return self._trailers
  92. }
  93. set {
  94. self.eventLoop.assertInEventLoop()
  95. self._trailers = newValue
  96. }
  97. }
  98. private var _trailers: HPACKHeaders = [:]
  99. public convenience init(
  100. eventLoop: EventLoop,
  101. headers: HPACKHeaders,
  102. logger: Logger,
  103. userInfo: UserInfo = UserInfo()
  104. ) {
  105. self.init(eventLoop: eventLoop, headers: headers, logger: logger, userInfoRef: .init(userInfo))
  106. }
  107. @inlinable
  108. internal init(
  109. eventLoop: EventLoop,
  110. headers: HPACKHeaders,
  111. logger: Logger,
  112. userInfoRef: Ref<UserInfo>
  113. ) {
  114. self.eventLoop = eventLoop
  115. self.headers = headers
  116. self.userInfoRef = userInfoRef
  117. self.logger = logger
  118. }
  119. }