Skip to content

Commit b036fb7

Browse files
authored
Add NIOTransportServices H2 client transport (grpc#2054)
Add a `NIOTransportServices` H2 client transport to gRPC v2.
1 parent a98b0f3 commit b036fb7

11 files changed

+466
-55
lines changed

Sources/GRPCHTTP2Core/Client/HTTP2ClientTransport.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension HTTP2ClientTransport {
2525
}
2626

2727
extension HTTP2ClientTransport.Config {
28-
public struct Compression: Sendable {
28+
public struct Compression: Sendable, Hashable {
2929
/// The default algorithm used for compressing outbound messages.
3030
///
3131
/// This can be overridden on a per-call basis via `CallOptions`.
@@ -51,7 +51,7 @@ extension HTTP2ClientTransport.Config {
5151
}
5252

5353
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
54-
public struct Keepalive: Sendable {
54+
public struct Keepalive: Sendable, Hashable {
5555
/// The amount of time to wait after reading data before sending a keepalive ping.
5656
///
5757
/// - Note: The transport may choose to increase this value if it is less than 10 seconds.
@@ -73,7 +73,7 @@ extension HTTP2ClientTransport.Config {
7373
}
7474

7575
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
76-
public struct Connection: Sendable {
76+
public struct Connection: Sendable, Hashable {
7777
/// The maximum amount of time a connection may be idle before it's closed.
7878
///
7979
/// Connections are considered idle when there are no open streams on them. Idle connections
@@ -103,7 +103,7 @@ extension HTTP2ClientTransport.Config {
103103
}
104104

105105
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
106-
public struct Backoff: Sendable {
106+
public struct Backoff: Sendable, Hashable {
107107
/// The initial duration to wait before reattempting to establish a connection.
108108
public var initial: Duration
109109

@@ -135,7 +135,7 @@ extension HTTP2ClientTransport.Config {
135135
}
136136
}
137137

138-
public struct HTTP2: Sendable {
138+
public struct HTTP2: Sendable, Hashable {
139139
/// The max frame size, in bytes.
140140
///
141141
/// The actual value used is clamped to `(1 << 14) ... (1 << 24) - 1` (the min and max values

Sources/GRPCHTTP2Core/Internal/NIOSocketAddress+GRPCSocketAddress.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
private import GRPCCore
1718
package import NIOCore
1819

1920
extension GRPCHTTP2Core.SocketAddress {
@@ -38,6 +39,22 @@ extension GRPCHTTP2Core.SocketAddress {
3839
}
3940

4041
extension NIOCore.SocketAddress {
42+
package init(_ socketAddress: GRPCHTTP2Core.SocketAddress) throws {
43+
if let ipv4 = socketAddress.ipv4 {
44+
self = try Self(ipv4)
45+
} else if let ipv6 = socketAddress.ipv6 {
46+
self = try Self(ipv6)
47+
} else if let unixDomainSocket = socketAddress.unixDomainSocket {
48+
self = try Self(unixDomainSocket)
49+
} else {
50+
throw RPCError(
51+
code: .internalError,
52+
message:
53+
"Unsupported mapping to NIOCore/SocketAddress for GRPCHTTP2Core/SocketAddress: \(socketAddress)."
54+
)
55+
}
56+
}
57+
4158
package init(_ address: GRPCHTTP2Core.SocketAddress.IPv4) throws {
4259
try self.init(ipAddress: address.host, port: address.port)
4360
}

Sources/GRPCHTTP2Core/Server/HTTP2ServerTransport.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension HTTP2ServerTransport {
2626
}
2727

2828
extension HTTP2ServerTransport.Config {
29-
public struct Compression: Sendable {
29+
public struct Compression: Sendable, Hashable {
3030
/// Compression algorithms enabled for inbound messages.
3131
///
3232
/// - Note: `CompressionAlgorithm.none` is always supported, even if it isn't set here.
@@ -46,7 +46,7 @@ extension HTTP2ServerTransport.Config {
4646
}
4747

4848
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
49-
public struct Keepalive: Sendable {
49+
public struct Keepalive: Sendable, Hashable {
5050
/// The amount of time to wait after reading data before sending a keepalive ping.
5151
public var time: Duration
5252

@@ -80,7 +80,7 @@ extension HTTP2ServerTransport.Config {
8080
}
8181

8282
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
83-
public struct ClientKeepaliveBehavior: Sendable {
83+
public struct ClientKeepaliveBehavior: Sendable, Hashable {
8484
/// The minimum allowed interval the client is allowed to send keep-alive pings.
8585
/// Pings more frequent than this interval count as 'strikes' and the connection is closed if there are
8686
/// too many strikes.
@@ -107,7 +107,7 @@ extension HTTP2ServerTransport.Config {
107107
}
108108

109109
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
110-
public struct Connection: Sendable {
110+
public struct Connection: Sendable, Hashable {
111111
/// The maximum amount of time a connection may exist before being gracefully closed.
112112
public var maxAge: Duration?
113113

@@ -142,7 +142,7 @@ extension HTTP2ServerTransport.Config {
142142
}
143143
}
144144

145-
public struct HTTP2: Sendable {
145+
public struct HTTP2: Sendable, Hashable {
146146
/// The maximum frame size to be used in an HTTP/2 connection.
147147
public var maxFrameSize: Int
148148

@@ -175,7 +175,7 @@ extension HTTP2ServerTransport.Config {
175175
}
176176
}
177177

178-
public struct RPC: Sendable {
178+
public struct RPC: Sendable, Hashable {
179179
/// The maximum request payload size.
180180
public var maxRequestPayloadSize: Int
181181

Sources/GRPCHTTP2TransportNIOPosix/HTTP2ClientTransport+Posix.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
public import GRPCCore
1818
public import GRPCHTTP2Core // should be @usableFromInline
19-
public import NIOCore
19+
public import NIOCore // has to be public because of EventLoopGroup param in init
2020
public import NIOPosix // has to be public because of default argument value in init
2121

2222
#if canImport(NIOSSL)
23-
import NIOSSL
23+
private import NIOSSL
2424
#endif
2525

2626
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
2727
extension HTTP2ClientTransport {
2828
/// A ``GRPCCore/ClientTransport`` using HTTP/2 built on top of `NIOPosix`.
2929
///
3030
/// This transport builds on top of SwiftNIO's Posix networking layer and is suitable for use
31-
/// on Linux and Darwin based platform (macOS, iOS, etc.) However, it's *strongly* recommended
31+
/// on Linux and Darwin based platforms (macOS, iOS, etc.). However, it's *strongly* recommended
3232
/// that if you are targeting Darwin platforms then you should use the `NIOTS` variant of
3333
/// the ``GRPCHTTP2Core/HTTP2ClientTransport``.
3434
///
@@ -62,7 +62,7 @@ extension HTTP2ClientTransport {
6262
public struct Posix: ClientTransport {
6363
private let channel: GRPCChannel
6464

65-
/// Creates a new Posix based HTTP/2 client transport.
65+
/// Creates a new NIOPosix-based HTTP/2 client transport.
6666
///
6767
/// - Parameters:
6868
/// - target: A target to resolve.
@@ -91,7 +91,6 @@ extension HTTP2ClientTransport {
9191
)
9292
}
9393

94-
// Configure a connector.
9594
self.channel = GRPCChannel(
9695
resolver: resolver,
9796
connector: try Connector(eventLoopGroup: eventLoopGroup, config: config),

Sources/GRPCHTTP2TransportNIOPosix/HTTP2ServerTransport+Posix.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -425,24 +425,6 @@ extension HTTP2ServerTransport.Posix {
425425
}
426426
}
427427

428-
extension NIOCore.SocketAddress {
429-
fileprivate init(_ socketAddress: GRPCHTTP2Core.SocketAddress) throws {
430-
if let ipv4 = socketAddress.ipv4 {
431-
self = try Self(ipv4)
432-
} else if let ipv6 = socketAddress.ipv6 {
433-
self = try Self(ipv6)
434-
} else if let unixDomainSocket = socketAddress.unixDomainSocket {
435-
self = try Self(unixDomainSocket)
436-
} else {
437-
throw RPCError(
438-
code: .internalError,
439-
message:
440-
"Unsupported mapping to NIOCore/SocketAddress for GRPCHTTP2Core/SocketAddress: \(socketAddress)."
441-
)
442-
}
443-
}
444-
}
445-
446428
extension ServerBootstrap {
447429
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
448430
fileprivate func bind<Output: Sendable>(

0 commit comments

Comments
 (0)