HTTP2ServerTransport.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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. /// Whether the server allows the client to send keepalive pings when there are no calls in progress.
  48. public var permitWithoutCalls: Bool
  49. /// The minimum allowed interval the client is allowed to send keep-alive pings.
  50. /// Pings more frequent than this interval count as 'strikes' and the connection is closed if there are
  51. /// too many strikes.
  52. public var minPingIntervalWithoutCalls: Duration
  53. /// Creates a new keepalive configuration.
  54. public init(
  55. time: Duration,
  56. timeout: Duration,
  57. permitWithoutCalls: Bool,
  58. minPingIntervalWithoutCalls: Duration
  59. ) {
  60. self.time = time
  61. self.timeout = timeout
  62. self.permitWithoutCalls = permitWithoutCalls
  63. self.minPingIntervalWithoutCalls = minPingIntervalWithoutCalls
  64. }
  65. /// Default values. The time after reading data a ping should be sent defaults to 2 hours, the timeout for
  66. /// keepalive pings defaults to 20 seconds, pings are not permitted when no calls are in progress, and
  67. /// the minimum allowed interval for clients to send pings defaults to 5 minutes.
  68. public static var defaults: Self {
  69. Self(
  70. time: .seconds(2 * 60 * 60), // 2 hours
  71. timeout: .seconds(20),
  72. permitWithoutCalls: false,
  73. minPingIntervalWithoutCalls: .seconds(5 * 60) // 5 minutes
  74. )
  75. }
  76. }
  77. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  78. public struct Connection: Sendable {
  79. /// The maximum amount of time a connection may exist before being gracefully closed.
  80. public var maxAge: Duration?
  81. /// The maximum amount of time that the connection has to close gracefully.
  82. public var maxGraceTime: Duration?
  83. /// The maximum amount of time a connection may be idle before it's closed.
  84. public var maxIdleTime: Duration?
  85. public init(
  86. maxAge: Duration?,
  87. maxGraceTime: Duration?,
  88. maxIdleTime: Duration?
  89. ) {
  90. self.maxAge = maxAge
  91. self.maxGraceTime = maxGraceTime
  92. self.maxIdleTime = maxIdleTime
  93. }
  94. /// Default values. All the max connection age, max grace time, and max idle time default to infinite.
  95. public static var defaults: Self {
  96. Self(maxAge: nil, maxGraceTime: nil, maxIdleTime: nil)
  97. }
  98. }
  99. public struct HTTP2: Sendable {
  100. /// The maximum frame size to be used in an HTTP/2 connection.
  101. public var maxFrameSize: Int
  102. /// The target window size for this connection.
  103. ///
  104. /// - Note: This will also be set as the initial window size for the connection.
  105. public var targetWindowSize: Int
  106. /// The number of concurrent streams on the HTTP/2 connection.
  107. public var maxConcurrentStreams: Int?
  108. public init(
  109. maxFrameSize: Int,
  110. targetWindowSize: Int,
  111. maxConcurrentStreams: Int?
  112. ) {
  113. self.maxFrameSize = maxFrameSize
  114. self.targetWindowSize = targetWindowSize
  115. self.maxConcurrentStreams = maxConcurrentStreams
  116. }
  117. /// Default values. The max frame size defaults to 2^14, the target window size defaults to 2^16-1, and
  118. /// the max concurrent streams default to infinite.
  119. public static var defaults: Self {
  120. Self(
  121. maxFrameSize: 1 << 14,
  122. targetWindowSize: (1 << 16) - 1,
  123. maxConcurrentStreams: nil
  124. )
  125. }
  126. }
  127. public struct RPC: Sendable {
  128. /// The maximum request payload size.
  129. public var maxRequestPayloadSize: Int
  130. public init(maxRequestPayloadSize: Int) {
  131. self.maxRequestPayloadSize = maxRequestPayloadSize
  132. }
  133. /// Default values. Maximum request payload size defaults to 4MiB.
  134. public static var defaults: Self {
  135. Self(maxRequestPayloadSize: 4 * 1024 * 1024)
  136. }
  137. }
  138. }