HTTP2ClientTransport.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. import GRPCCore
  17. /// A namespace for the HTTP/2 client transport.
  18. public enum HTTP2ClientTransport {}
  19. extension HTTP2ClientTransport {
  20. /// A namespace for HTTP/2 client transport configuration.
  21. public enum Config {}
  22. }
  23. extension HTTP2ClientTransport.Config {
  24. public struct Compression: Sendable {
  25. /// The default algorithm used for compressing outbound messages.
  26. ///
  27. /// This can be overridden on a per-call basis via ``CallOptions``.
  28. public var algorithm: CompressionAlgorithm
  29. /// Compression algorithms enabled for inbound messages.
  30. ///
  31. /// - Note: ``CompressionAlgorithm/none`` is always supported, even if it isn't set here.
  32. public var enabledAlgorithms: CompressionAlgorithmSet
  33. /// Creates a new compression configuration.
  34. ///
  35. /// - SeeAlso: ``defaults``.
  36. public init(algorithm: CompressionAlgorithm, enabledAlgorithms: CompressionAlgorithmSet) {
  37. self.algorithm = algorithm
  38. self.enabledAlgorithms = enabledAlgorithms
  39. }
  40. /// Default values, compression is disabled.
  41. public static var defaults: Self {
  42. Self(algorithm: .none, enabledAlgorithms: .none)
  43. }
  44. }
  45. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  46. public struct Keepalive: Sendable {
  47. /// The amount of time to wait after reading data before sending a keepalive ping.
  48. ///
  49. /// - Note: The transport may choose to increase this value if it is less than 10 seconds.
  50. public var time: Duration
  51. /// The amount of time the server has to respond to a keepalive ping before the connection
  52. /// is closed.
  53. public var timeout: Duration
  54. /// Whether the client sends keepalive pings when there are no calls in progress.
  55. public var allowWithoutCalls: Bool
  56. /// Creates a new keepalive configuration.
  57. public init(time: Duration, timeout: Duration, allowWithoutCalls: Bool) {
  58. self.time = time
  59. self.timeout = timeout
  60. self.allowWithoutCalls = allowWithoutCalls
  61. }
  62. }
  63. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  64. public struct Connection: Sendable {
  65. /// The maximum amount of time a connection may be idle before it's closed.
  66. ///
  67. /// Connections are considered idle when there are no open streams on them. Idle connections
  68. /// can be closed after a configured amount of time to free resources. Note that servers may
  69. /// separately monitor and close idle connections.
  70. public var maxIdleTime: Duration?
  71. /// Configuration for keepalive.
  72. ///
  73. /// Keepalive is typically applied to connection which have open streams. It can be useful to
  74. /// detect dropped connections, particularly if the streams running on a connection don't have
  75. /// much activity.
  76. ///
  77. /// See also: gRFC A8: Client-side Keepalive.
  78. public var keepalive: Keepalive?
  79. /// Creates a connection configuration.
  80. public init(maxIdleTime: Duration, keepalive: Keepalive?) {
  81. self.maxIdleTime = maxIdleTime
  82. self.keepalive = keepalive
  83. }
  84. /// Default values, a 30 minute max idle time and no keepalive.
  85. public static var defaults: Self {
  86. Self(maxIdleTime: .seconds(30 * 60), keepalive: nil)
  87. }
  88. }
  89. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  90. public struct Backoff: Sendable {
  91. /// The initial duration to wait before reattempting to establish a connection.
  92. public var initial: Duration
  93. /// The maximum duration to wait (before jitter is applied) to wait between connect attempts.
  94. public var max: Duration
  95. /// The scaling factor applied to the backoff duration between connect attempts.
  96. public var multiplier: Double
  97. /// An amount to randomize the backoff by.
  98. ///
  99. /// If backoff is computed to be 10 seconds and jitter is set to `0.2`, then the amount of
  100. /// jitter will be selected randomly from the range `-0.2 ✕ 10` seconds to `0.2 ✕ 10` seconds.
  101. /// The resulting backoff will therefore be between 8 seconds and 12 seconds.
  102. public var jitter: Double
  103. /// Creates a new backoff configuration.
  104. public init(initial: Duration, max: Duration, multiplier: Double, jitter: Double) {
  105. self.initial = initial
  106. self.max = max
  107. self.multiplier = multiplier
  108. self.jitter = jitter
  109. }
  110. /// Default values, initial backoff is one second and maximum back off is two minutes. The
  111. /// multiplier is `1.6` and the jitter is set to `0.2`.
  112. public static var defaults: Self {
  113. Self(initial: .seconds(1), max: .seconds(120), multiplier: 1.6, jitter: 0.2)
  114. }
  115. }
  116. public struct HTTP2: Sendable {
  117. /// The max frame size, in bytes.
  118. ///
  119. /// The actual value used is clamped to `(1 << 14) ... (1 << 24) - 1` (the min and max values
  120. /// allowed by RFC 9113 § 6.5.2).
  121. public var maxFrameSize: Int
  122. /// The target flow control window size, in bytes.
  123. ///
  124. /// The value is clamped to `... (1 << 31) - 1`.
  125. public var targetWindowSize: Int
  126. /// Creates a new HTTP/2 configuration.
  127. public init(maxFrameSize: Int, targetWindowSize: Int) {
  128. self.maxFrameSize = maxFrameSize
  129. self.targetWindowSize = targetWindowSize
  130. }
  131. /// Default values, max frame size is 16KiB, and the target window size is 8MiB.
  132. public static var defaults: Self {
  133. Self(maxFrameSize: 1 << 14, targetWindowSize: 8 * 1024 * 1024)
  134. }
  135. }
  136. }