GRPCAsyncServerCallContext.swift 4.3 KB

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