grpc/examples/cpp/compression
Esun Kim 5fb4c2364d
Clean-up pkgconfig (#31660)
* Clean-up pkgconfig

* Removed run_distrib_test_cmake_module_install_pkgconfig.sh

* Proper re2

* Clean up pkgconfig.sh test script
2022-11-22 10:51:17 -08:00
..
BUILD Add a test for a (now-illegal) build file construct (#27602) 2021-10-06 07:43:59 -07:00
CMakeLists.txt Added CMakeLists.txt to cpp/examples (#25692) 2021-03-17 19:59:23 -07:00
Makefile Clean-up pkgconfig (#31660) 2022-11-22 10:51:17 -08:00
README.md get rid of the https://grpc.io/release plague 2020-02-04 15:01:03 +01:00
greeter_client.cc clang-format C++ examples (#25764) 2021-03-22 10:23:52 -07:00
greeter_server.cc clang-format C++ examples (#25764) 2021-03-22 10:23:52 -07:00

README.md

gRPC C++ Message Compression Tutorial

Prerequisite

Make sure you have run the hello world example or understood the basics of gRPC. We will not dive into the details that have been discussed in the hello world example.

Get the tutorial source code

The example code for this and our other examples lives in the examples directory. Clone this repository at the latest stable release tag to your local machine by running the following command:

$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc

Change your current directory to examples/cpp/compression

$ cd examples/cpp/compression/

Generating gRPC code

To generate the client and server side interfaces:

$ make helloworld.grpc.pb.cc helloworld.pb.cc

Which internally invokes the proto-compiler as:

$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto

Writing a client and a server

The client and the server can be based on the hello world example.

Additionally, we can configure the compression settings.

In the client, set the default compression algorithm of the channel via the channel arg.

  ChannelArguments args;
  // Set the default compression algorithm for the channel.
  args.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP);
  GreeterClient greeter(grpc::CreateCustomChannel(
      "localhost:50051", grpc::InsecureChannelCredentials(), args));

Each call's compression configuration can be overwritten by client context.

    // Overwrite the call's compression algorithm to DEFLATE.
    context.set_compression_algorithm(GRPC_COMPRESS_DEFLATE);

In the server, set the default compression algorithm via the server builder.

  ServerBuilder builder;
  // Set the default compression algorithm for the server.
  builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_GZIP);

Each call's compression configuration can be overwritten by server context.

    // Overwrite the call's compression algorithm to DEFLATE.
    context->set_compression_algorithm(GRPC_COMPRESS_DEFLATE);

For a working example, refer to greeter_client.cc and greeter_server.cc.

Build and run the (compressing) client and the server by the following commands.

make
./greeter_server
./greeter_client