From 6bcc11c0aeffdf6817c3d9eced4a1e198b8976bd Mon Sep 17 00:00:00 2001 From: George Barnett Date: Tue, 3 Dec 2024 11:24:36 +0000 Subject: [PATCH] More docs and comments --- .../Client/Connection/Connection.swift | 9 +++++++-- .../Client/Connection/ConnectionFactory.swift | 5 +++++ .../Client/Connection/GRPCChannel.swift | 7 +++++-- .../Connection/LoadBalancers/PickFirstLoadBalancer.swift | 4 ++-- .../LoadBalancers/RoundRobinLoadBalancer.swift | 4 ++-- .../Client/Connection/LoadBalancers/Subchannel.swift | 4 ++-- 6 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/Connection.swift b/Sources/GRPCNIOTransportCore/Client/Connection/Connection.swift index a2defda..c366194 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/Connection.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/Connection.swift @@ -85,8 +85,8 @@ package final class Connection: Sendable { /// The address to connect to. private let address: SocketAddress - /// The server authority. If `nil`, a value will be computed based on the endpoint being - /// connected to. + /// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint + /// being connected to. private let authority: String? /// The default compression algorithm used for requests. @@ -137,6 +137,9 @@ package final class Connection: Sendable { 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'. authority: self.authority ?? self.address.sniHostname ) } catch let error as RPCError { @@ -223,6 +226,8 @@ package final class Connection: Sendable { let streamHandler = GRPCClientStreamHandler( methodDescriptor: descriptor, scheme: 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, diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/ConnectionFactory.swift b/Sources/GRPCNIOTransportCore/Client/Connection/ConnectionFactory.swift index 9ce584d..d03deb0 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/ConnectionFactory.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/ConnectionFactory.swift @@ -19,6 +19,11 @@ package import NIOHTTP2 internal import NIOPosix package protocol HTTP2Connector: Sendable { + /// Attempt to establish a connection to the given address. + /// + /// - Parameters: + /// - address: The address to connect to. + /// - authority: The authority as used for the TLS SNI extension (if applicable). func establishConnection( to address: SocketAddress, authority: String? diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/GRPCChannel.swift b/Sources/GRPCNIOTransportCore/Client/Connection/GRPCChannel.swift index 53c954d..7618f99 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/GRPCChannel.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/GRPCChannel.swift @@ -52,8 +52,8 @@ package final class GRPCChannel: ClientTransport { /// A factory for connections. private let connector: any HTTP2Connector - /// The server authority. If `nil`, a value will be computed based on the endpoint being - /// connected to. + /// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint + /// being connected to. private let authority: String? /// The connection backoff configuration used by the subchannel when establishing a connection. @@ -86,6 +86,9 @@ package final class GRPCChannel: ClientTransport { self.input = AsyncStream.makeStream() self.connector = connector + // Determine the authority to use for the ":authority" pseudo-header and in the TLS SNI + // extension. This value will be used for all subchannels and connections. If no authority + // is set then one will be derived for each address being connected to. if let authority = config.http2.authority ?? resolver.authority { self.authority = PercentEncoding.encodeAuthority(authority) } else { diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/PickFirstLoadBalancer.swift b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/PickFirstLoadBalancer.swift index 39c958e..c0f1b6f 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/PickFirstLoadBalancer.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/PickFirstLoadBalancer.swift @@ -77,8 +77,8 @@ package final class PickFirstLoadBalancer: Sendable { /// A connector, capable of creating connections. private let connector: any HTTP2Connector - /// The server authority. If `nil`, a value will be computed based on the endpoint being - /// connected to. + /// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint + /// being connected to. private let authority: String? /// Connection backoff configuration. diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/RoundRobinLoadBalancer.swift b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/RoundRobinLoadBalancer.swift index ce3244a..a9511ea 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/RoundRobinLoadBalancer.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/RoundRobinLoadBalancer.swift @@ -109,8 +109,8 @@ package final class RoundRobinLoadBalancer: Sendable { /// A connector, capable of creating connections. private let connector: any HTTP2Connector - /// The server authority. If `nil`, a value will be computed based on the endpoint being - /// connected to. + /// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint + /// being connected to. private let authority: String? /// Connection backoff configuration. diff --git a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/Subchannel.swift b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/Subchannel.swift index 207a9a4..41387f6 100644 --- a/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/Subchannel.swift +++ b/Sources/GRPCNIOTransportCore/Client/Connection/LoadBalancers/Subchannel.swift @@ -83,8 +83,8 @@ package final class Subchannel: Sendable { /// A factory for connections. private let connector: any HTTP2Connector - /// The server authority. If `nil`, a value will be computed based on the endpoint being - /// connected to. + /// The percent-encoded server authority. If `nil`, a value will be computed based on the endpoint + /// being connected to. private let authority: String? /// The connection backoff configuration used by the subchannel when establishing a connection.