ConnectionKeepalive.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2020, 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 NIO
  17. /// Provides keepalive pings.
  18. ///
  19. /// The defaults are determined by the gRPC keepalive
  20. /// [documentation] (https://github.com/grpc/grpc/blob/master/doc/keepalive.md).
  21. public struct ClientConnectionKeepalive: Hashable {
  22. /// The amount of time to wait before sending a keepalive ping.
  23. public var interval: TimeAmount
  24. /// The amount of time to wait for an acknowledgment.
  25. /// If it does not receive an acknowledgment within this time, it will close the connection
  26. /// This value must be less than `interval`
  27. public var timeout: TimeAmount
  28. /// Send keepalive pings even if there are no calls in flight.
  29. public var permitWithoutCalls: Bool
  30. /// Maximum number of pings that can be sent when there is no data/header frame to be sent.
  31. public var maximumPingsWithoutData: UInt
  32. /// If there are no data/header frames being received:
  33. /// The minimum amount of time to wait between successive pings.
  34. public var minimumSentPingIntervalWithoutData: TimeAmount
  35. public init(
  36. interval: TimeAmount = .nanoseconds(Int64.max),
  37. timeout: TimeAmount = .seconds(20),
  38. permitWithoutCalls: Bool = false,
  39. maximumPingsWithoutData: UInt = 2,
  40. minimumSentPingIntervalWithoutData: TimeAmount = .minutes(5)
  41. ) {
  42. precondition(timeout < interval, "`timeout` must be less than `interval`")
  43. self.interval = interval
  44. self.timeout = timeout
  45. self.permitWithoutCalls = permitWithoutCalls
  46. self.maximumPingsWithoutData = maximumPingsWithoutData
  47. self.minimumSentPingIntervalWithoutData = minimumSentPingIntervalWithoutData
  48. }
  49. }
  50. public struct ServerConnectionKeepalive: Hashable {
  51. /// The amount of time to wait before sending a keepalive ping.
  52. public var interval: TimeAmount
  53. /// The amount of time to wait for an acknowledgment.
  54. /// If it does not receive an acknowledgment within this time, it will close the connection
  55. /// This value must be less than `interval`
  56. public var timeout: TimeAmount
  57. /// Send keepalive pings even if there are no calls in flight.
  58. public var permitWithoutCalls: Bool
  59. /// Maximum number of pings that can be sent when there is no data/header frame to be sent.
  60. public var maximumPingsWithoutData: UInt
  61. /// If there are no data/header frames being received:
  62. /// The minimum amount of time to wait between successive pings.
  63. public var minimumSentPingIntervalWithoutData: TimeAmount
  64. /// If there are no data/header frames being sent:
  65. /// The minimum amount of time expected between receiving successive pings.
  66. /// If the time between successive pings is less than this value, then the ping will be considered a bad ping from the peer.
  67. /// Such a ping counts as a "ping strike".
  68. public var minimumReceivedPingIntervalWithoutData: TimeAmount
  69. /// Maximum number of bad pings that the server will tolerate before sending an HTTP2 GOAWAY frame and closing the connection.
  70. /// Setting it to `0` allows the server to accept any number of bad pings.
  71. public var maximumPingStrikes: UInt
  72. public init(
  73. interval: TimeAmount = .hours(2),
  74. timeout: TimeAmount = .seconds(20),
  75. permitWithoutCalls: Bool = false,
  76. maximumPingsWithoutData: UInt = 2,
  77. minimumSentPingIntervalWithoutData: TimeAmount = .minutes(5),
  78. minimumReceivedPingIntervalWithoutData: TimeAmount = .minutes(5),
  79. maximumPingStrikes: UInt = 2
  80. ) {
  81. precondition(timeout < interval, "`timeout` must be less than `interval`")
  82. self.interval = interval
  83. self.timeout = timeout
  84. self.permitWithoutCalls = permitWithoutCalls
  85. self.maximumPingsWithoutData = maximumPingsWithoutData
  86. self.minimumSentPingIntervalWithoutData = minimumSentPingIntervalWithoutData
  87. self.minimumReceivedPingIntervalWithoutData = minimumReceivedPingIntervalWithoutData
  88. self.maximumPingStrikes = maximumPingStrikes
  89. }
  90. }