forked from grpc/grpc-swift-nio-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.swift
569 lines (498 loc) · 19.3 KB
/
Connection.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
/*
* Copyright 2024, 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.
*/
package import GRPCCore
package import NIOCore
package import NIOHTTP2
private import Synchronization
/// A `Connection` provides communication to a single remote peer.
///
/// Each `Connection` object is 'one-shot': it may only be used for a single connection over
/// its lifetime. If a connect attempt fails then the `Connection` must be discarded and a new one
/// must be created. However, an active connection may be used multiple times to provide streams
/// to the backend.
///
/// To use the `Connection` you must run it in a task. You can consume event updates by listening
/// to `events`:
///
/// ```swift
/// await withTaskGroup(of: Void.self) { group in
/// group.addTask { await connection.run() }
///
/// for await event in connection.events {
/// switch event {
/// case .connectSucceeded:
/// // ...
/// default:
/// // ...
/// }
/// }
/// }
/// ```
package final class Connection: Sendable {
/// Events which can happen over the lifetime of the connection.
package enum Event: Sendable {
/// The connect attempt succeeded and the connection is ready to use.
case connectSucceeded
/// The connect attempt failed.
case connectFailed(RPCError)
/// The connection received a GOAWAY and will close soon. No new streams
/// should be opened on this connection.
case goingAway(HTTP2ErrorCode, String)
/// The connection is closed.
case closed(Connection.CloseReason)
}
/// The reason the connection closed.
package enum CloseReason: Sendable {
/// Closed because an idle timeout fired.
case idleTimeout
/// Closed because a keepalive timer fired.
case keepaliveTimeout
/// Closed because the caller initiated shutdown and all RPCs on the connection finished.
case initiatedLocally
/// Closed because the remote peer initiate shutdown (i.e. sent a GOAWAY frame).
case remote
/// Closed because the connection encountered an unexpected error.
case error(RPCError, wasIdle: Bool)
}
/// Inputs to the 'run' method.
private enum Input: Sendable {
case close
}
/// Events which have happened to the connection.
private let event: (stream: AsyncStream<Event>, continuation: AsyncStream<Event>.Continuation)
/// Events which the connection must react to.
private let input: (stream: AsyncStream<Input>, continuation: AsyncStream<Input>.Continuation)
/// The address to connect to.
private let address: SocketAddress
/// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint
/// being connected to.
private let authority: String?
/// The name of the server used for the TLS SNI extension, if applicable.
private let sniServerHostname: String?
/// The default compression algorithm used for requests.
private let defaultCompression: CompressionAlgorithm
/// The set of enabled compression algorithms.
private let enabledCompression: CompressionAlgorithmSet
/// A connector used to establish a connection.
private let http2Connector: any HTTP2Connector
/// The state of the connection.
private let state: Mutex<State>
/// The default max request message size in bytes, 4 MiB.
private static var defaultMaxRequestMessageSizeBytes: Int {
4 * 1024 * 1024
}
/// A stream of events which can happen to the connection.
package var events: AsyncStream<Event> {
self.event.stream
}
private static func sanitizeAuthorityForSNI(_ authority: String) -> String {
// Strip off a trailing ":{PORT}". Look for the last non-digit byte, if it's
// a colon then keep everything up to that index.
let index = authority.utf8.lastIndex { byte in
return byte < UInt8(ascii: "0") || byte > UInt8(ascii: "9")
}
if let index = index, authority.utf8[index] == UInt8(ascii: ":") {
return String(authority.utf8[..<index])!
} else {
return authority
}
}
package init(
address: SocketAddress,
authority: String?,
http2Connector: any HTTP2Connector,
defaultCompression: CompressionAlgorithm,
enabledCompression: CompressionAlgorithmSet
) {
self.address = address
self.authority = authority
self.sniServerHostname = authority.map { Self.sanitizeAuthorityForSNI($0) }
self.defaultCompression = defaultCompression
self.enabledCompression = enabledCompression
self.http2Connector = http2Connector
self.event = AsyncStream.makeStream(of: Event.self)
self.input = AsyncStream.makeStream(of: Input.self)
self.state = Mutex(.notConnected)
}
/// Connect and run the connection.
///
/// This function returns when the connection has closed. You can observe connection events
/// by consuming the ``events`` sequence.
package func run() async {
func establishConnectionOrThrow() async throws(RPCError) -> HTTP2Connection {
do {
return try await self.http2Connector.establishConnection(
to: self.address,
// The authority here is used for the SNI hostname in the TLS handshake (if applicable)
// where a raw IP address isn't permitted, so fallback to 'address.sniHostname' rather
// than 'address.authority'.
sniServerHostname: self.sniServerHostname ?? self.address.sniHostname
)
} catch let error as RPCError {
throw error
} catch {
throw RPCError(
code: .unavailable,
message: "Could not establish a connection to \(self.address).",
cause: error
)
}
}
let connectResult = await Result(catching: establishConnectionOrThrow)
switch connectResult {
case .success(let connected):
// Connected successfully, update state and report the event.
self.state.withLock { state in
state.connected(connected)
}
await withDiscardingTaskGroup { group in
// Add a task to run the connection and consume events.
group.addTask {
try? await connected.channel.executeThenClose { inbound, outbound in
await self.consumeConnectionEvents(inbound)
}
}
// Meanwhile, consume input events. This sequence will end when the connection has closed.
for await input in self.input.stream {
switch input {
case .close:
let asyncChannel = self.state.withLock { $0.beginClosing() }
if let channel = asyncChannel?.channel {
let event = ClientConnectionHandler.OutboundEvent.closeGracefully
channel.triggerUserOutboundEvent(event, promise: nil)
}
}
}
}
case .failure(let error):
// Connect failed, this connection is no longer useful.
self.state.withLock { $0.closed() }
self.finishStreams(withEvent: .connectFailed(error))
}
}
/// Gracefully close the connection.
package func close() {
self.input.continuation.yield(.close)
}
/// Make a stream using the connection if it's connected.
///
/// - Parameter descriptor: A descriptor of the method to create a stream for.
/// - Returns: The open stream.
package func makeStream(
descriptor: MethodDescriptor,
options: CallOptions
) async throws -> Stream {
let connected = try self.state.withLock { state in
switch state {
case .connected(let connected):
return connected
case .notConnected, .closing, .closed:
throw RPCError(code: .unavailable, message: "subchannel isn't ready")
}
}
let compression: CompressionAlgorithm
if let override = options.compression {
compression = self.enabledCompression.contains(override) ? override : .none
} else {
compression = self.defaultCompression
}
let maxRequestSize = options.maxRequestMessageBytes ?? Self.defaultMaxRequestMessageSizeBytes
do {
let stream = try await connected.multiplexer.openStream { channel in
channel.eventLoop.makeCompletedFuture {
let streamHandler = GRPCClientStreamHandler(
methodDescriptor: descriptor,
scheme: connected.scheme,
// The value of authority here is being used for the ":authority" pseudo-header. Derive
// one from the address if we don't already have one.
authority: self.authority ?? self.address.authority,
outboundEncoding: compression,
acceptedEncodings: self.enabledCompression,
maxPayloadSize: maxRequestSize
)
try channel.pipeline.syncOperations.addHandler(streamHandler)
return try NIOAsyncChannel(
wrappingChannelSynchronously: channel,
configuration: NIOAsyncChannel.Configuration(
isOutboundHalfClosureEnabled: true,
inboundType: RPCResponsePart<GRPCNIOTransportBytes>.self,
outboundType: RPCRequestPart<GRPCNIOTransportBytes>.self
)
)
}.runInitializerIfSet(connected.onCreateHTTP2Stream, on: channel)
}
let context = ClientContext(
descriptor: descriptor,
remotePeer: connected.remotePeer,
localPeer: connected.localPeer
)
return Stream(wrapping: stream, context: context)
} catch {
throw RPCError(code: .unavailable, message: "subchannel is unavailable", cause: error)
}
}
private func consumeConnectionEvents(
_ connectionEvents: NIOAsyncChannelInboundStream<ClientConnectionEvent>
) async {
// The connection becomes 'ready' when the initial HTTP/2 SETTINGS frame is received.
// Establishing a TCP connection is insufficient as the TLS handshake may not complete or the
// server might not be configured for gRPC or HTTP/2.
//
// This state is tracked here so that if the connection events sequence finishes and the
// connection never became ready then the connection can report that the connect failed.
var isReady = false
var unexpectedCloseError: (any Error)?
func makeNeverReadyError(cause: (any Error)?) -> RPCError {
return RPCError(
code: .unavailable,
message: """
The server accepted the TCP connection but closed the connection before completing \
the HTTP/2 connection preface.
""",
cause: cause
)
}
do {
var channelCloseReason: ClientConnectionEvent.CloseReason?
for try await connectionEvent in connectionEvents {
switch connectionEvent {
case .ready:
isReady = true
self.event.continuation.yield(.connectSucceeded)
case .closing(let reason):
self.state.withLock { $0.closing() }
switch reason {
case .goAway(let errorCode, let reason):
// The connection will close at some point soon, yield a notification for this
// because the close might not be imminent and this could result in address resolution.
self.event.continuation.yield(.goingAway(errorCode, reason))
case .idle, .keepaliveExpired, .initiatedLocally:
// The connection will be closed imminently in these cases there's no need to do
// anything.
()
case .unexpected(let error, _):
// The connection will be closed imminently in this case.
// We'll store the error that caused the unexpected closure so we
// can surface it.
unexpectedCloseError = error
}
// Take the reason with the highest precedence. A GOAWAY may be superseded by user
// closing, for example.
if channelCloseReason.map({ reason.precedence > $0.precedence }) ?? true {
channelCloseReason = reason
}
}
}
let finalEvent: Event
if isReady {
let connectionCloseReason: CloseReason
switch channelCloseReason {
case .keepaliveExpired:
connectionCloseReason = .keepaliveTimeout
case .idle:
// Connection became idle, that's fine.
connectionCloseReason = .idleTimeout
case .goAway:
// Remote peer told us to GOAWAY.
connectionCloseReason = .remote
case .initiatedLocally:
// Shutdown was initiated locally.
connectionCloseReason = .initiatedLocally
case .unexpected(let error, let isIdle):
let error = RPCError(
code: .unavailable,
message: "The TCP connection was dropped unexpectedly.",
cause: error
)
connectionCloseReason = .error(error, wasIdle: isIdle)
case .none:
let error = RPCError(
code: .unavailable,
message: "The TCP connection was dropped unexpectedly.",
cause: nil
)
connectionCloseReason = .error(error, wasIdle: true)
}
finalEvent = .closed(connectionCloseReason)
} else {
// The connection never became ready, this therefore counts as a failed connect attempt.
finalEvent = .connectFailed(makeNeverReadyError(cause: unexpectedCloseError))
}
// The connection events sequence has finished: the connection is now closed.
self.state.withLock { $0.closed() }
self.finishStreams(withEvent: finalEvent)
} catch {
let finalEvent: Event
if isReady {
// Any error must come from consuming the inbound channel meaning that the connection
// must be borked, wrap it up and close.
let rpcError = RPCError(code: .unavailable, message: "connection closed", cause: error)
finalEvent = .closed(.error(rpcError, wasIdle: true))
} else {
// The connection never became ready, this therefore counts as a failed connect attempt.
finalEvent = .connectFailed(makeNeverReadyError(cause: error))
}
self.state.withLock { $0.closed() }
self.finishStreams(withEvent: finalEvent)
}
}
private func finishStreams(withEvent event: Event) {
self.event.continuation.yield(event)
self.event.continuation.finish()
self.input.continuation.finish()
}
}
extension Connection {
package struct Stream {
package typealias Inbound = NIOAsyncChannelInboundStream<RPCResponsePart<GRPCNIOTransportBytes>>
typealias RequestWriter = NIOAsyncChannelOutboundWriter<
RPCRequestPart<GRPCNIOTransportBytes>
>
typealias HTTP2Stream = NIOAsyncChannel<
RPCResponsePart<GRPCNIOTransportBytes>,
RPCRequestPart<GRPCNIOTransportBytes>
>
package struct Outbound: ClosableRPCWriterProtocol {
package typealias Element = RPCRequestPart<GRPCNIOTransportBytes>
private let requestWriter: RequestWriter
private let http2Stream: HTTP2Stream
fileprivate init(
requestWriter: RequestWriter,
http2Stream: HTTP2Stream
) {
self.requestWriter = requestWriter
self.http2Stream = http2Stream
}
package func write(_ element: RPCRequestPart<GRPCNIOTransportBytes>) async throws {
try await self.requestWriter.write(element)
}
package func write(contentsOf elements: some Sequence<Self.Element>) async throws {
try await self.requestWriter.write(contentsOf: elements)
}
package func finish() {
self.requestWriter.finish()
}
package func finish(throwing error: any Error) {
// Fire the error inbound; this fails the inbound writer.
self.http2Stream.channel.pipeline.fireErrorCaught(error)
}
}
let context: ClientContext
private let http2Stream: HTTP2Stream
init(
wrapping stream: HTTP2Stream,
context: ClientContext
) {
self.http2Stream = stream
self.context = context
}
package func execute<T>(
_ closure: (_ inbound: Inbound, _ outbound: Outbound) async throws -> T
) async throws -> T where T: Sendable {
try await self.http2Stream.executeThenClose { inbound, outbound in
return try await closure(
inbound,
Outbound(requestWriter: outbound, http2Stream: self.http2Stream)
)
}
}
}
}
extension Connection {
private enum State: Sendable {
/// The connection is idle or connecting.
case notConnected
/// A TCP connection has been established with the remote peer. However, the connection may not
/// be ready to use yet.
case connected(Connected)
/// The connection has started to close. This may be initiated locally or by the remote.
case closing
/// The connection has closed. This is a terminal state.
case closed
struct Connected: Sendable {
/// The connection channel.
var channel: NIOAsyncChannel<ClientConnectionEvent, Void>
/// The connection's remote peer information.
var remotePeer: String
/// The connection's local peer information.
var localPeer: String
/// Multiplexer for creating HTTP/2 streams.
var multiplexer: NIOHTTP2Handler.AsyncStreamMultiplexer<Void>
/// Whether the connection is plaintext, `false` implies TLS is being used.
var scheme: Scheme
/// A user-provided callback to call after creating the stream.
var onCreateHTTP2Stream: (@Sendable (any Channel) -> EventLoopFuture<Void>)?
init(_ connection: HTTP2Connection) {
self.channel = connection.channel
self.remotePeer = connection.channel.remoteAddressInfo
self.localPeer = connection.channel.localAddressInfo
self.multiplexer = connection.multiplexer
self.scheme = connection.isPlaintext ? .http : .https
self.onCreateHTTP2Stream = connection.onCreateHTTP2Stream
}
}
mutating func connected(_ channel: HTTP2Connection) {
switch self {
case .notConnected:
self = .connected(State.Connected(channel))
case .connected, .closing, .closed:
fatalError("Invalid state: 'run()' must only be called once")
}
}
mutating func beginClosing() -> NIOAsyncChannel<ClientConnectionEvent, Void>? {
switch self {
case .notConnected:
fatalError("Invalid state: 'run()' must be called first")
case .connected(let connected):
self = .closing
return connected.channel
case .closing, .closed:
return nil
}
}
mutating func closing() {
switch self {
case .notConnected:
// Not reachable: happens as a result of a connection event, that can only happen if
// the connection has started (i.e. must be in the 'connected' state or later).
fatalError("Invalid state")
case .connected:
self = .closing
case .closing, .closed:
()
}
}
mutating func closed() {
self = .closed
}
}
}
extension ClientConnectionEvent.CloseReason {
fileprivate var precedence: Int {
switch self {
case .unexpected:
return -1
case .goAway:
return 0
case .idle:
return 1
case .keepaliveExpired:
return 2
case .initiatedLocally:
return 3
}
}
}