Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@qcam
Last active August 30, 2016 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcam/2011a2d7ea35de4c479894f3e4869196 to your computer and use it in GitHub Desktop.
Save qcam/2011a2d7ea35de4c479894f3e4869196 to your computer and use it in GitHub Desktop.
[HARDCORE] gRPC

What's gRPC?

In gRPC a client application can directly call methods on a server application on a different machine as if it was a local object, making it easier for you to create distributed applications and services. As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.

gRPC clients and servers can communicate no matter what programming languages they're written in. For instance, you can easily create gRPC servers in C++ and clients in Ruby/Python.

Transport and Semantics

gRPC uses (Protocol Buffers, aka. Protobuf)(https://github.com/google/protobuf), for data serialization.

Concept

This is how you can define a service in gRPC

service HelloService {
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string greeting = 1;
}

message HelloResponse {
  string reply = 1;
}

gRPC lets you define four kinds of service method:

Unary RPC

The client sends a single request to the server and gets a single response back, just like a normal function call.

rpc SayHello(HelloRequest) returns (HelloResponse){
}

Server streaming RPC

The client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages.

rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse){
}

Client streaming RPCs

The client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them and return its response.

rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {
}

Bidirectional streaming RPCs

Both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved.

rpc BidiHello(stream HelloRequest) returns (stream HelloResponse){
}

Authentication

The following authentication mechanisms are built-in to gRPC:

  1. SSL/TLS: gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the server, and to encrypt all the data exchanged between the client and the server.

  2. Token-based authentication with Google

Wire format

The transportation of gRPC is based on HTTP/2.

Errors handling

gRPC provides multiple built-in errors. See at: http://www.grpc.io/docs/guides/error.html#error-status-codes

References

  1. gRPC Guides - http://www.grpc.io/docs/guides/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment