@@ -34,15 +34,20 @@ extension HTTPConnectionPool {
34
34
private var lastConnectFailure : Error ?
35
35
36
36
private( set) var requests : RequestQueue
37
- private var state : State = . running
37
+ private( set ) var lifecycleState : StateMachine . LifecycleState
38
38
39
- init ( idGenerator: Connection . ID . Generator , maximumConcurrentConnections: Int ) {
39
+ init (
40
+ idGenerator: Connection . ID . Generator ,
41
+ maximumConcurrentConnections: Int ,
42
+ lifecycleState: StateMachine . LifecycleState
43
+ ) {
40
44
self . connections = HTTP1Connections (
41
45
maximumConcurrentConnections: maximumConcurrentConnections,
42
46
generator: idGenerator
43
47
)
44
48
45
49
self . requests = RequestQueue ( )
50
+ self . lifecycleState = lifecycleState
46
51
}
47
52
48
53
mutating func migrateFromHTTP2(
@@ -111,7 +116,7 @@ extension HTTPConnectionPool {
111
116
// MARK: - Events -
112
117
113
118
mutating func executeRequest( _ request: Request ) -> Action {
114
- switch self . state {
119
+ switch self . lifecycleState {
115
120
case . running:
116
121
if let eventLoop = request. requiredEventLoop {
117
122
return self . executeRequestOnRequiredEventLoop ( request, eventLoop: eventLoop)
@@ -218,7 +223,7 @@ extension HTTPConnectionPool {
218
223
self . failedConsecutiveConnectionAttempts += 1
219
224
self . lastConnectFailure = error
220
225
221
- switch self . state {
226
+ switch self . lifecycleState {
222
227
case . running:
223
228
// We don't care how many waiting requests we have at this point, we will schedule a
224
229
// retry. More tasks, may appear until the backoff has completed. The final
@@ -243,7 +248,7 @@ extension HTTPConnectionPool {
243
248
}
244
249
245
250
mutating func connectionCreationBackoffDone( _ connectionID: Connection . ID ) -> Action {
246
- switch self . state {
251
+ switch self . lifecycleState {
247
252
case . running:
248
253
// The naming of `failConnection` is a little confusing here. All it does is moving the
249
254
// connection state from `.backingOff` to `.closed` here. It also returns the
@@ -271,7 +276,7 @@ extension HTTPConnectionPool {
271
276
return . none
272
277
}
273
278
274
- precondition ( self . state == . running, " If we are shutting down, we must not have any idle connections " )
279
+ precondition ( self . lifecycleState == . running, " If we are shutting down, we must not have any idle connections " )
275
280
276
281
return . init(
277
282
request: . none,
@@ -332,7 +337,7 @@ extension HTTPConnectionPool {
332
337
}
333
338
334
339
mutating func shutdown( ) -> Action {
335
- precondition ( self . state == . running, " Shutdown must only be called once " )
340
+ precondition ( self . lifecycleState == . running, " Shutdown must only be called once " )
336
341
337
342
// If we have remaining request queued, we should fail all of them with a cancelled
338
343
// error.
@@ -350,10 +355,10 @@ extension HTTPConnectionPool {
350
355
let isShutdown : StateMachine . ConnectionAction . IsShutdown
351
356
let unclean = !( cleanupContext. cancel. isEmpty && waitingRequests. isEmpty)
352
357
if self . connections. isEmpty && self . http2Connections == nil {
353
- self . state = . shutDown
358
+ self . lifecycleState = . shutDown
354
359
isShutdown = . yes( unclean: unclean)
355
360
} else {
356
- self . state = . shuttingDown( unclean: unclean)
361
+ self . lifecycleState = . shuttingDown( unclean: unclean)
357
362
isShutdown = . no
358
363
}
359
364
@@ -371,7 +376,7 @@ extension HTTPConnectionPool {
371
376
at index: Int ,
372
377
context: HTTP1Connections . IdleConnectionContext
373
378
) -> EstablishedAction {
374
- switch self . state {
379
+ switch self . lifecycleState {
375
380
case . running:
376
381
switch context. use {
377
382
case . generalPurpose:
@@ -457,7 +462,7 @@ extension HTTPConnectionPool {
457
462
at index: Int ,
458
463
context: HTTP1Connections . FailedConnectionContext
459
464
) -> Action {
460
- switch self . state {
465
+ switch self . lifecycleState {
461
466
case . running:
462
467
switch context. use {
463
468
case . generalPurpose:
@@ -537,7 +542,7 @@ extension HTTPConnectionPool {
537
542
}
538
543
539
544
mutating func http2ConnectionClosed( _ connectionID: Connection . ID ) -> Action {
540
- switch self . state {
545
+ switch self . lifecycleState {
541
546
case . running:
542
547
_ = self . http2Connections? . failConnection ( connectionID)
543
548
if self . http2Connections? . isEmpty == true {
@@ -570,7 +575,7 @@ extension HTTPConnectionPool {
570
575
mutating func http2ConnectionStreamClosed( _ connectionID: Connection . ID ) -> Action {
571
576
// It is save to bang the http2Connections here. If we get this callback but we don't have
572
577
// http2 connections something has gone terribly wrong.
573
- switch self . state {
578
+ switch self . lifecycleState {
574
579
case . running:
575
580
let ( index, context) = self . http2Connections!. releaseStream ( connectionID)
576
581
guard context. isIdle else {
0 commit comments