@@ -189,6 +189,30 @@ extension HTTPConnectionPool {
189
189
preconditionFailure ( " Unexpected state: Did not expect to have connections with this state in the state machine: \( self . state) " )
190
190
}
191
191
}
192
+
193
+ enum MigrateAction {
194
+ case removeConnection
195
+ case keepConnection
196
+ }
197
+
198
+ func migrateToHTTP2( _ context: inout HTTP1Connections . HTTP2ToHTTP1MigrationContext ) -> MigrateAction {
199
+ switch self . state {
200
+ case . starting:
201
+ context. starting. append ( ( self . connectionID, self . eventLoop) )
202
+ return . removeConnection
203
+ case . backingOff:
204
+ context. backingOff. append ( ( self . connectionID, self . eventLoop) )
205
+ return . removeConnection
206
+ case . idle( let connection, since: _) :
207
+ // Idle connections can be removed right away
208
+ context. close. append ( connection)
209
+ return . removeConnection
210
+ case . leased:
211
+ return . keepConnection
212
+ case . closed:
213
+ preconditionFailure ( " Unexpected state: Did not expect to have connections with this state in the state machine: \( self . state) " )
214
+ }
215
+ }
192
216
}
193
217
194
218
/// A structure to hold the currently active HTTP/1.1 connections.
@@ -298,6 +322,12 @@ extension HTTPConnectionPool {
298
322
var connectionsStartingForUseCase : Int
299
323
}
300
324
325
+ struct HTTP2ToHTTP1MigrationContext {
326
+ var backingOff : [ ( Connection . ID , EventLoop ) ] = [ ]
327
+ var starting : [ ( Connection . ID , EventLoop ) ] = [ ]
328
+ var close : [ Connection ] = [ ]
329
+ }
330
+
301
331
// MARK: Connection creation
302
332
303
333
mutating func createNewConnection( on eventLoop: EventLoop ) -> Connection . ID {
@@ -485,6 +515,21 @@ extension HTTPConnectionPool {
485
515
return ( index, context)
486
516
}
487
517
518
+ // MARK: Migration
519
+
520
+ mutating func migrateToHTTP2( ) -> HTTP2ToHTTP1MigrationContext {
521
+ var migrationContext = HTTP2ToHTTP1MigrationContext ( )
522
+ self . connections. removeAll { connection in
523
+ switch connection. migrateToHTTP2 ( & migrationContext) {
524
+ case . removeConnection:
525
+ return true
526
+ case . keepConnection:
527
+ return false
528
+ }
529
+ }
530
+ return migrationContext
531
+ }
532
+
488
533
// MARK: Shutdown
489
534
490
535
mutating func shutdown( ) -> CleanupContext {
@@ -610,12 +655,12 @@ extension HTTPConnectionPool {
610
655
611
656
return nil
612
657
}
613
- }
614
658
615
- struct Stats {
616
- var idle : Int = 0
617
- var leased : Int = 0
618
- var connecting : Int = 0
619
- var backingOff : Int = 0
659
+ struct Stats {
660
+ var idle : Int = 0
661
+ var leased : Int = 0
662
+ var connecting : Int = 0
663
+ var backingOff : Int = 0
664
+ }
620
665
}
621
666
}
0 commit comments