forked from grpc/grpc-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionManager.swift
1076 lines (912 loc) · 36.8 KB
/
ConnectionManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOHTTP2
#if compiler(>=5.6)
// Unchecked because mutable state is always accessed and modified on a particular event loop.
// APIs which _may_ be called from different threads execute onto the correct event loop first.
// APIs which _must_ be called from an exact event loop have preconditions checking that the correct
// event loop is being used.
extension ConnectionManager: @unchecked Sendable {}
#endif // compiler(>=5.6)
@usableFromInline
internal final class ConnectionManager {
internal enum Reconnect {
case none
case after(TimeInterval)
}
internal struct ConnectingState {
var backoffIterator: ConnectionBackoffIterator?
var reconnect: Reconnect
var candidate: EventLoopFuture<Channel>
var readyChannelMuxPromise: EventLoopPromise<HTTP2StreamMultiplexer>
var candidateMuxPromise: EventLoopPromise<HTTP2StreamMultiplexer>
}
internal struct ConnectedState {
var backoffIterator: ConnectionBackoffIterator?
var reconnect: Reconnect
var candidate: Channel
var readyChannelMuxPromise: EventLoopPromise<HTTP2StreamMultiplexer>
var multiplexer: HTTP2StreamMultiplexer
var error: Error?
init(from state: ConnectingState, candidate: Channel, multiplexer: HTTP2StreamMultiplexer) {
self.backoffIterator = state.backoffIterator
self.reconnect = state.reconnect
self.candidate = candidate
self.readyChannelMuxPromise = state.readyChannelMuxPromise
self.multiplexer = multiplexer
}
}
internal struct ReadyState {
var channel: Channel
var multiplexer: HTTP2StreamMultiplexer
var error: Error?
init(from state: ConnectedState) {
self.channel = state.candidate
self.multiplexer = state.multiplexer
}
}
internal struct TransientFailureState {
var backoffIterator: ConnectionBackoffIterator?
var readyChannelMuxPromise: EventLoopPromise<HTTP2StreamMultiplexer>
var scheduled: Scheduled<Void>
var reason: Error
init(from state: ConnectingState, scheduled: Scheduled<Void>, reason: Error) {
self.backoffIterator = state.backoffIterator
self.readyChannelMuxPromise = state.readyChannelMuxPromise
self.scheduled = scheduled
self.reason = reason
}
init(from state: ConnectedState, scheduled: Scheduled<Void>) {
self.backoffIterator = state.backoffIterator
self.readyChannelMuxPromise = state.readyChannelMuxPromise
self.scheduled = scheduled
self.reason = state.error ?? GRPCStatus(
code: .unavailable,
message: "Unexpected connection drop"
)
}
init(
from state: ReadyState,
scheduled: Scheduled<Void>,
backoffIterator: ConnectionBackoffIterator?
) {
self.backoffIterator = backoffIterator
self.readyChannelMuxPromise = state.channel.eventLoop.makePromise()
self.scheduled = scheduled
self.reason = state.error ?? GRPCStatus(
code: .unavailable,
message: "Unexpected connection drop"
)
}
}
internal struct ShutdownState {
var closeFuture: EventLoopFuture<Void>
/// The reason we are shutdown. Any requests for a `Channel` in this state will be failed with
/// this error.
var reason: Error
init(closeFuture: EventLoopFuture<Void>, reason: Error) {
self.closeFuture = closeFuture
self.reason = reason
}
static func shutdownByUser(closeFuture: EventLoopFuture<Void>) -> ShutdownState {
return ShutdownState(
closeFuture: closeFuture,
reason: GRPCStatus(code: .unavailable, message: "Connection was shutdown by the user")
)
}
}
internal enum State {
/// No `Channel` is required.
///
/// Valid next states:
/// - `connecting`
/// - `shutdown`
case idle(lastError: Error?)
/// We're actively trying to establish a connection.
///
/// Valid next states:
/// - `active`
/// - `transientFailure` (if our attempt fails and we're going to try again)
/// - `shutdown`
case connecting(ConnectingState)
/// We've established a `Channel`, it might not be suitable (TLS handshake may fail, etc.).
/// Our signal to be 'ready' is the initial HTTP/2 SETTINGS frame.
///
/// Valid next states:
/// - `ready`
/// - `transientFailure` (if we our handshake fails or other error happens and we can attempt
/// to re-establish the connection)
/// - `shutdown`
case active(ConnectedState)
/// We have an active `Channel` which has seen the initial HTTP/2 SETTINGS frame. We can use
/// the channel for making RPCs.
///
/// Valid next states:
/// - `idle` (we're not serving any RPCs, we can drop the connection for now)
/// - `transientFailure` (we encountered an error and will re-establish the connection)
/// - `shutdown`
case ready(ReadyState)
/// A `Channel` is desired, we'll attempt to create one in the future.
///
/// Valid next states:
/// - `connecting`
/// - `shutdown`
case transientFailure(TransientFailureState)
/// We never want another `Channel`: this state is terminal.
case shutdown(ShutdownState)
fileprivate var label: String {
switch self {
case .idle:
return "idle"
case .connecting:
return "connecting"
case .active:
return "active"
case .ready:
return "ready"
case .transientFailure:
return "transientFailure"
case .shutdown:
return "shutdown"
}
}
}
/// The last 'external' state we are in, a subset of the internal state.
private var externalState: _ConnectivityState = .idle(nil)
/// Update the external state, potentially notifying a delegate about the change.
private func updateExternalState(to nextState: _ConnectivityState) {
if !self.externalState.isSameState(as: nextState) {
let oldState = self.externalState
self.externalState = nextState
self.connectivityDelegate?.connectionStateDidChange(self, from: oldState, to: nextState)
}
}
/// Our current state.
private var state: State {
didSet {
switch self.state {
case let .idle(error):
self.updateExternalState(to: .idle(error))
self.updateConnectionID()
case .connecting:
self.updateExternalState(to: .connecting)
// This is an internal state.
case .active:
()
case .ready:
self.updateExternalState(to: .ready)
case let .transientFailure(state):
self.updateExternalState(to: .transientFailure(state.reason))
self.updateConnectionID()
case .shutdown:
self.updateExternalState(to: .shutdown)
}
}
}
/// Returns whether the state is 'idle'.
private var isIdle: Bool {
self.eventLoop.assertInEventLoop()
switch self.state {
case .idle:
return true
case .connecting, .transientFailure, .active, .ready, .shutdown:
return false
}
}
/// Returns whether the state is 'shutdown'.
private var isShutdown: Bool {
self.eventLoop.assertInEventLoop()
switch self.state {
case .shutdown:
return true
case .idle, .connecting, .transientFailure, .active, .ready:
return false
}
}
/// Returns the `HTTP2StreamMultiplexer` from the 'ready' state or `nil` if it is not available.
private var multiplexer: HTTP2StreamMultiplexer? {
self.eventLoop.assertInEventLoop()
switch self.state {
case let .ready(state):
return state.multiplexer
case .idle, .connecting, .transientFailure, .active, .shutdown:
return nil
}
}
/// The `EventLoop` that the managed connection will run on.
internal let eventLoop: EventLoop
/// A delegate for connectivity changes. Executed on the `EventLoop`.
private var connectivityDelegate: ConnectionManagerConnectivityDelegate?
/// A delegate for HTTP/2 connection changes. Executed on the `EventLoop`.
private var http2Delegate: ConnectionManagerHTTP2Delegate?
/// An `EventLoopFuture<Channel>` provider.
private let channelProvider: ConnectionManagerChannelProvider
/// The behavior for starting a call, i.e. how patient is the caller when asking for a
/// multiplexer.
private let callStartBehavior: CallStartBehavior.Behavior
/// The configuration to use when backing off between connection attempts, if reconnection
/// attempts should be made at all.
private let connectionBackoff: ConnectionBackoff?
/// A logger.
internal var logger: Logger
private let connectionID: String
private var channelNumber: UInt64
private var channelNumberLock = NIOLock()
private var _connectionIDAndNumber: String {
return "\(self.connectionID)/\(self.channelNumber)"
}
private var connectionIDAndNumber: String {
return self.channelNumberLock.withLock {
return self._connectionIDAndNumber
}
}
private func updateConnectionID() {
self.channelNumberLock.withLock {
self.channelNumber &+= 1
self.logger[metadataKey: MetadataKey.connectionID] = "\(self._connectionIDAndNumber)"
}
}
internal func appendMetadata(to logger: inout Logger) {
logger[metadataKey: MetadataKey.connectionID] = "\(self.connectionIDAndNumber)"
}
internal convenience init(
configuration: ClientConnection.Configuration,
channelProvider: ConnectionManagerChannelProvider? = nil,
connectivityDelegate: ConnectionManagerConnectivityDelegate?,
logger: Logger
) {
self.init(
eventLoop: configuration.eventLoopGroup.next(),
channelProvider: channelProvider ?? DefaultChannelProvider(configuration: configuration),
callStartBehavior: configuration.callStartBehavior.wrapped,
connectionBackoff: configuration.connectionBackoff,
connectivityDelegate: connectivityDelegate,
http2Delegate: nil,
logger: logger
)
}
internal init(
eventLoop: EventLoop,
channelProvider: ConnectionManagerChannelProvider,
callStartBehavior: CallStartBehavior.Behavior,
connectionBackoff: ConnectionBackoff?,
connectivityDelegate: ConnectionManagerConnectivityDelegate?,
http2Delegate: ConnectionManagerHTTP2Delegate?,
logger: Logger
) {
// Setup the logger.
var logger = logger
let connectionID = UUID().uuidString
let channelNumber: UInt64 = 0
logger[metadataKey: MetadataKey.connectionID] = "\(connectionID)/\(channelNumber)"
self.eventLoop = eventLoop
self.state = .idle(lastError: nil)
self.channelProvider = channelProvider
self.callStartBehavior = callStartBehavior
self.connectionBackoff = connectionBackoff
self.connectivityDelegate = connectivityDelegate
self.http2Delegate = http2Delegate
self.connectionID = connectionID
self.channelNumber = channelNumber
self.logger = logger
}
/// Get the multiplexer from the underlying channel handling gRPC calls.
/// if the `ConnectionManager` was configured to be `fastFailure` this will have
/// one chance to connect - if not reconnections are managed here.
internal func getHTTP2Multiplexer() -> EventLoopFuture<HTTP2StreamMultiplexer> {
func getHTTP2Multiplexer0() -> EventLoopFuture<HTTP2StreamMultiplexer> {
switch self.callStartBehavior {
case .waitsForConnectivity:
return self.getHTTP2MultiplexerPatient()
case .fastFailure:
return self.getHTTP2MultiplexerOptimistic()
}
}
if self.eventLoop.inEventLoop {
return getHTTP2Multiplexer0()
} else {
return self.eventLoop.flatSubmit {
getHTTP2Multiplexer0()
}
}
}
/// Returns a future for the multiplexer which succeeded when the channel is connected.
/// Reconnects are handled if necessary.
private func getHTTP2MultiplexerPatient() -> EventLoopFuture<HTTP2StreamMultiplexer> {
let multiplexer: EventLoopFuture<HTTP2StreamMultiplexer>
switch self.state {
case .idle:
self.startConnecting()
// We started connecting so we must transition to the `connecting` state.
guard case let .connecting(connecting) = self.state else {
self.invalidState()
}
multiplexer = connecting.readyChannelMuxPromise.futureResult
case let .connecting(state):
multiplexer = state.readyChannelMuxPromise.futureResult
case let .active(state):
multiplexer = state.readyChannelMuxPromise.futureResult
case let .ready(state):
multiplexer = self.eventLoop.makeSucceededFuture(state.multiplexer)
case let .transientFailure(state):
multiplexer = state.readyChannelMuxPromise.futureResult
case let .shutdown(state):
multiplexer = self.eventLoop.makeFailedFuture(state.reason)
}
self.logger.debug("vending multiplexer future", metadata: [
"connectivity_state": "\(self.state.label)",
])
return multiplexer
}
/// Returns a future for the current HTTP/2 stream multiplexer, or future HTTP/2 stream multiplexer from the current connection
/// attempt, or if the state is 'idle' returns the future for the next connection attempt.
///
/// Note: if the state is 'transientFailure' or 'shutdown' then a failed future will be returned.
private func getHTTP2MultiplexerOptimistic() -> EventLoopFuture<HTTP2StreamMultiplexer> {
// `getHTTP2Multiplexer` makes sure we're on the event loop but let's just be sure.
self.eventLoop.preconditionInEventLoop()
let muxFuture: EventLoopFuture<HTTP2StreamMultiplexer> = { () in
switch self.state {
case .idle:
self.startConnecting()
// We started connecting so we must transition to the `connecting` state.
guard case let .connecting(connecting) = self.state else {
self.invalidState()
}
return connecting.candidateMuxPromise.futureResult
case let .connecting(state):
return state.candidateMuxPromise.futureResult
case let .active(active):
return self.eventLoop.makeSucceededFuture(active.multiplexer)
case let .ready(ready):
return self.eventLoop.makeSucceededFuture(ready.multiplexer)
case let .transientFailure(state):
return self.eventLoop.makeFailedFuture(state.reason)
case let .shutdown(state):
return self.eventLoop.makeFailedFuture(state.reason)
}
}()
self.logger.debug("vending fast-failing multiplexer future", metadata: [
"connectivity_state": "\(self.state.label)",
])
return muxFuture
}
@usableFromInline
internal enum ShutdownMode {
/// Closes the underlying channel without waiting for existing RPCs to complete.
case forceful
/// Allows running RPCs to run their course before closing the underlying channel. No new
/// streams may be created.
case graceful(NIODeadline)
}
/// Shutdown the underlying connection.
///
/// - Note: Initiating a `forceful` shutdown after a `graceful` shutdown has no effect.
internal func shutdown(mode: ShutdownMode) -> EventLoopFuture<Void> {
let promise = self.eventLoop.makePromise(of: Void.self)
self.shutdown(mode: mode, promise: promise)
return promise.futureResult
}
/// Shutdown the underlying connection.
///
/// - Note: Initiating a `forceful` shutdown after a `graceful` shutdown has no effect.
internal func shutdown(mode: ShutdownMode, promise: EventLoopPromise<Void>) {
if self.eventLoop.inEventLoop {
self._shutdown(mode: mode, promise: promise)
} else {
self.eventLoop.execute {
self._shutdown(mode: mode, promise: promise)
}
}
}
private func _shutdown(mode: ShutdownMode, promise: EventLoopPromise<Void>) {
self.logger.debug("shutting down connection", metadata: [
"connectivity_state": "\(self.state.label)",
"shutdown.mode": "\(mode)",
])
switch self.state {
// We don't have a channel and we don't want one, easy!
case .idle:
let shutdown: ShutdownState = .shutdownByUser(closeFuture: promise.futureResult)
self.state = .shutdown(shutdown)
promise.succeed(())
// We're mid-connection: the application doesn't have any 'ready' channels so we'll succeed
// the shutdown future and deal with any fallout from the connecting channel without the
// application knowing.
case let .connecting(state):
let shutdown: ShutdownState = .shutdownByUser(closeFuture: promise.futureResult)
self.state = .shutdown(shutdown)
// Fail the ready channel mux promise: we're shutting down so even if we manage to successfully
// connect the application shouldn't have access to the channel or multiplexer.
state.readyChannelMuxPromise.fail(GRPCStatus(code: .unavailable, message: nil))
state.candidateMuxPromise.fail(GRPCStatus(code: .unavailable, message: nil))
// Complete the shutdown promise when the connection attempt has completed.
state.candidate.whenComplete {
switch $0 {
case let .success(channel):
// In case we do successfully connect, close on the next loop tick. When connecting a
// channel NIO will complete the promise for the channel before firing channel active.
// That means we may close and fire inactive before active which HTTP/2 will be unhappy
// about.
self.eventLoop.execute {
channel.close(mode: .all, promise: nil)
promise.completeWith(channel.closeFuture.recoveringFromUncleanShutdown())
}
case .failure:
// We failed to connect, that's fine we still shutdown successfully.
promise.succeed(())
}
}
// We have an active channel but the application doesn't know about it yet. We'll do the same
// as for `.connecting`.
case let .active(state):
let shutdown: ShutdownState = .shutdownByUser(closeFuture: promise.futureResult)
self.state = .shutdown(shutdown)
// Fail the ready channel mux promise: we're shutting down so even if we manage to successfully
// connect the application shouldn't have access to the channel or multiplexer.
state.readyChannelMuxPromise.fail(GRPCStatus(code: .unavailable, message: nil))
// We have a channel, close it. We only create streams in the ready state so there's no need
// to quiesce here.
state.candidate.close(mode: .all, promise: nil)
promise.completeWith(state.candidate.closeFuture.recoveringFromUncleanShutdown())
// The channel is up and running: the application could be using it. We can close it and
// return the `closeFuture`.
case let .ready(state):
let shutdown: ShutdownState = .shutdownByUser(closeFuture: promise.futureResult)
self.state = .shutdown(shutdown)
switch mode {
case .forceful:
// We have a channel, close it.
state.channel.close(mode: .all, promise: nil)
case let .graceful(deadline):
// If we don't close by the deadline forcibly close the channel.
let scheduledForceClose = state.channel.eventLoop.scheduleTask(deadline: deadline) {
self.logger.info("shutdown timer expired, forcibly closing connection")
state.channel.close(mode: .all, promise: nil)
}
// Cancel the force close if we close normally first.
state.channel.closeFuture.whenComplete { _ in
scheduledForceClose.cancel()
}
// Tell the channel to quiesce. It will be picked up by the idle handler which will close
// the channel when all streams have been closed.
state.channel.pipeline.fireUserInboundEventTriggered(ChannelShouldQuiesceEvent())
}
// Complete the promise when we eventually close.
promise.completeWith(state.channel.closeFuture.recoveringFromUncleanShutdown())
// Like `.connecting` and `.active` the application does not have a `.ready` channel. We'll
// do the same but also cancel any scheduled connection attempts and deal with any fallout
// if we cancelled too late.
case let .transientFailure(state):
let shutdown: ShutdownState = .shutdownByUser(closeFuture: promise.futureResult)
self.state = .shutdown(shutdown)
// Stop the creation of a new channel, if we can. If we can't then the task to
// `startConnecting()` will see our new `shutdown` state and ignore the request to connect.
state.scheduled.cancel()
// Fail the ready channel mux promise: we're shutting down so even if we manage to successfully
// connect the application shouldn't should have access to the channel.
state.readyChannelMuxPromise.fail(shutdown.reason)
// No active channel, so complete the shutdown promise now.
promise.succeed(())
// We're already shutdown; there's nothing to do.
case let .shutdown(state):
promise.completeWith(state.closeFuture)
}
}
/// Registers a callback which fires when the current active connection is closed.
///
/// If there is a connection, the callback will be invoked with `true` when the connection is
/// closed. Otherwise the callback is invoked with `false`.
internal func onCurrentConnectionClose(_ onClose: @escaping (Bool) -> Void) {
if self.eventLoop.inEventLoop {
self._onCurrentConnectionClose(onClose)
} else {
self.eventLoop.execute {
self._onCurrentConnectionClose(onClose)
}
}
}
private func _onCurrentConnectionClose(_ onClose: @escaping (Bool) -> Void) {
self.eventLoop.assertInEventLoop()
switch self.state {
case let .ready(state):
state.channel.closeFuture.whenComplete { _ in onClose(true) }
case .idle, .connecting, .active, .transientFailure, .shutdown:
onClose(false)
}
}
// MARK: - State changes from the channel handler.
/// The channel caught an error. Hold on to it until the channel becomes inactive, it may provide
/// some context.
internal func channelError(_ error: Error) {
self.eventLoop.preconditionInEventLoop()
switch self.state {
// Hitting an error in idle is a surprise, but not really something we do anything about. Either the
// error is channel fatal, in which case we'll see channelInactive soon (acceptable), or it's not,
// and future I/O will either fail fast or work. In either case, all we do is log this and move on.
case .idle:
self.logger.warning("ignoring unexpected error in idle", metadata: [
MetadataKey.error: "\(error)",
])
case .connecting:
self.connectionFailed(withError: error)
case var .active(state):
state.error = error
self.state = .active(state)
case var .ready(state):
state.error = error
self.state = .ready(state)
// If we've already in one of these states, then additional errors aren't helpful to us.
case .transientFailure, .shutdown:
()
}
}
/// The connecting channel became `active`. Must be called on the `EventLoop`.
internal func channelActive(channel: Channel, multiplexer: HTTP2StreamMultiplexer) {
self.eventLoop.preconditionInEventLoop()
self.logger.debug("activating connection", metadata: [
"connectivity_state": "\(self.state.label)",
])
switch self.state {
case let .connecting(connecting):
let connected = ConnectedState(from: connecting, candidate: channel, multiplexer: multiplexer)
self.state = .active(connected)
// Optimistic connections are happy this this level of setup.
connecting.candidateMuxPromise.succeed(multiplexer)
// Application called shutdown before the channel become active; we should close it.
case .shutdown:
channel.close(mode: .all, promise: nil)
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .idle:
self.invalidState()
case .active:
self.invalidState()
case .ready:
self.invalidState()
case .transientFailure:
self.invalidState()
}
}
/// An established channel (i.e. `active` or `ready`) has become inactive: should we reconnect?
/// Must be called on the `EventLoop`.
internal func channelInactive() {
self.eventLoop.preconditionInEventLoop()
self.logger.debug("deactivating connection", metadata: [
"connectivity_state": "\(self.state.label)",
])
switch self.state {
// The channel is `active` but not `ready`. Should we try again?
case let .active(active):
switch active.reconnect {
// No, shutdown instead.
case .none:
self.logger.debug("shutting down connection")
let error = GRPCStatus(
code: .unavailable,
message: "The connection was dropped and connection re-establishment is disabled"
)
let shutdownState = ShutdownState(
closeFuture: self.eventLoop.makeSucceededFuture(()),
reason: error
)
self.state = .shutdown(shutdownState)
active.readyChannelMuxPromise.fail(error)
// Yes, after some time.
case let .after(delay):
let scheduled = self.eventLoop.scheduleTask(in: .seconds(timeInterval: delay)) {
self.startConnecting()
}
self.logger.debug("scheduling connection attempt", metadata: ["delay_secs": "\(delay)"])
self.state = .transientFailure(TransientFailureState(from: active, scheduled: scheduled))
}
// The channel was ready and working fine but something went wrong. Should we try to replace
// the channel?
case let .ready(ready):
// No, no backoff is configured.
if self.connectionBackoff == nil {
self.logger.debug("shutting down connection, no reconnect configured/remaining")
self.state = .shutdown(
ShutdownState(
closeFuture: ready.channel.closeFuture,
reason: GRPCStatus(
code: .unavailable,
message: "The connection was dropped and a reconnect was not configured"
)
)
)
} else {
// Yes, start connecting now. We should go via `transientFailure`, however.
let scheduled = self.eventLoop.scheduleTask(in: .nanoseconds(0)) {
self.startConnecting()
}
self.logger.debug("scheduling connection attempt", metadata: ["delay": "0"])
let backoffIterator = self.connectionBackoff?.makeIterator()
self.state = .transientFailure(TransientFailureState(
from: ready,
scheduled: scheduled,
backoffIterator: backoffIterator
))
}
// This is fine: we expect the channel to become inactive after becoming idle.
case .idle:
()
// We're already shutdown, that's fine.
case .shutdown:
()
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .connecting:
self.invalidState()
case .transientFailure:
self.invalidState()
}
}
/// The channel has become ready, that is, it has seen the initial HTTP/2 SETTINGS frame. Must be
/// called on the `EventLoop`.
internal func ready() {
self.eventLoop.preconditionInEventLoop()
self.logger.debug("connection ready", metadata: [
"connectivity_state": "\(self.state.label)",
])
switch self.state {
case let .active(connected):
self.state = .ready(ReadyState(from: connected))
connected.readyChannelMuxPromise.succeed(connected.multiplexer)
case .shutdown:
()
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .idle:
self.invalidState()
case .transientFailure:
self.invalidState()
case .connecting:
self.invalidState()
case .ready:
self.invalidState()
}
}
/// No active RPCs are happening on 'ready' channel: close the channel for now. Must be called on
/// the `EventLoop`.
internal func idle() {
self.eventLoop.preconditionInEventLoop()
self.logger.debug("idling connection", metadata: [
"connectivity_state": "\(self.state.label)",
])
switch self.state {
case let .active(state):
// This state is reachable if the keepalive timer fires before we reach the ready state.
self.state = .idle(lastError: state.error)
state.readyChannelMuxPromise
.fail(GRPCStatus(code: .unavailable, message: "Idled before reaching ready state"))
case let .ready(state):
self.state = .idle(lastError: state.error)
case .shutdown:
// This is expected when the connection is closed by the user: when the channel becomes
// inactive and there are no outstanding RPCs, 'idle()' will be called instead of
// 'channelInactive()'.
()
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .idle:
self.invalidState()
case .connecting:
self.invalidState()
case .transientFailure:
self.invalidState()
}
}
internal func streamOpened() {
self.eventLoop.assertInEventLoop()
self.http2Delegate?.streamOpened(self)
}
internal func streamClosed() {
self.eventLoop.assertInEventLoop()
self.http2Delegate?.streamClosed(self)
}
internal func maxConcurrentStreamsChanged(_ maxConcurrentStreams: Int) {
self.eventLoop.assertInEventLoop()
self.http2Delegate?.receivedSettingsMaxConcurrentStreams(
self, maxConcurrentStreams: maxConcurrentStreams
)
}
/// The connection has started quiescing: notify the connectivity monitor of this.
internal func beginQuiescing() {
self.eventLoop.assertInEventLoop()
self.connectivityDelegate?.connectionIsQuiescing(self)
}
}
extension ConnectionManager {
// A connection attempt failed; we never established a connection.
private func connectionFailed(withError error: Error) {
self.eventLoop.preconditionInEventLoop()
switch self.state {
case let .connecting(connecting):
// Should we reconnect?
switch connecting.reconnect {
// No, shutdown.
case .none:
self.logger.debug("shutting down connection, no reconnect configured/remaining")
self.state = .shutdown(
ShutdownState(closeFuture: self.eventLoop.makeSucceededFuture(()), reason: error)
)
connecting.readyChannelMuxPromise.fail(error)
connecting.candidateMuxPromise.fail(error)
// Yes, after a delay.
case let .after(delay):
self.logger.debug("scheduling connection attempt", metadata: ["delay": "\(delay)"])
let scheduled = self.eventLoop.scheduleTask(in: .seconds(timeInterval: delay)) {
self.startConnecting()
}
self.state = .transientFailure(
TransientFailureState(from: connecting, scheduled: scheduled, reason: error)
)
// Candidate mux users are not willing to wait.
connecting.candidateMuxPromise.fail(error)
}
// The application must have called shutdown while we were trying to establish a connection
// which was doomed to fail anyway. That's fine, we can ignore this.
case .shutdown:
()
// We can't fail to connect if we aren't trying.
//
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .idle:
self.invalidState()
case .active:
self.invalidState()
case .ready:
self.invalidState()
case .transientFailure:
self.invalidState()
}
}
}
extension ConnectionManager {
// Start establishing a connection: we can only do this from the `idle` and `transientFailure`
// states. Must be called on the `EventLoop`.
private func startConnecting() {
self.eventLoop.assertInEventLoop()
switch self.state {
case .idle:
let iterator = self.connectionBackoff?.makeIterator()
self.startConnecting(
backoffIterator: iterator,
muxPromise: self.eventLoop.makePromise()
)
case let .transientFailure(pending):
self.startConnecting(
backoffIterator: pending.backoffIterator,
muxPromise: pending.readyChannelMuxPromise
)
// We shutdown before a scheduled connection attempt had started.
case .shutdown:
()
// These cases are purposefully separated: some crash reporting services provide stack traces
// which don't include the precondition failure message (which contain the invalid state we were
// in). Keeping the cases separate allows us work out the state from the line number.
case .connecting:
self.invalidState()
case .active:
self.invalidState()
case .ready:
self.invalidState()
}
}
private func startConnecting(
backoffIterator: ConnectionBackoffIterator?,
muxPromise: EventLoopPromise<HTTP2StreamMultiplexer>
) {
let timeoutAndBackoff = backoffIterator?.next()
// We're already on the event loop: submit the connect so it starts after we've made the
// state change to `.connecting`.
self.eventLoop.assertInEventLoop()
let candidate: EventLoopFuture<Channel> = self.eventLoop.flatSubmit {
let channel: EventLoopFuture<Channel> = self.channelProvider.makeChannel(
managedBy: self,
onEventLoop: self.eventLoop,
connectTimeout: timeoutAndBackoff.map { .seconds(timeInterval: $0.timeout) },
logger: self.logger
)
channel.whenFailure { error in
self.connectionFailed(withError: error)
}
return channel
}
// Should we reconnect if the candidate channel fails?
let reconnect: Reconnect = timeoutAndBackoff.map { .after($0.backoff) } ?? .none
let connecting = ConnectingState(
backoffIterator: backoffIterator,
reconnect: reconnect,
candidate: candidate,
readyChannelMuxPromise: muxPromise,
candidateMuxPromise: self.eventLoop.makePromise()