@@ -16,12 +16,6 @@ import NIOCore
16
16
17
17
extension HTTPConnectionPool {
18
18
struct HTTP1StateMachine {
19
- enum State : Equatable {
20
- case running
21
- case shuttingDown( unclean: Bool )
22
- case shutDown
23
- }
24
-
25
19
typealias Action = HTTPConnectionPool . StateMachine . Action
26
20
typealias ConnectionMigrationAction = HTTPConnectionPool . StateMachine . ConnectionMigrationAction
27
21
typealias EstablishedAction = HTTPConnectionPool . StateMachine . EstablishedAction
@@ -34,15 +28,20 @@ extension HTTPConnectionPool {
34
28
private var lastConnectFailure : Error ?
35
29
36
30
private( set) var requests : RequestQueue
37
- private var state : State = . running
31
+ private( set ) var lifecycleState : StateMachine . LifecycleState
38
32
39
- init ( idGenerator: Connection . ID . Generator , maximumConcurrentConnections: Int ) {
33
+ init (
34
+ idGenerator: Connection . ID . Generator ,
35
+ maximumConcurrentConnections: Int ,
36
+ lifecycleState: StateMachine . LifecycleState
37
+ ) {
40
38
self . connections = HTTP1Connections (
41
39
maximumConcurrentConnections: maximumConcurrentConnections,
42
40
generator: idGenerator
43
41
)
44
42
45
43
self . requests = RequestQueue ( )
44
+ self . lifecycleState = lifecycleState
46
45
}
47
46
48
47
mutating func migrateFromHTTP2(
@@ -111,7 +110,7 @@ extension HTTPConnectionPool {
111
110
// MARK: - Events -
112
111
113
112
mutating func executeRequest( _ request: Request ) -> Action {
114
- switch self . state {
113
+ switch self . lifecycleState {
115
114
case . running:
116
115
if let eventLoop = request. requiredEventLoop {
117
116
return self . executeRequestOnRequiredEventLoop ( request, eventLoop: eventLoop)
@@ -218,7 +217,7 @@ extension HTTPConnectionPool {
218
217
self . failedConsecutiveConnectionAttempts += 1
219
218
self . lastConnectFailure = error
220
219
221
- switch self . state {
220
+ switch self . lifecycleState {
222
221
case . running:
223
222
// We don't care how many waiting requests we have at this point, we will schedule a
224
223
// retry. More tasks, may appear until the backoff has completed. The final
@@ -243,7 +242,7 @@ extension HTTPConnectionPool {
243
242
}
244
243
245
244
mutating func connectionCreationBackoffDone( _ connectionID: Connection . ID ) -> Action {
246
- switch self . state {
245
+ switch self . lifecycleState {
247
246
case . running:
248
247
// The naming of `failConnection` is a little confusing here. All it does is moving the
249
248
// connection state from `.backingOff` to `.closed` here. It also returns the
@@ -271,7 +270,7 @@ extension HTTPConnectionPool {
271
270
return . none
272
271
}
273
272
274
- precondition ( self . state == . running, " If we are shutting down, we must not have any idle connections " )
273
+ precondition ( self . lifecycleState == . running, " If we are shutting down, we must not have any idle connections " )
275
274
276
275
return . init(
277
276
request: . none,
@@ -332,7 +331,7 @@ extension HTTPConnectionPool {
332
331
}
333
332
334
333
mutating func shutdown( ) -> Action {
335
- precondition ( self . state == . running, " Shutdown must only be called once " )
334
+ precondition ( self . lifecycleState == . running, " Shutdown must only be called once " )
336
335
337
336
// If we have remaining request queued, we should fail all of them with a cancelled
338
337
// error.
@@ -350,10 +349,10 @@ extension HTTPConnectionPool {
350
349
let isShutdown : StateMachine . ConnectionAction . IsShutdown
351
350
let unclean = !( cleanupContext. cancel. isEmpty && waitingRequests. isEmpty)
352
351
if self . connections. isEmpty && self . http2Connections == nil {
353
- self . state = . shutDown
352
+ self . lifecycleState = . shutDown
354
353
isShutdown = . yes( unclean: unclean)
355
354
} else {
356
- self . state = . shuttingDown( unclean: unclean)
355
+ self . lifecycleState = . shuttingDown( unclean: unclean)
357
356
isShutdown = . no
358
357
}
359
358
@@ -371,7 +370,7 @@ extension HTTPConnectionPool {
371
370
at index: Int ,
372
371
context: HTTP1Connections . IdleConnectionContext
373
372
) -> EstablishedAction {
374
- switch self . state {
373
+ switch self . lifecycleState {
375
374
case . running:
376
375
switch context. use {
377
376
case . generalPurpose:
@@ -457,7 +456,7 @@ extension HTTPConnectionPool {
457
456
at index: Int ,
458
457
context: HTTP1Connections . FailedConnectionContext
459
458
) -> Action {
460
- switch self . state {
459
+ switch self . lifecycleState {
461
460
case . running:
462
461
switch context. use {
463
462
case . generalPurpose:
@@ -537,7 +536,7 @@ extension HTTPConnectionPool {
537
536
}
538
537
539
538
mutating func http2ConnectionClosed( _ connectionID: Connection . ID ) -> Action {
540
- switch self . state {
539
+ switch self . lifecycleState {
541
540
case . running:
542
541
_ = self . http2Connections? . failConnection ( connectionID)
543
542
if self . http2Connections? . isEmpty == true {
@@ -570,7 +569,7 @@ extension HTTPConnectionPool {
570
569
mutating func http2ConnectionStreamClosed( _ connectionID: Connection . ID ) -> Action {
571
570
// It is save to bang the http2Connections here. If we get this callback but we don't have
572
571
// http2 connections something has gone terribly wrong.
573
- switch self . state {
572
+ switch self . lifecycleState {
574
573
case . running:
575
574
let ( index, context) = self . http2Connections!. releaseStream ( connectionID)
576
575
guard context. isIdle else {
0 commit comments