HTTP2ServerTransport.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2024, 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. public import GRPCCore
  17. internal import NIOHTTP2
  18. /// A namespace for the HTTP/2 server transport.
  19. public enum HTTP2ServerTransport {}
  20. extension HTTP2ServerTransport {
  21. /// A namespace for HTTP/2 server transport configuration.
  22. public enum Config {}
  23. }
  24. extension HTTP2ServerTransport.Config {
  25. public struct Compression: Sendable {
  26. /// Compression algorithms enabled for inbound messages.
  27. ///
  28. /// - Note: ``CompressionAlgorithm/none`` is always supported, even if it isn't set here.
  29. public var enabledAlgorithms: CompressionAlgorithmSet
  30. /// Creates a new compression configuration.
  31. ///
  32. /// - SeeAlso: ``defaults``.
  33. public init(enabledAlgorithms: CompressionAlgorithmSet) {
  34. self.enabledAlgorithms = enabledAlgorithms
  35. }
  36. /// Default values, compression is disabled.
  37. public static var defaults: Self {
  38. Self(enabledAlgorithms: .none)
  39. }
  40. }
  41. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  42. public struct Keepalive: Sendable {
  43. /// The amount of time to wait after reading data before sending a keepalive ping.
  44. public var time: Duration
  45. /// The amount of time the server has to respond to a keepalive ping before the connection is closed.
  46. public var timeout: Duration
  47. /// Configuration for how the server enforces client keepalive.
  48. public var clientBehavior: ClientKeepaliveBehavior
  49. /// Creates a new keepalive configuration.
  50. public init(
  51. time: Duration,
  52. timeout: Duration,
  53. clientBehavior: ClientKeepaliveBehavior
  54. ) {
  55. self.time = time
  56. self.timeout = timeout
  57. self.clientBehavior = clientBehavior
  58. }
  59. /// Default values. The time after reading data a ping should be sent defaults to 2 hours, the timeout for
  60. /// keepalive pings defaults to 20 seconds, pings are not permitted when no calls are in progress, and
  61. /// the minimum allowed interval for clients to send pings defaults to 5 minutes.
  62. public static var defaults: Self {
  63. Self(
  64. time: .seconds(2 * 60 * 60), // 2 hours
  65. timeout: .seconds(20),
  66. clientBehavior: .defaults
  67. )
  68. }
  69. }
  70. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  71. public struct ClientKeepaliveBehavior: Sendable {
  72. /// The minimum allowed interval the client is allowed to send keep-alive pings.
  73. /// Pings more frequent than this interval count as 'strikes' and the connection is closed if there are
  74. /// too many strikes.
  75. public var minPingIntervalWithoutCalls: Duration
  76. /// Whether the server allows the client to send keepalive pings when there are no calls in progress.
  77. public var allowWithoutCalls: Bool
  78. /// Creates a new configuration for permitted client keepalive behavior.
  79. public init(
  80. minPingIntervalWithoutCalls: Duration,
  81. allowWithoutCalls: Bool
  82. ) {
  83. self.minPingIntervalWithoutCalls = minPingIntervalWithoutCalls
  84. self.allowWithoutCalls = allowWithoutCalls
  85. }
  86. /// Default values. The time after reading data a ping should be sent defaults to 2 hours, the timeout for
  87. /// keepalive pings defaults to 20 seconds, pings are not permitted when no calls are in progress, and
  88. /// the minimum allowed interval for clients to send pings defaults to 5 minutes.
  89. public static var defaults: Self {
  90. Self(minPingIntervalWithoutCalls: .seconds(5 * 60), allowWithoutCalls: false)
  91. }
  92. }
  93. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  94. public struct Connection: Sendable {
  95. /// The maximum amount of time a connection may exist before being gracefully closed.
  96. public var maxAge: Duration?
  97. /// The maximum amount of time that the connection has to close gracefully.
  98. public var maxGraceTime: Duration?
  99. /// The maximum amount of time a connection may be idle before it's closed.
  100. public var maxIdleTime: Duration?
  101. /// Configuration for keepalive used to detect broken connections.
  102. ///
  103. /// - SeeAlso: gRFC A8 for client side keepalive, and gRFC A9 for server connection management.
  104. public var keepalive: Keepalive
  105. public init(
  106. maxAge: Duration?,
  107. maxGraceTime: Duration?,
  108. maxIdleTime: Duration?,
  109. keepalive: Keepalive
  110. ) {
  111. self.maxAge = maxAge
  112. self.maxGraceTime = maxGraceTime
  113. self.maxIdleTime = maxIdleTime
  114. self.keepalive = keepalive
  115. }
  116. /// Default values. The max connection age, max grace time, and max idle time default to
  117. /// `nil` (i.e. infinite). See ``HTTP2ServerTransport/Config/Keepalive/defaults`` for keepalive
  118. /// defaults.
  119. public static var defaults: Self {
  120. Self(maxAge: nil, maxGraceTime: nil, maxIdleTime: nil, keepalive: .defaults)
  121. }
  122. }
  123. public struct HTTP2: Sendable {
  124. /// The maximum frame size to be used in an HTTP/2 connection.
  125. public var maxFrameSize: Int
  126. /// The target window size for this connection.
  127. ///
  128. /// - Note: This will also be set as the initial window size for the connection.
  129. public var targetWindowSize: Int
  130. /// The number of concurrent streams on the HTTP/2 connection.
  131. public var maxConcurrentStreams: Int?
  132. public init(
  133. maxFrameSize: Int,
  134. targetWindowSize: Int,
  135. maxConcurrentStreams: Int?
  136. ) {
  137. self.maxFrameSize = maxFrameSize
  138. self.targetWindowSize = targetWindowSize
  139. self.maxConcurrentStreams = maxConcurrentStreams
  140. }
  141. /// Default values. The max frame size defaults to 2^14, the target window size defaults to 2^16-1, and
  142. /// the max concurrent streams default to infinite.
  143. public static var defaults: Self {
  144. Self(
  145. maxFrameSize: 1 << 14,
  146. targetWindowSize: (1 << 16) - 1,
  147. maxConcurrentStreams: nil
  148. )
  149. }
  150. }
  151. public struct RPC: Sendable {
  152. /// The maximum request payload size.
  153. public var maxRequestPayloadSize: Int
  154. public init(maxRequestPayloadSize: Int) {
  155. self.maxRequestPayloadSize = maxRequestPayloadSize
  156. }
  157. /// Default values. Maximum request payload size defaults to 4MiB.
  158. public static var defaults: Self {
  159. Self(maxRequestPayloadSize: 4 * 1024 * 1024)
  160. }
  161. }
  162. }