HTTP2ClientTransport.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 permitWithoutCalls: Bool
  56. /// Creates a new keepalive configuration.
  57. public init(time: Duration, timeout: Duration, permitWithoutCalls: Bool) {
  58. self.time = time
  59. self.timeout = timeout
  60. self.permitWithoutCalls = permitWithoutCalls
  61. }
  62. }
  63. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  64. public struct Idle: Sendable {
  65. /// The maximum amount of time a connection may be idle before it's closed.
  66. public var maxTime: Duration
  67. /// Creates an idle configuration.
  68. public init(maxTime: Duration) {
  69. self.maxTime = maxTime
  70. }
  71. /// Default values, a 30 minute max idle time.
  72. public static var defaults: Self {
  73. Self(maxTime: .seconds(30 * 60))
  74. }
  75. }
  76. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  77. public struct Backoff: Sendable {
  78. /// The initial duration to wait before reattempting to establish a connection.
  79. public var initial: Duration
  80. /// The maximum duration to wait (before jitter is applied) to wait between connect attempts.
  81. public var max: Duration
  82. /// The scaling factor applied to the backoff duration between connect attempts.
  83. public var multiplier: Double
  84. /// An amount to randomize the backoff by.
  85. ///
  86. /// If backoff is computed to be 10 seconds and jitter is set to `0.2`, then the amount of
  87. /// jitter will be selected randomly from the range `-0.2 ✕ 10` seconds to `0.2 ✕ 10` seconds.
  88. /// The resulting backoff will therefore be between 8 seconds and 12 seconds.
  89. public var jitter: Double
  90. /// Creates a new backoff configuration.
  91. public init(initial: Duration, max: Duration, multiplier: Double, jitter: Double) {
  92. self.initial = initial
  93. self.max = max
  94. self.multiplier = multiplier
  95. self.jitter = jitter
  96. }
  97. /// Default values, initial backoff is one second and maximum back off is two minutes. The
  98. /// multiplier is `1.6` and the jitter is set to `0.2`.
  99. public static var defaults: Self {
  100. Self(initial: .seconds(1), max: .seconds(120), multiplier: 1.6, jitter: 0.2)
  101. }
  102. }
  103. public struct HTTP2: Sendable {
  104. /// The max frame size, in bytes.
  105. ///
  106. /// The actual value used is clamped to `(1 << 14) ... (1 << 24) - 1` (the min and max values
  107. /// allowed by RFC 9113 § 6.5.2).
  108. public var maxFrameSize: Int
  109. /// The target flow control window size, in bytes.
  110. ///
  111. /// The value is clamped to `... (1 << 31) - 1`.
  112. public var targetWindowSize: Int
  113. /// Creates a new HTTP/2 configuration.
  114. public init(maxFrameSize: Int, targetWindowSize: Int) {
  115. self.maxFrameSize = maxFrameSize
  116. self.targetWindowSize = targetWindowSize
  117. }
  118. /// Default values, max frame size is 16KiB, and the target window size is 8MiB.
  119. public static var defaults: Self {
  120. Self(maxFrameSize: 1 << 14, targetWindowSize: 8 * 1024 * 1024)
  121. }
  122. }
  123. }