gRPC Server Reflection Support

gRPC Server Reflection Support in Pixiu

gRPC Server Reflection Support

Implementation Reference

The gRPC Proxy filter (dgp.filter.grpc.proxy) now supports gRPC Server Reflection, enabling dynamic message parsing and inspection at the gateway level without requiring pre-compiled proto files.

Overview

gRPC Server Reflection is a feature that allows Pixiu gateway to dynamically discover and decode gRPC service definitions at runtime. This eliminates the need to maintain proto files in the gateway configuration.

Key Features

  • Three Reflection Modes: Passthrough, Reflection, and Hybrid
  • Dynamic Message Decoding: Parse messages at runtime without proto files
  • TTL-based Caching: Efficient descriptor caching with automatic cleanup
  • Protocol Detection: Support for both gRPC and Triple protocols
  • Graceful Fallback: Hybrid mode provides automatic passthrough fallback

Reflection Modes

Passthrough Mode (Default)

Performs transparent binary proxying without decoding messages.

Use Cases:

  • High-performance scenarios where message inspection is not needed
  • Simple routing based on service/method names only

Configuration:

grpc_filters:
  - name: dgp.filter.grpc.proxy
    config:
      reflection_mode: "passthrough"  # or omit (default)

Reflection Mode

Uses gRPC Server Reflection API to dynamically decode and inspect message contents.

Use Cases:

  • Content-aware routing (route based on message fields)
  • Field-level filtering or transformation
  • Logging and debugging with full message inspection

Configuration:

grpc_filters:
  - name: dgp.filter.grpc.proxy
    config:
      reflection_mode: "reflection"
      descriptor_cache_ttl: 300  # 5 minutes cache

Hybrid Mode

Tries reflection first, falls back to passthrough on failure.

Use Cases:

  • Mixed environments with varying reflection support
  • Migration scenarios (gradually enabling reflection)
  • Production environments requiring high availability

Configuration:

grpc_filters:
  - name: dgp.filter.grpc.proxy
    config:
      reflection_mode: "hybrid"
      reflection_timeout: 5s

Complete Configuration Example

static_resources:
  listeners:
    - name: "grpc-gateway"
      protocol_type: "GRPC"
      address:
        socket_address:
          address: "0.0.0.0"
          port: 8882
      filter_chains:
        filters:
          - name: dgp.filter.network.grpcconnectionmanager
            config:
              route_config:
                routes:
                  - match:
                      prefix: "/echo.EchoService/"
                    route:
                      cluster: "echo-grpc"
              grpc_filters:
                - name: dgp.filter.grpc.proxy
                  config:
                    # Reflection mode (default: "passthrough")
                    reflection_mode: "reflection"

                    # Cache TTL for method descriptors (seconds)
                    descriptor_cache_ttl: 300

                    # Enable Triple protocol detection
                    enable_protocol_detection: true

                    # Reflection timeout for hybrid mode
                    reflection_timeout: 5s

  clusters:
    - name: "echo-grpc"
      lb_policy: "RoundRobin"
      endpoints:
        - socket_address:
            address: 127.0.0.1
            port: 50051
            protocol_type: "GRPC"

Configuration Fields

FieldTypeDefaultDescription
reflection_modestring"passthrough"Reflection mode: "passthrough", "reflection", or "hybrid"
descriptor_cache_ttlint300Cache TTL for method descriptors in seconds
enable_protocol_detectionboolfalseEnable Triple protocol detection
reflection_timeoutstring"5s"Max time to wait for reflection in hybrid mode
enable_tlsboolfalseEnable TLS for backend connections
tls_cert_filestring""Path to TLS certificate file
tls_key_filestring""Path to TLS key file
keepalive_timestring"300s"Keepalive time for backend connections
keepalive_timeoutstring"5s"Keepalive timeout
connect_timeoutstring"5s"Connection timeout
max_concurrent_streamsuint320 (unlimited)Max concurrent streams

Enabling Server Reflection

To use reflection or hybrid modes, your backend gRPC server must have Server Reflection enabled.

Go (gRPC-Go)

import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/reflection"
)

func main() {
    server := grpc.NewServer()

    // Register your service
    echo.RegisterEchoServiceServer(server, &echoServer{})

    // Enable server reflection
    reflection.Register(server)

    // Start server
    lis, _ := net.Listen("tcp", ":50051")
    server.Serve(lis)
}

Java (gRPC-Java)

import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.reflection.v1alpha.ServerReflectionGrpc;

public class EchoServer {
    public static void main(String[] args) throws Exception {
        Server server = ServerBuilder
            .forPort(50051)
            .addService(new EchoServiceImpl())
            // Enable server reflection
            .addService(ServerReflectionGrpc.newInstance())
            .build()
            .start();

        server.awaitTermination();
    }
}

Python (gRPC-Python)

import grpc
from grpc_reflection.v1alpha import reflection
from concurrent import futures

class EchoService(echo_pb2_grpc.EchoServiceServicer):
    # ... implementation ...

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    echo_pb2_grpc.add_EchoServiceServicer_to_server(EchoService(), server)

    # Enable server reflection
    reflection.enable_server_reflection(
        service_names=[echo.DESCRIPTOR.services_by_name['EchoService'].full_name,
                       reflection.SERVICE_NAME],
        server=server
    )

    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

Mode Comparison

FeaturePassthroughReflectionHybrid
PerformanceBestGoodBetter
Message InspectionNoYesYes (when available)
Requires ReflectionNoYesOptional
FallbackN/ANoYes

Descriptor Cache

The reflection mode uses a TTL-based cache to store method descriptors retrieved from the backend server.

Recommended TTL Values:

  • Development: 60 (1 minute)
  • Testing: 300 (5 minutes)
  • Production: 1800 (30 minutes)

Triple Protocol Detection

Pixiu supports the Dubbo Triple protocol, a gRPC-compatible protocol developed by the Apache Dubbo community.

Enable protocol detection:

grpc_filters:
  - name: dgp.filter.grpc.proxy
    config:
      enable_protocol_detection: true

Troubleshooting

Problem: Reflection mode returns “service not found”

Cause: Backend server does not have Server Reflection enabled.

Solution: Enable reflection on your server:

reflection.Register(grpcServer)

Problem: Hybrid mode falls back to passthrough

Cause: Reflection timeout exceeded or reflection service unavailable.

Solution:

  1. Check if reflection is enabled on the backend
  2. Increase reflection_timeout value
  3. Check network connectivity