grpcbridge

package
v0.45.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package grpcbridge provides a gRPC gateway that bridges a DDS participant to gRPC clients using JSON encoding.

The DDSBridge service exposes three RPCs:

  • Subscribe(SubscribeRequest) → stream(Sample): server-streaming, delivers DDS samples as they arrive.
  • Publish(PublishRequest) → PublishAck: unary publish of a single sample.
  • StreamPublish(stream PublishRequest) → PublishAck: client-streaming for high-throughput writes; returns the total count when the stream closes.

Messages are JSON-encoded over standard gRPC framing (Content-Type: application/grpc+json). Go clients set this via grpc.ForceCodec(grpcbridge.JSONCodec{}) on the dial options.

Usage:

p, _ := rtps.New(dds.Domain(0))
b := grpcbridge.New(p, grpcbridge.Options{})
lis, _ := net.Listen("tcp", ":9090")
b.Server().Serve(lis)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyConfig

func ApplyConfig(b *Bridge, cfg *Config) error

ApplyConfig pre-subscribes the topics listed in cfg on p. The Bridge opts are updated with the config's auth token when opts.AuthToken is empty.

func RegisterDDSBridgeServer

func RegisterDDSBridgeServer(s *grpc.Server, srv DDSBridgeServer)

RegisterDDSBridgeServer registers srv on s.

Types

type Bridge

type Bridge struct {
	// contains filtered or unexported fields
}

Bridge implements DDSBridgeServer and manages lazy subscribers/publishers.

func New

func New(p dds.Participant, opts Options) *Bridge

New returns a Bridge wrapping p. Call Server().Serve(lis) to start accepting gRPC connections.

func (*Bridge) Close

func (b *Bridge) Close() error

Close closes all open subscribers, publishers, and the gRPC server.

func (*Bridge) Publish

func (b *Bridge) Publish(ctx context.Context, req *PublishRequest) (*PublishAck, error)

Publish implements DDSBridgeServer.

func (*Bridge) Server

func (b *Bridge) Server() *grpc.Server

Server returns the underlying grpc.Server. Use it to call Serve(lis).

func (*Bridge) StreamPublish

func (b *Bridge) StreamPublish(stream grpc.ClientStreamingServer[PublishRequest, PublishAck]) error

StreamPublish implements DDSBridgeServer.

func (*Bridge) Subscribe

func (b *Bridge) Subscribe(req *SubscribeRequest, stream grpc.ServerStreamingServer[Sample]) error

Subscribe implements DDSBridgeServer.

type Config

type Config struct {
	// Listen is the gRPC listen address (e.g. ":9090"). Used by LoadAndApply.
	Listen string `yaml:"listen"`
	// AuthToken, if non-empty, requires every RPC to carry a matching Bearer token.
	AuthToken string `yaml:"auth_token"`
	// Topics lists topics to pre-subscribe when the bridge starts.
	Topics []TopicConfig `yaml:"topics"`
}

Config is the bridge configuration loaded from a YAML file.

Example:

listen: ":9090"
auth_token: "shared-secret"
topics:
  - name: "sensors/temperature"
    qos: "reliable"
  - name: "vehicle/speed"
    qos: "best_effort"

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads a YAML config file at path and returns the parsed Config.

type DDSBridgeClient

type DDSBridgeClient interface {
	Subscribe(ctx context.Context, req *SubscribeRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[Sample], error)
	Publish(ctx context.Context, req *PublishRequest, opts ...grpc.CallOption) (*PublishAck, error)
	StreamPublish(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStreamingClient[PublishRequest, PublishAck], error)
}

DDSBridgeClient is the client-side interface for DDSBridge.

func NewClient

func NewClient(ctx context.Context, addr string, extra ...grpc.DialOption) (DDSBridgeClient, *grpc.ClientConn, error)

NewClient returns a DDSBridgeClient that speaks to addr using JSON encoding. The returned conn must be closed by the caller when done.

func NewRawClient

func NewRawClient(cc grpc.ClientConnInterface) DDSBridgeClient

NewRawClient wraps an existing *grpc.ClientConn as a DDSBridgeClient. The caller is responsible for closing the connection.

type DDSBridgeServer

DDSBridgeServer is implemented by Bridge and registered with a grpc.Server.

type FilterFunc

type FilterFunc func(topic string, payload []byte) bool

FilterFunc decides whether to forward a sample. Return false to drop it.

type JSONCodec

type JSONCodec struct{}

JSONCodec encodes gRPC messages as JSON. Register it via encoding.RegisterCodec(JSONCodec{}) or grpc.ForceCodec(JSONCodec{}).

func (JSONCodec) Marshal

func (JSONCodec) Marshal(v interface{}) ([]byte, error)

func (JSONCodec) Name

func (JSONCodec) Name() string

func (JSONCodec) Unmarshal

func (JSONCodec) Unmarshal(data []byte, v interface{}) error

type Options

type Options struct {
	// AuthToken, if non-empty, requires every RPC to carry the metadata key
	//   authorization: Bearer <AuthToken>
	AuthToken string

	// QoS is applied to all subscribers and publishers created by the bridge.
	QoS dds.QoS

	// Filter, if non-nil, is called for each outbound sample (Subscribe path).
	// Return false to drop the sample.
	Filter FilterFunc

	// Transform, if non-nil, rewrites the payload of each outbound sample
	// (Subscribe path) before delivery to the gRPC client.
	Transform TransformFunc
}

Options configures a Bridge.

type PublishAck

type PublishAck struct {
	Count uint32 `json:"count"`
}

PublishAck is the response from Publish and StreamPublish RPCs.

type PublishRequest

type PublishRequest struct {
	Topic   string `json:"topic"`
	Payload []byte `json:"payload"`
}

PublishRequest is the request message for Publish and StreamPublish RPCs.

type Sample

type Sample struct {
	Topic          string `json:"topic"`
	Payload        []byte `json:"payload"`
	SequenceNumber uint64 `json:"seq_num"`
	TimestampNs    int64  `json:"timestamp_ns"`
	WriterGUID     []byte `json:"writer_guid"`
}

Sample is a DDS sample delivered by the Subscribe RPC.

type SubscribeRequest

type SubscribeRequest struct {
	Topic string `json:"topic"`
}

SubscribeRequest is the request message for the Subscribe RPC.

type TopicConfig

type TopicConfig struct {
	// Name is the DDS topic name (e.g. "sensors/temperature").
	Name string `yaml:"name"`
	// QoS is "reliable" or "best_effort". Defaults to DefaultQoS if unset.
	QoS string `yaml:"qos"`
}

TopicConfig configures a single topic in the bridge.

type TransformFunc

type TransformFunc func(topic string, payload []byte) ([]byte, error)

TransformFunc rewrites a sample payload before forwarding. Return the new payload or an error to drop the sample.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL