GRPCKeepaliveHandlers.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 NIOCore
  17. import NIOHTTP2
  18. struct PingHandler {
  19. /// Opaque ping data used for keep-alive pings.
  20. private let pingData: HTTP2PingData
  21. /// Opaque ping data used for a ping sent after a GOAWAY frame.
  22. internal let pingDataGoAway: HTTP2PingData
  23. /// The amount of time to wait before sending a keepalive ping.
  24. private let interval: TimeAmount
  25. /// The amount of time to wait for an acknowledgment.
  26. /// If it does not receive an acknowledgment within this time, it will close the connection
  27. private let timeout: TimeAmount
  28. /// Send keepalive pings even if there are no calls in flight.
  29. private let permitWithoutCalls: Bool
  30. /// Maximum number of pings that can be sent when there is no data/header frame to be sent.
  31. private let maximumPingsWithoutData: UInt
  32. /// If there are no data/header frames being received:
  33. /// The minimum amount of time to wait between successive pings.
  34. private let minimumSentPingIntervalWithoutData: TimeAmount
  35. /// If there are no data/header frames being sent:
  36. /// The minimum amount of time expected between receiving successive pings.
  37. /// If the time between successive pings is less than this value, then the ping will be considered a bad ping from the peer.
  38. /// Such a ping counts as a "ping strike".
  39. /// Ping strikes are only applicable to server handler
  40. private let minimumReceivedPingIntervalWithoutData: TimeAmount?
  41. /// Maximum number of bad pings that the server will tolerate before sending an HTTP2 GOAWAY frame and closing the connection.
  42. /// Setting it to `0` allows the server to accept any number of bad pings.
  43. /// Ping strikes are only applicable to server handler
  44. private let maximumPingStrikes: UInt?
  45. /// When the handler started pinging
  46. private var startedAt: NIODeadline?
  47. /// When the last ping was received
  48. private var lastReceivedPingDate: NIODeadline?
  49. /// When the last ping was sent
  50. private var lastSentPingDate: NIODeadline?
  51. /// The number of pings sent on the transport without any data
  52. private var sentPingsWithoutData = 0
  53. /// Number of strikes
  54. private var pingStrikes: UInt = 0
  55. /// The scheduled task which will close the connection.
  56. private var scheduledClose: Scheduled<Void>?
  57. /// Number of active streams
  58. private var activeStreams = 0 {
  59. didSet {
  60. if self.activeStreams > 0 {
  61. self.sentPingsWithoutData = 0
  62. }
  63. }
  64. }
  65. private static let goAwayFrame = HTTP2Frame.FramePayload.goAway(
  66. lastStreamID: .rootStream,
  67. errorCode: .enhanceYourCalm,
  68. opaqueData: nil
  69. )
  70. // For testing only
  71. var _testingOnlyNow: NIODeadline?
  72. enum Action {
  73. case none
  74. case ack
  75. case schedulePing(delay: TimeAmount, timeout: TimeAmount)
  76. case cancelScheduledTimeout
  77. case reply(HTTP2Frame.FramePayload)
  78. case ratchetDownLastSeenStreamID
  79. }
  80. init(
  81. pingCode: UInt64,
  82. interval: TimeAmount,
  83. timeout: TimeAmount,
  84. permitWithoutCalls: Bool,
  85. maximumPingsWithoutData: UInt,
  86. minimumSentPingIntervalWithoutData: TimeAmount,
  87. minimumReceivedPingIntervalWithoutData: TimeAmount? = nil,
  88. maximumPingStrikes: UInt? = nil
  89. ) {
  90. self.pingData = HTTP2PingData(withInteger: pingCode)
  91. self.pingDataGoAway = HTTP2PingData(withInteger: ~pingCode)
  92. self.interval = interval
  93. self.timeout = timeout
  94. self.permitWithoutCalls = permitWithoutCalls
  95. self.maximumPingsWithoutData = maximumPingsWithoutData
  96. self.minimumSentPingIntervalWithoutData = minimumSentPingIntervalWithoutData
  97. self.minimumReceivedPingIntervalWithoutData = minimumReceivedPingIntervalWithoutData
  98. self.maximumPingStrikes = maximumPingStrikes
  99. }
  100. mutating func streamCreated() -> Action {
  101. self.activeStreams += 1
  102. if self.startedAt == nil {
  103. self.startedAt = self.now()
  104. return .schedulePing(delay: self.interval, timeout: self.timeout)
  105. } else {
  106. return .none
  107. }
  108. }
  109. mutating func streamClosed() -> Action {
  110. self.activeStreams -= 1
  111. return .none
  112. }
  113. mutating func read(pingData: HTTP2PingData, ack: Bool) -> Action {
  114. if ack {
  115. return self.handlePong(pingData)
  116. } else {
  117. return self.handlePing(pingData)
  118. }
  119. }
  120. private func handlePong(_ pingData: HTTP2PingData) -> Action {
  121. if pingData == self.pingData {
  122. return .cancelScheduledTimeout
  123. } else if pingData == self.pingDataGoAway {
  124. // We received a pong for a ping we sent to trail a GOAWAY frame: this means we can now
  125. // send another GOAWAY frame with a (possibly) lower stream ID.
  126. return .ratchetDownLastSeenStreamID
  127. } else {
  128. return .none
  129. }
  130. }
  131. private mutating func handlePing(_ pingData: HTTP2PingData) -> Action {
  132. // Do we support ping strikes (only servers support ping strikes)?
  133. if let maximumPingStrikes = self.maximumPingStrikes {
  134. // Is this a ping strike?
  135. if self.isPingStrike {
  136. self.pingStrikes += 1
  137. // A maximum ping strike of zero indicates that we tolerate any number of strikes.
  138. if maximumPingStrikes != 0, self.pingStrikes > maximumPingStrikes {
  139. return .reply(PingHandler.goAwayFrame)
  140. } else {
  141. return .none
  142. }
  143. } else {
  144. // This is a valid ping, reset our strike count and reply with a pong.
  145. self.pingStrikes = 0
  146. self.lastReceivedPingDate = self.now()
  147. return .ack
  148. }
  149. } else {
  150. // We don't support ping strikes. We'll just reply with a pong.
  151. //
  152. // Note: we don't need to update `pingStrikes` or `lastReceivedPingDate` as we don't
  153. // support ping strikes.
  154. return .ack
  155. }
  156. }
  157. mutating func pingFired() -> Action {
  158. if self.shouldBlockPing {
  159. return .none
  160. } else {
  161. return .reply(self.generatePingFrame(data: self.pingData))
  162. }
  163. }
  164. private mutating func generatePingFrame(
  165. data: HTTP2PingData
  166. ) -> HTTP2Frame.FramePayload {
  167. if self.activeStreams == 0 {
  168. self.sentPingsWithoutData += 1
  169. }
  170. self.lastSentPingDate = self.now()
  171. return HTTP2Frame.FramePayload.ping(data, ack: false)
  172. }
  173. /// Returns true if, on receipt of a ping, the ping should be regarded as a ping strike.
  174. ///
  175. /// A ping is considered a 'strike' if:
  176. /// - There are no active streams.
  177. /// - We allow pings to be sent when there are no active streams (i.e. `self.permitWithoutCalls`).
  178. /// - The time since the last ping we received is less than the minimum allowed interval.
  179. ///
  180. /// - Precondition: Ping strikes are supported (i.e. `self.maximumPingStrikes != nil`)
  181. private var isPingStrike: Bool {
  182. assert(
  183. self.maximumPingStrikes != nil,
  184. "Ping strikes are not supported but we're checking for one"
  185. )
  186. guard self.activeStreams == 0, self.permitWithoutCalls,
  187. let lastReceivedPingDate = self.lastReceivedPingDate,
  188. let minimumReceivedPingIntervalWithoutData = self.minimumReceivedPingIntervalWithoutData
  189. else {
  190. return false
  191. }
  192. return self.now() - lastReceivedPingDate < minimumReceivedPingIntervalWithoutData
  193. }
  194. private var shouldBlockPing: Bool {
  195. // There is no active call on the transport and pings should not be sent
  196. guard self.activeStreams > 0 || self.permitWithoutCalls else {
  197. return true
  198. }
  199. // There is no active call on the transport but pings should be sent
  200. if self.activeStreams == 0, self.permitWithoutCalls {
  201. // The number of pings already sent on the transport without any data has already exceeded the limit
  202. if self.sentPingsWithoutData > self.maximumPingsWithoutData {
  203. return true
  204. }
  205. // The time elapsed since the previous ping is less than the minimum required
  206. if let lastSentPingDate = self.lastSentPingDate,
  207. self.now() - lastSentPingDate < self.minimumSentPingIntervalWithoutData
  208. {
  209. return true
  210. }
  211. return false
  212. }
  213. return false
  214. }
  215. private func now() -> NIODeadline {
  216. return self._testingOnlyNow ?? .now()
  217. }
  218. }