Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gRPC using SwiftNIO #121

Merged
merged 25 commits into from Mar 28, 2023
Merged

Add support for gRPC using SwiftNIO #121

merged 25 commits into from Mar 28, 2023

Conversation

rebello95
Copy link
Contributor

@rebello95 rebello95 commented Mar 20, 2023

Summary

This PR introduces support for the vanilla gRPC protocol by providing a new ConnectNIO library which utilizes SwiftNIO (which provides support for trailers) to expose a new .grpc extension on NetworkProtocol.

Note that this new library is not available through CocoaPods because SwiftNIO no longer supports CocoaPods.

Details

  • In order to avoid requiring consumers who are using the Connect or gRPC-Web protocols to depend on SwiftNIO, a new ConnectNIO module is being introduced alongside the existing Connect module. This means that existing Connect/gRPC-Web users do not need to include this new dependency.
  • This module contains a new NIOHTTPClient type which wraps SwiftNIO concepts and conforms to Connect.HTTPClientInterface.
  • The ConnectNIO module also contains logic to support gRPC, which is exposed as an extension on NetworkProtocol (demonstrated below).
  • Consumers who want to use the NIO-based HTTP implementation with other protocols can accomplish this by including ConnectNIO and replacing URLSessionHTTPClient usage with NIOHTTPClient.

Example

The Eliza Swift Package Manager example has been updated to include support for gRPC using the NIOHTTPClient. Using gRPC with generated Connect clients is as easy as adding an import and making a 2-line change:

import Connect
import ConnectNIO // Include NIO and gRPC support

...

let protocolClient = ProtocolClient(
    httpClient: NIOHTTPClient(host: "https://demo.buf.build"), // Use NIO
    config: ProtocolClientConfig(
        host: "https://demo.buf.build",
        networkProtocol: .grpc, // Use gRPC
        codec: ProtoCodec()
    )
)

Binary size impact

There is no binary size impact on clients that only include the Connect library. When including ConnectNIO, SwiftNIO dependencies get compiled in, resulting in a ~2.1MB increase on an unstripped .ipa. For example:

  • Eliza app with Connect: 699KB
  • Eliza app with ConnectNIO: 2.8MB

Testing

The suite of crosstests has been updated to run a matrix of tests which include:

  • Connect + (JSON & Proto binary) using (URLSessionHTTPClient & NIOHTTPClient)
  • gRPC-Web + (JSON & Proto binary) using (URLSessionHTTPClient & NIOHTTPClient)
  • gRPC + (JSON & Proto binary) using (NIOHTTPClient)

@rebello95 rebello95 marked this pull request as ready for review March 21, 2023 18:12
@rebello95 rebello95 merged commit 375f492 into main Mar 28, 2023
13 checks passed
@rebello95 rebello95 deleted the nio branch March 28, 2023 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants