@@ -234,7 +234,9 @@ struct PoolStateMachine<
234234
235235 @inlinable
236236 mutating func releaseConnection( _ connection: Connection , streams: UInt16 ) -> Action {
237- let ( index, context) = self . connections. releaseConnection ( connection. id, streams: streams)
237+ guard let ( index, context) = self . connections. releaseConnection ( connection. id, streams: streams) else {
238+ return . none( )
239+ }
238240 return self . handleAvailableConnection ( index: index, availableContext: context)
239241 }
240242
@@ -251,8 +253,13 @@ struct PoolStateMachine<
251253
252254 @inlinable
253255 mutating func connectionEstablished( _ connection: Connection , maxStreams: UInt16 ) -> Action {
254- let ( index, context) = self . connections. newConnectionEstablished ( connection, maxStreams: maxStreams)
255- return self . handleAvailableConnection ( index: index, availableContext: context)
256+ switch self . poolState {
257+ case . running, . shuttingDown( graceful: true ) :
258+ let ( index, context) = self . connections. newConnectionEstablished ( connection, maxStreams: maxStreams)
259+ return self . handleAvailableConnection ( index: index, availableContext: context)
260+ case . shuttingDown( graceful: false ) , . shutDown:
261+ return . init( request: . none, connection: . closeConnection( connection, [ ] ) )
262+ }
256263 }
257264
258265 @inlinable
@@ -274,31 +281,43 @@ struct PoolStateMachine<
274281
275282 @inlinable
276283 mutating func connectionEstablishFailed( _ error: Error , for request: ConnectionRequest ) -> Action {
277- self . failedConsecutiveConnectionAttempts += 1
284+ switch self . poolState {
285+ case . running, . shuttingDown( graceful: true ) :
286+ self . failedConsecutiveConnectionAttempts += 1
278287
279- let connectionTimer = self . connections. backoffNextConnectionAttempt ( request. connectionID)
280- let backoff = Self . calculateBackoff ( failedAttempt: self . failedConsecutiveConnectionAttempts)
281- let timer = Timer ( connectionTimer, duration: backoff)
282- return . init( request: . none, connection: . scheduleTimers( . init( timer) ) )
288+ let connectionTimer = self . connections. backoffNextConnectionAttempt ( request. connectionID)
289+ let backoff = Self . calculateBackoff ( failedAttempt: self . failedConsecutiveConnectionAttempts)
290+ let timer = Timer ( connectionTimer, duration: backoff)
291+ return . init( request: . none, connection: . scheduleTimers( . init( timer) ) )
292+
293+ case . shuttingDown( graceful: false ) , . shutDown:
294+ return . none( )
295+ }
283296 }
284297
285298 @inlinable
286299 mutating func connectionCreationBackoffDone( _ connectionID: ConnectionID ) -> Action {
287- let soonAvailable = self . connections. soonAvailableConnections
288- let retry = ( soonAvailable - 1 ) < self . requestQueue. count
289-
290- switch self . connections. backoffDone ( connectionID, retry: retry) {
291- case . createConnection( let request, let continuation) :
292- let timers : TinyFastSequence < TimerCancellationToken >
293- if let continuation {
294- timers = . init( element: continuation)
295- } else {
296- timers = . init( )
300+ switch self . poolState {
301+ case . running, . shuttingDown( graceful: true ) :
302+ let soonAvailable = self . connections. soonAvailableConnections
303+ let retry = ( soonAvailable - 1 ) < self . requestQueue. count
304+
305+ switch self . connections. backoffDone ( connectionID, retry: retry) {
306+ case . createConnection( let request, let continuation) :
307+ let timers : TinyFastSequence < TimerCancellationToken >
308+ if let continuation {
309+ timers = . init( element: continuation)
310+ } else {
311+ timers = . init( )
312+ }
313+ return . init( request: . none, connection: . makeConnection( request, timers) )
314+
315+ case . cancelTimers( let timers) :
316+ return . init( request: . none, connection: . cancelTimers( . init( timers) ) )
297317 }
298- return . init( request: . none, connection: . makeConnection( request, timers) )
299318
300- case . cancelTimers ( let timers ) :
301- return . init ( request : . none, connection : . cancelTimers ( . init ( timers ) ) )
319+ case . shuttingDown ( graceful : false ) , . shutDown :
320+ return . none( )
302321 }
303322 }
304323
0 commit comments