GRPCAsyncServerCallContext.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2021, 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. #if compiler(>=5.6)
  17. import Logging
  18. import NIOConcurrencyHelpers
  19. import NIOHPACK
  20. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  21. public struct GRPCAsyncServerCallContext: Sendable {
  22. @usableFromInline
  23. let contextProvider: AsyncServerCallContextProvider
  24. /// Details of the request, including request headers and a logger.
  25. public var request: Request
  26. /// A response context which may be used to set response headers and trailers.
  27. public var response: Response {
  28. Response(contextProvider: self.contextProvider)
  29. }
  30. /// Access the ``UserInfo`` dictionary which is shared with the interceptor contexts for this RPC.
  31. ///
  32. /// - Important: While ``UserInfo`` has value-semantics, this function accesses a reference
  33. /// wrapped ``UserInfo``. The contexts passed to interceptors provide the same reference. As such
  34. /// this may be used as a mechanism to pass information between interceptors and service
  35. /// providers.
  36. public func withUserInfo<Result: Sendable>(
  37. _ body: @Sendable @escaping (UserInfo) throws -> Result
  38. ) async throws -> Result {
  39. return try await self.contextProvider.withUserInfo(body)
  40. }
  41. /// Modify the ``UserInfo`` dictionary which is shared with the interceptor contexts for this RPC.
  42. ///
  43. /// - Important: While ``UserInfo`` has value-semantics, this function accesses a reference
  44. /// wrapped ``UserInfo``. The contexts passed to interceptors provide the same reference. As such
  45. /// this may be used as a mechanism to pass information between interceptors and service
  46. /// providers.
  47. public func withMutableUserInfo<Result: Sendable>(
  48. _ modify: @Sendable @escaping (inout UserInfo) -> Result
  49. ) async throws -> Result {
  50. return try await self.contextProvider.withMutableUserInfo(modify)
  51. }
  52. @inlinable
  53. internal init(
  54. headers: HPACKHeaders,
  55. logger: Logger,
  56. contextProvider: AsyncServerCallContextProvider
  57. ) {
  58. self.request = Request(headers: headers, logger: logger)
  59. self.contextProvider = contextProvider
  60. }
  61. }
  62. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  63. extension GRPCAsyncServerCallContext {
  64. public struct Request: Sendable {
  65. /// The request headers received from the client at the start of the RPC.
  66. public var headers: HPACKHeaders
  67. /// A logger.
  68. public var logger: Logger
  69. @usableFromInline
  70. init(headers: HPACKHeaders, logger: Logger) {
  71. self.headers = headers
  72. self.logger = logger
  73. }
  74. }
  75. public struct Response: Sendable {
  76. private let contextProvider: AsyncServerCallContextProvider
  77. /// Set the metadata to return at the start of the RPC.
  78. ///
  79. /// - Important: If this is required it should be updated _before_ the first response is sent
  80. /// via the response stream writer. Updates must not be made after the first response has
  81. /// been sent.
  82. public func setHeaders(_ headers: HPACKHeaders) async throws {
  83. try await self.contextProvider.setResponseHeaders(headers)
  84. }
  85. /// Set the metadata to return at the end of the RPC.
  86. ///
  87. /// If this is required it must be updated before returning from the handler.
  88. public func setTrailers(_ trailers: HPACKHeaders) async throws {
  89. try await self.contextProvider.setResponseTrailers(trailers)
  90. }
  91. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  92. /// this value to take effect compression must have been enabled on the server and a compression
  93. /// algorithm must have been negotiated with the client.
  94. public func compressResponses(_ compress: Bool) async throws {
  95. try await self.contextProvider.setResponseCompression(compress)
  96. }
  97. @usableFromInline
  98. internal init(contextProvider: AsyncServerCallContextProvider) {
  99. self.contextProvider = contextProvider
  100. }
  101. }
  102. }
  103. #endif