Skip to main content

Module grpc

Module grpc 

Source
Expand description

gRPC runtime support for Spikard

This module provides gRPC server infrastructure using Tonic, enabling Spikard to handle both HTTP/1.1 REST requests and HTTP/2 gRPC requests.

§Architecture

The gRPC support follows the same language-agnostic pattern as the HTTP handler:

  1. GrpcHandler trait: Language-agnostic interface for handling gRPC requests
  2. Service bridge: Converts between Tonic’s types and our internal representation
  3. Streaming support: Utilities for handling streaming RPCs
  4. Server integration: Multiplexes HTTP/1.1 and HTTP/2 traffic

§Example

use spikard_http::grpc::{GrpcHandler, GrpcRequestData, GrpcResponseData};
use std::sync::Arc;

// Implement GrpcHandler for your language binding
struct MyGrpcHandler;

impl GrpcHandler for MyGrpcHandler {
    fn call(&self, request: GrpcRequestData) -> Pin<Box<dyn Future<Output = GrpcHandlerResult> + Send>> {
        Box::pin(async move {
            // Handle the gRPC request
            Ok(GrpcResponseData {
                payload: bytes::Bytes::from("response"),
                metadata: tonic::metadata::MetadataMap::new(),
            })
        })
    }

    fn service_name(&self) -> &str {
        "mypackage.MyService"
    }
}

// Register with the server
let handler = Arc::new(MyGrpcHandler);
let config = GrpcConfig::default();

Re-exports§

pub use framing::parse_grpc_client_stream;
pub use handler::GrpcHandler;
pub use handler::GrpcHandlerResult;
pub use handler::GrpcRequestData;
pub use handler::GrpcResponseData;
pub use handler::RpcMode;
pub use service::GenericGrpcService;
pub use service::copy_metadata;
pub use service::is_grpc_request;
pub use service::parse_grpc_path;
pub use streaming::MessageStream;
pub use streaming::StreamingRequest;
pub use streaming::StreamingResponse;

Modules§

framing
HTTP/2 gRPC frame parsing for client streaming support
handler
Core GrpcHandler trait for language-agnostic gRPC request handling
service
Tonic service bridge
streaming
Streaming support utilities for gRPC

Structs§

GrpcConfig
Configuration for gRPC support
GrpcRegistry