GRPCAsyncServerCallContext.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// Notifies the client that the RPC has been accepted for processing by the server.
  30. ///
  31. /// On accepting the RPC the server will send the given headers (which may be empty) along with
  32. /// any transport specific headers (such the ":status" pseudo header) to the client.
  33. ///
  34. /// It is not necessary to call this function: the RPC is implicitly accepted when the first
  35. /// response message is sent, however this may be useful when clients require an early indication
  36. /// that the RPC has been accepted.
  37. ///
  38. /// If the RPC has already been accepted (either implicitly or explicitly) then this function is
  39. /// a no-op.
  40. public func acceptRPC(headers: HPACKHeaders) async {
  41. await self.contextProvider.acceptRPC(headers)
  42. }
  43. /// Access the ``UserInfo`` dictionary which is shared with the interceptor contexts for this RPC.
  44. ///
  45. /// - Important: While ``UserInfo`` has value-semantics, this function accesses a reference
  46. /// wrapped ``UserInfo``. The contexts passed to interceptors provide the same reference. As such
  47. /// this may be used as a mechanism to pass information between interceptors and service
  48. /// providers.
  49. public func withUserInfo<Result: Sendable>(
  50. _ body: @Sendable @escaping (UserInfo) throws -> Result
  51. ) async throws -> Result {
  52. return try await self.contextProvider.withUserInfo(body)
  53. }
  54. /// Modify the ``UserInfo`` dictionary which is shared with the interceptor contexts for this RPC.
  55. ///
  56. /// - Important: While ``UserInfo`` has value-semantics, this function accesses a reference
  57. /// wrapped ``UserInfo``. The contexts passed to interceptors provide the same reference. As such
  58. /// this may be used as a mechanism to pass information between interceptors and service
  59. /// providers.
  60. public func withMutableUserInfo<Result: Sendable>(
  61. _ modify: @Sendable @escaping (inout UserInfo) -> Result
  62. ) async throws -> Result {
  63. return try await self.contextProvider.withMutableUserInfo(modify)
  64. }
  65. @inlinable
  66. internal init(
  67. headers: HPACKHeaders,
  68. logger: Logger,
  69. contextProvider: AsyncServerCallContextProvider
  70. ) {
  71. self.request = Request(headers: headers, logger: logger)
  72. self.contextProvider = contextProvider
  73. }
  74. }
  75. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  76. extension GRPCAsyncServerCallContext {
  77. public struct Request: Sendable {
  78. /// The request headers received from the client at the start of the RPC.
  79. public var headers: HPACKHeaders
  80. /// A logger.
  81. public var logger: Logger
  82. @usableFromInline
  83. init(headers: HPACKHeaders, logger: Logger) {
  84. self.headers = headers
  85. self.logger = logger
  86. }
  87. }
  88. public struct Response: Sendable {
  89. private let contextProvider: AsyncServerCallContextProvider
  90. /// Set the metadata to return at the start of the RPC.
  91. ///
  92. /// - Important: If this is required it should be updated _before_ the first response is sent
  93. /// via the response stream writer. Updates must not be made after the RPC has been accepted
  94. /// or the first response has been sent otherwise this method will throw an error.
  95. public func setHeaders(_ headers: HPACKHeaders) async throws {
  96. try await self.contextProvider.setResponseHeaders(headers)
  97. }
  98. /// Set the metadata to return at the end of the RPC.
  99. ///
  100. /// If this is required it must be updated before returning from the handler.
  101. public func setTrailers(_ trailers: HPACKHeaders) async throws {
  102. try await self.contextProvider.setResponseTrailers(trailers)
  103. }
  104. /// Whether compression should be enabled for responses, defaulting to `true`. Note that for
  105. /// this value to take effect compression must have been enabled on the server and a compression
  106. /// algorithm must have been negotiated with the client.
  107. public func compressResponses(_ compress: Bool) async throws {
  108. try await self.contextProvider.setResponseCompression(compress)
  109. }
  110. @usableFromInline
  111. internal init(contextProvider: AsyncServerCallContextProvider) {
  112. self.contextProvider = contextProvider
  113. }
  114. }
  115. }