GRPCKeepaliveHandlers.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 schedulePing(delay: TimeAmount, timeout: TimeAmount)
  75. case cancelScheduledTimeout
  76. case reply(HTTP2Frame.FramePayload)
  77. case ratchetDownLastSeenStreamID
  78. }
  79. init(
  80. pingCode: UInt64,
  81. interval: TimeAmount,
  82. timeout: TimeAmount,
  83. permitWithoutCalls: Bool,
  84. maximumPingsWithoutData: UInt,
  85. minimumSentPingIntervalWithoutData: TimeAmount,
  86. minimumReceivedPingIntervalWithoutData: TimeAmount? = nil,
  87. maximumPingStrikes: UInt? = nil
  88. ) {
  89. self.pingData = HTTP2PingData(withInteger: pingCode)
  90. self.pingDataGoAway = HTTP2PingData(withInteger: ~pingCode)
  91. self.interval = interval
  92. self.timeout = timeout
  93. self.permitWithoutCalls = permitWithoutCalls
  94. self.maximumPingsWithoutData = maximumPingsWithoutData
  95. self.minimumSentPingIntervalWithoutData = minimumSentPingIntervalWithoutData
  96. self.minimumReceivedPingIntervalWithoutData = minimumReceivedPingIntervalWithoutData
  97. self.maximumPingStrikes = maximumPingStrikes
  98. }
  99. mutating func streamCreated() -> Action {
  100. self.activeStreams += 1
  101. if self.startedAt == nil {
  102. self.startedAt = self.now()
  103. return .schedulePing(delay: self.interval, timeout: self.timeout)
  104. } else {
  105. return .none
  106. }
  107. }
  108. mutating func streamClosed() -> Action {
  109. self.activeStreams -= 1
  110. return .none
  111. }
  112. mutating func read(pingData: HTTP2PingData, ack: Bool) -> Action {
  113. if ack {
  114. return self.handlePong(pingData)
  115. } else {
  116. return self.handlePing(pingData)
  117. }
  118. }
  119. private func handlePong(_ pingData: HTTP2PingData) -> Action {
  120. if pingData == self.pingData {
  121. return .cancelScheduledTimeout
  122. } else if pingData == self.pingDataGoAway {
  123. // We received a pong for a ping we sent to trail a GOAWAY frame: this means we can now
  124. // send another GOAWAY frame with a (possibly) lower stream ID.
  125. return .ratchetDownLastSeenStreamID
  126. } else {
  127. return .none
  128. }
  129. }
  130. private mutating func handlePing(_ pingData: HTTP2PingData) -> Action {
  131. // Do we support ping strikes (only servers support ping strikes)?
  132. if let maximumPingStrikes = self.maximumPingStrikes {
  133. // Is this a ping strike?
  134. if self.isPingStrike {
  135. self.pingStrikes += 1
  136. // A maximum ping strike of zero indicates that we tolerate any number of strikes.
  137. if maximumPingStrikes != 0, self.pingStrikes > maximumPingStrikes {
  138. return .reply(PingHandler.goAwayFrame)
  139. } else {
  140. return .none
  141. }
  142. } else {
  143. // This is a valid ping, reset our strike count and reply with a pong.
  144. self.pingStrikes = 0
  145. self.lastReceivedPingDate = self.now()
  146. return .reply(self.generatePingFrame(data: pingData, ack: true))
  147. }
  148. } else {
  149. // We don't support ping strikes. We'll just reply with a pong.
  150. //
  151. // Note: we don't need to update `pingStrikes` or `lastReceivedPingDate` as we don't
  152. // support ping strikes.
  153. return .reply(self.generatePingFrame(data: pingData, ack: true))
  154. }
  155. }
  156. mutating func pingFired() -> Action {
  157. if self.shouldBlockPing {
  158. return .none
  159. } else {
  160. return .reply(self.generatePingFrame(data: self.pingData, ack: false))
  161. }
  162. }
  163. private mutating func generatePingFrame(
  164. data: HTTP2PingData,
  165. ack: Bool
  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: ack)
  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. return true
  209. }
  210. return false
  211. }
  212. return false
  213. }
  214. private func now() -> NIODeadline {
  215. return self._testingOnlyNow ?? .now()
  216. }
  217. }