tRPC-Cpp Documentation

repository·main·Indexed 18 days ago

https://github.com/trpc-group/trpc-cpp

A high-performance C++ implementation of the tRPC framework featuring a modular architecture with a core runtime and extensible plugins. It supports scalable RPC services, various protocols, serialization formats, and observability tools. Key features include an HTTP admin interface for configuration and profiling, RPC filters via RpcServerFilter and RpcClientFilter for request interception, and support for fiber and future forwarding. Build systems supported include Bazel and CMake.

Tokens
184.7K
Snippets
478
Records
690
Agent score
59%

What's inside tRPC-Cpp

  1. Overview of the tRPC-Cpp management service

    main

    The tRPC-Cpp management service is an HTTP-based service that allows users to monitor and manage the status of their services. It provides interfaces to:

    • View framework version and configuration.
    • View and modify log levels.
    • View server-side statistics (connection count, request count, latency, etc.).
    • View framework and user-defined tvar variables.
    • Collect system resource usage (CPU, memory).
    • View rpcz information.
    • Access Prometheus metrics.
    • Customize management commands.

    You can access this service via a web browser or by making manual HTTP requests (e.g., using curl).

  2. Overview of tRPC-Cpp

    main
    tRPC-Cpp is a high-performance, pluggable RPC framework for C++. It is the C++ implementation of the tRPC design principles, designed to support various business scenarios by providing multiple runtime models and protocol support. It allows developers to build services supporting multiple protocols (trpc, http(s), grpc) and access backend services synchronously, asynchronously, or via one-way calls.
  3. Overview of Tvar statistics library

    main

    Tvar is a multi-threaded statistics library within the tRPC-Cpp framework. It is designed to help users record and track various program states during execution.

    Design Philosophy: Tvar is optimized for scenarios with frequent writes and infrequent reads. It achieves high write performance by using a thread_local mechanism, which avoids the cache bouncing issues associated with global atomic variables used in frameworks like Envoy. This approach is similar to brpc and flare, prioritizing write efficiency over read efficiency.

  4. Overview of tvar implementation

    main

    The tvar directory provides a collection of specialized thread-safe variables designed for high-performance telemetry and monitoring.

    Design Philosophy

    tvar is optimized for write-heavy scenarios. It utilizes a thread-local mechanism to maximize write performance by reducing contention between threads. As a trade-off, this design results in lower read performance compared to traditional atomic variables, making it ideal for high-frequency updates (like counters or latency tracking) where reading the aggregate value is less frequent than updating it.

  5. Overview of tRPC-Cpp Features

    main

    tRPC-Cpp provides a comprehensive set of features for building high-performance RPC services:

    Runtime & Server/Client Capabilities

    • Thread Models: Supports fiber (M:N coroutine) and thread (merged or separate IO/handle models).
    • I/O Models: Supports reactor (network) and async-io (disk).
    • Networking: Supports tcp, udp, ssl, and unix domain socket for both servers and clients.
    • RPC Implementation: Supports rpc, streaming-rpc, and non-rpc calls.
    • Connection Management: Clients support conn-complex, conn-pool, and conn-pipeline.

    Plugin Ecosystem

    • Protocols (Codec): trpc, http(s/2.0), grpc, etc.
    • Serialization: pb (Protocol Buffers), flatbuffers, json, and noop (text/binary).
    • Compression: gzip, zlib, snappy, and lz4.
    • Observability & Management:
      • Naming: polarismesh
      • Config: etcd
      • Logging: cls
      • Metrics: prometheus
      • Tracing: jaeger
      • Telemetry: opentelemetry

    Tools & Components

    • Tools: admin, tvar, rpcz, and proto IDL dependency management.
    • Components: Includes a redis client.
  6. Explore tRPC-Cpp Features and Plugins

    main

    tRPC-Cpp provides extensive support through its core and plugin ecosystem:

    Runtime & Networking

    • Thread Models: fiber (M:N coroutine) or thread (IO/handle merge or separate).
    • IO Models: reactor (network) and async-io (disk).
    • Server Transport: tcp, udp, ssl, and unix domain socket.
    • Client Transport: tcp, udp, and ssl.
    • Client Connection: Supports long connections (connection reuse, connection pools, pipeline) and short connections.

    Protocols & Serialization

    • Protocols: trpc, http (including http/2.0), grpc, etc.
    • Serialization: pb, flatbuffers, json, and noop (text/binary).
    • Compression: gzip, zlib, snappy, and lz4.

    Ecosystem Plugins

    • Naming Service: polarismesh.
    • Configuration: etcd.
    • Logging: cls (remote logging).
    • Monitoring & Observability: prometheus (metrics), jaeger (tracing), and opentelemetry (telemetry).

    Tools & Components

    • Tools: admin, tvar, rpcz, and proto IDL dependency management.
    • Components: redis client.
  7. Project Structure of Thirdparty Protocol Demo

    main

    The demo is organized into three main components:

    • client/: Contains the client implementation (client.cc) and its configuration (trpc_cpp_fiber.yaml).
    • server/: Contains the server implementation (demo_server.cc) and its configuration (trpc_cpp_fiber.yaml).
    • common/: Contains the implementation of the thirdparty protocol, including codecs (demo_client_codec.cc, demo_server_codec.cc) and protocol definitions (demo_protocol.cc).
    examples/features/thirdparty_protocol/
    ├── client
    │   ├── BUILD
    │   ├── client.cc
    │   └── trpc_cpp_fiber.yaml
    ├── CMakeLists.txt
    ├── common
    │   ├── BUILD
    │   ├── demo_client_codec.cc
    │   ├── demo_client_codec.h
    │   ├── demo_protocol.cc
    │   ├── demo_protocol.h
    │   ├── demo_server_codec.cc
    │   └── demo_server_codec.h
    ├── README.md
    ├── run_cmake.sh
    ├── run.sh
    └── server
        ├── BUILD
        ├── demo_server.cc
        └── trpc_cpp_fiber.yaml
  8. Run the gRPC streaming example

    main

    The grpc_stream example demonstrates a streaming server implemented using the tRPC framework, which transforms a standard gRPC framework server example into a tRPC-based one.

    Note on Runtime Requirements: You must use the Fiber thread model for this example to function correctly.

    Current Status: The streaming server is implemented, but the gRPC streaming client is still in progress.

  9. Access standard HTTP services with tRPC-Cpp

    main
    tRPC-Cpp allows you to access standard HTTP services (Plain Text or JSON) or RPC services (Protobuf) using an HTTP client. This enables a single RPC service to support both tRPC and HTTP protocols simultaneously. You can use the HttpServiceProxy to perform standard HTTP methods like GET, HEAD, and POST.
  10. What is Fiber and how does it work?

    main

    Fiber is an M:N coroutine implementation in tRPC-Cpp, similar to goroutines in Go. It schedules multiple user-level threads (Fibers) onto multiple native system threads.

    Key Advantages:

    • Synchronous Programming: Facilitates easier usability compared to asynchronous callbacks.
    • Efficiency: Lower cost for creation and context switching compared to native threads.
    • High Parallelism: Capable of running concurrently on multiple threads to fully utilize system resources.

    Core Characteristics:

    • Scheduling Groups: Uses multiple groups to reduce contention and improve multi-core scalability.
    • Work-Stealing: Supports fiber stealing between scheduling groups (intra-NUMA and inter-NUMA) to balance workloads.
    • Synchronization: Provides comprehensive synchronization primitives and good interoperability with Pthreads.
    • API Style: Supports Future-style APIs.