Swiftpack.co - Package - grpc/grpc-swift

Build Status

Swift gRPC

This repository contains an experimental Swift gRPC API and code generator.

It is intended for use with Apple's swift-protobuf support for Protocol Buffers. Both projects contain code generation plugins for protoc, Google's Protocol Buffer compiler, and both contain libraries of supporting code that is needed to build and run the generated code.

APIs and generated code is provided for both gRPC clients and servers, and can be built either with Xcode or the Swift Package Manager. Support is provided for all four gRPC API styles (Unary, Server Streaming, Client Streaming, and Bidirectional Streaming) and connections can be made either over secure (TLS) or insecure channels.

The Echo example provides a comprehensive demonstration of currently-supported features.

Swift Package Manager builds may also be made on Linux systems. Please see DOCKER.md and LINUX.md for details.

CocoaPods integration

Swift gRPC is currently available from CocoaPods. To integrate, add the following line to your Podfile:

pod 'SwiftGRPC'

Then, run pod install from command line and use your project's generated .xcworkspace file.

Manual integration

When not using CocoaPods, Swift gRPC includes vendored copies of the gRPC Core library and BoringSSL (an OpenSSL fork that is used by the gRPC Core). These are built automatically in Swift Package Manager builds.

After building your project, add the generated SwiftGRPC.xcodeproj to your project, and add build dependencies on BoringSSL, CgRPC, and SwiftGRPC.

Please also note that your project will need to include the SwiftProtobuf.xcodeproj from Swift Protobuf and the source files that you generate with protoc/plugins.

See Echo for a working Xcode-based example, and don't hesitate to file issues if you find any problems.

Usage

The recommended way to use Swift gRPC is to first define an API using the Protocol Buffer language and then use the Protocol Buffer Compiler and the Swift Protobuf and Swift gRPC plugins to generate the necessary support code.

Getting the plugins

Binary releases of protoc, the Protocol Buffer Compiler, are available on GitHub.

To build the plugins, run make plugin in the main directory. This uses the Swift Package Manager to build both of the necessary plugins: protoc-gen-swift, which generates Protocol Buffer support code and protoc-gen-swiftgrpc, which generates gRPC interface code.

To install these plugins, just copy the two executables (protoc-gen-swift and protoc-gen-swiftgrpc) that show up in the main directory into a directory that is part of your PATH environment variable.

Using the plugins

To use the plugins, protoc and both plugins should be in your search path (see above). Invoke them with commands like the following:

protoc <your proto files> \
    --swift_out=. \
    --swiftgrpc_out=.

By convention the --swift_out option invokes the protoc-gen-swift plugin and --swiftgrpc_out invokes protoc-gen-swiftgrpc.

Parameters

To pass extra parameters to the plugin, use a comma-separated parameter list separated from the output directory by a colon.

| Flag | Values | Default | Description | |:-|:-|:-|:-| | Visibility | Internal/Public | Internal | ACL of generated code | | Server | true/false | true | Whether to generate server code | | Client | true/false | true | Whether to generate client code | | Async | true/false | true | Whether to generate asynchronous code | | Sync | true/false | true | Whether to generate synchronous code | | Implementations | true/false | true | Whether to generate protocols and non-test service code. Toggling this to false is mostly useful when combined with TestStubs=true to generate files containing only test stub code | | TestStubs | true/false | false | Whether to generate test stub code | | FileNaming | FullPath/PathToUnderscores/DropPath | FullPath | How to handle the naming of generated sources | | ExtraModuleImports | String | `` | Extra module to import in generated code. This parameter may be included multiple times to import more than one module |

Example:

$ protoc <your proto> --swiftgrpc_out=Client=true,Server=false:.

Building your project

Most grpc-swift development is done with the Swift Package Manager. For usage in Xcode projects, we rely on the swift package generate-xcodeproj command to generate an Xcode project for the grpc-swift core libraries.

The top-level Makefile uses the Swift Package Manager to generate an Xcode project for the SwiftGRPC package:

$ make && make project

This will create SwiftGRPC.xcodeproj, which you should add to your project, along with setting the necessary build dependencies mentioned above.

Low-level gRPC

While the recommended way to use gRPC is with Protocol Buffers and generated code, at its core gRPC is a powerful HTTP/2-based communication system that can support arbitrary payloads. As such, each gRPC library includes low-level interfaces that can be used to directly build API clients and servers with no generated code. For an example of this in Swift, please see the Simple example.

Known issues

The SwiftGRPC implementation that is backed by gRPC-Core (and not SwiftNIO) is known to have some connectivity issues on iOS clients - namely, silently disconnecting (making it seem like active calls/connections are hanging) when switching between wifi <> cellular or between cellular technologies (3G <> LTE). The root cause of these problems is that the backing gRPC-Core doesn't get the optimizations made by iOS' networking stack when these types of changes occur, and isn't able to handle them itself.

There is also documentation of this behavior in this gRPC-Core readme.

To aid in this problem, there is a ClientNetworkMonitor that monitors the device for events that can cause gRPC to disconnect silently. We recommend utilizing this component to call shutdown() (or destroy) any active Channel instances, and start new ones when the network is reachable.

Setting the keepAliveTimeout argument on channels is also encouraged.

Details:

  • Switching between wifi <> cellular: Channels silently disconnect
  • Switching between 3G <> LTE (etc.): Channels silently disconnect
  • Network becoming unreachable: Most times channels will time out after a few seconds, but ClientNetworkMonitor will notify of these changes much faster
  • Switching between background <> foreground: No known issues

Original SwiftGRPC issue: https://github.com/grpc/grpc-swift/issues/337.

Having build problems?

grpc-swift depends on Swift, Xcode, and swift-protobuf. We are currently testing with the following versions:

  • Xcode 10.2
  • Swift 4.2 / 5.0
  • swift-protobuf 1.5.0

SwiftGRPCNIO package

SwiftGRPCNIO is a clean-room implementation of the gRPC protocol on top of the SwiftNIO library. This implementation is not yet production-ready as it lacks several things recommended for production use:

  • Better test coverage
  • Full error handling
  • SSL support
  • Client support
  • Example projects
  • iOS support
  • Removal of the libnghttp2 dependency from SwiftNIOHTTP2

However, if you are planning to implement a gRPC service based on SwiftNIO or the Vapor framework, you might find this package useful. In addition, once ready, this package should provide more predictable and reliable behavior in the future, combined with an improved API and better developer experience.

You may also want to have a look at this presentation for more details on the motivation for this package.

License

grpc-swift is released under the same license as gRPC, repeated in LICENSE.

Contributing

Please get involved! See our guidelines for contributing.

Releasing

When issuing a new release, the following steps should be followed:

  1. Run the CocoaPods linter to ensure that there are no new warnings/errors:

    $ pod spec lint SwiftGRPC.podspec

  2. Update the Carthage Xcode project (diff will need to be checked in with the version bump):

    $ make project-carthage

  3. Bump the version in the SwiftGRPC.podspec file

  4. Merge these changes, then create a new Release with corresponding Tag. Be sure to include a list of changes in the message

  5. Push the update to the CocoaPods specs repo:

    $ pod trunk push

Github

link
Stars: 644
Help us keep the lights on

Used By

Total:

Releases

0.9.0 - Apr 18, 2019

This release contains fixes for Swift 5 warnings that were emitted by SwiftGRPC and SwiftProtobuf.

  • Bump version to 0.9.0 (#444) - Michael Rebello (d5f0c46)
  • Try de-flaking testMultipleConnectivityObserversAreCalled. (#441) - Daniel Alm (c0eca15)
  • Update SwiftProtobuf to 1.5.0 (#440) - Michael Rebello (2d822f1)
  • Run CI on both Swift 4.2 and 5, and fix Swift 5 warning (#426) - Michael Rebello (0f2af51)
  • Change the swiftc argument to use static Swift libraries. (#424) - Daniel Alm (6bbc15c)

0.8.2 - Apr 11, 2019

  • Update vendored gRPC-core to v1.19.1. This commit also fixes a few minor problems in the vendoring scripts and updates a call in shim/channel.c to use a modified API.

  • Update ClientCancellingTests to give clients more time to cancel before the server finishes streaming.

  • Update Roots.swift to use the latest roots.pem from gRPC.

  • Add SleepingEchoProvider.

  • Updating podspec. gRPC-Core dependency is v1.19.0. There was no podspec published for gRPC-Core v1.19.1 but it has no changes in the C layer: vendoring v1.19.0 yields the same code as vendoring v1.19.1.

  • Changes from running "make project-carthage"

0.8.1 - Mar 28, 2019

  • Update to 0.8.1 - Michael Rebello (f8738a6)
  • Disable iOS 12 cellular connectivity monitor by default - Michael Rebello (58a009b)
  • Implement async client test stub methods (#378) - Carlos Pages (630dfa5)
  • Temporarily disable NIOClientTimeoutTests.testBidirectionalStreamingTimeoutAfterSending on Linux for now (#409) - Daniel Alm (4df501a)
  • Use FileHandle instead of io.swift. (#408) - Martin Petrov (1d056f1)
  • Update Echo certificate and key (#407) - George Barnett (14734c7)
  • Rewrite LengthPrefixedMessageReader and tests (#397) - George Barnett (143255e)
  • Allow generating test stubs separately from the client implementation (#402) - Martin Petrov (869c168)
  • Generate client test stubs in a dedicated section. (#403) - Martin Petrov (90a5498)
  • Add public init to client call unary stubs. (#401) - Martin Petrov (dc2e8bb)
  • Mark unary async calls as @discardableResult. (#399) - Martin Petrov (f7807da)
  • Generate ClientCallUnary test stubs. (#398) - Martin Petrov (c0478aa)
  • Add/update scripts for vendoring dependencies (#396) - Michael Rebello (9745a76)
  • Fix crashes due to mismatching responses sent to the channel when event observer factories fail. (#395) - Daniel Alm (772b78e)
  • Enforce request cardinality for unary-request calls also for the case of zero request messages being sent. (#392) - Daniel Alm (d4a6366)
  • Add public factory methods for CallResult. (#394) - Martin Petrov (140d34a)
  • Make a few more write calls flushing. (#391) - Daniel Alm (30a7fbc)
  • Refactor some properties shared by all server-side call contexts into a common ServerCallContext protocol … (#389) - Daniel Alm (c082197)

0.8.0 - Mar 1, 2019

  • Update CocoaPods & Carthage for 0.8.0 release (#370) - Michael Rebello (8b064bd)
  • Add ClientNetworkMonitor for tracking network changes (#387) - Michael Rebello (3b3c5c5)
  • First pass implementation of NIO client (#357) - George Barnett (97ff923)
  • Clean up gRPC.swift (#386) - Michael Rebello (ee0f374)
  • Add ability to manually shut down channels (#384) - Michael Rebello (930440a)
  • Update releasing instructions (#383) - Michael Rebello (f5c0d63)
  • Comments/cleanup to Package.swift. (#371) - Daniel Alm (ba335b2)
  • Refactor channel connectivity to avoid multiple spin loops (#380) - Michael Rebello (10aff09)
  • Improve error handling in NIO server. (#364) - George Barnett (158c4ef)
  • Fix channel credentials memory leak in shims (#369) - Kevin Sweeney (587218a)
  • Make ConnectivityObserver class final (#375) - Michael Rebello (5f24269)
  • Store the root certificates as a multi-line string literal. (#372) - Daniel Alm (ac3e175)
  • Run ChannelCrashTests on Linux/CI (#379) - Michael Rebello (b899a30)
  • Move ConnectivityState to new file & update initializer (#376) - Michael Rebello (9dff24d)
  • Merge pull request #350 from sergiocampama/http1 - Tim Burks (ac1939e)
  • Adds Echo Web example with functional prototype - Sergio Campama (86748a7)
  • Add HTTP1 Tests for GRPCSwiftNIO - Sergio Campama (eee57da)
  • Add gRPC Web support. - Sergio Campama (24c8e5d)
  • Proposal to fix #362. (#363) - Alexey Gordeev (0195dfd)
  • Allow client to specify metadata per call (#356) - Taeho Kim (3cd505a)
  • Update SwiftProtobuf to 1.3.1 (#367) - Michael Rebello (96bf49e)
  • Improve memory management in Channel (#368) - Kevin Sweeney (fcb8ab3)
  • XCode 10.1 + Carthage support (#360) - Dmitry Malakhov (99d3834)
  • Upgrade swift-nio to 1.12 - George Barnett (a43337c)
  • Allow "FileNaming" option to be set via CLI - George Barnett (78c5fed)
  • Bump podspec to 0.7.0 - Michael Rebello (6928fb2)

0.7.0 - Dec 22, 2018

  • Support proto-gen-swift's proto-to-module mapping. (ff2d161 - Tony Allevato)
  • Update Linux build documentation (#346) (824814b - George Barnett)
  • Add libnghttp2 depdendency to Docker image (#338) (#344) (c3a03f1 - George Barnett)
  • Add environment flag for BoringSSL/OpenSSL on MacOS (#341) (54659b8 - james h)
  • First implementation of gRPC based on SwiftNIO (#281) (bf20e93 - Daniel Alm)
  • Remove .swift-version file (d3ad74b - Yosuke Ishikawa)
  • Add support for RPC status (#331) (b6dc500 - Sebastian Thiebaud)
  • Fix crash during Channel with subscription is destroying (#328) (2fd06dc - slavabulgakov)
  • Improve the documentation on building the protoc plugins and add a make plugin option. (48c274f - Daniel Alm)
  • Remove build-carthage altogether from CI for now. (7e6e28c - Daniel Alm)
  • Pull build-carthage to the beginning of the build process for faster CI iteration times on failing Carthage builds. (8cd1420 - Daniel Alm)
  • Minor tweaks in preparation for SwiftGRPCNIO. (00d18f6 - Daniel Alm)
  • Transmission type parameters for sync/async code generation (611e527 - Pontus Andersson)
  • Re-run make project-carthage (6cc5371 - Yosuke Ishikawa)
  • Replace absolute path with relative path after generating xcodeproj (30441f7 - Yosuke Ishikawa)
  • Docs/add question template (#310) (0faf1d6 - tikidunpon)
  • Add io.grpc. prefix to bundle identifier (c9b15b8 - Yosuke Ishikawa)
  • Update README.md (02b8fc5 - Daniel Alm)
  • Update README.md (8ff1778 - Daniel Alm)
  • Update README.md (ec7c3e4 - Daniel Alm)
  • Update README.md (52195d2 - Daniel Alm)
  • Tweak Echo Readme (see #273) (a387dd6 - Daniel Alm)
  • docs: add a mention about resolved issues about ssl. (5ba63b6 - tikidunpon)
  • docs: add issue template for bug reporting. (660adca - tikidunpon)