Documentation
¶
Overview ¶
Package protobuf provides support for Protocol Buffer message serialization and routing for the RabbitMQ library. It includes automatic marshaling/unmarshaling of protobuf messages and type-safe message routing capabilities.
Index ¶
- Constants
- func GetProtobufMessageType(delivery *Delivery) (string, bool)
- func IsProtobufMessage(delivery *Delivery) bool
- func RegisterHandler[T proto.Message](mux *MessageMux, handlerFunc func(ctx context.Context, msg T) error)
- type BasicMessage
- type Delivery
- type Message
- type MessageMux
- func (m *MessageMux) GetRegisteredTypes() []string
- func (m *MessageMux) Handle(ctx context.Context, delivery *Delivery) error
- func (m *MessageMux) SetDefaultHandler(handlerFunc func(ctx context.Context, delivery *Delivery) error)
- func (m *MessageMux) SetUnknownProtobufHandler(handlerFunc func(ctx context.Context, messageType string, data []byte) error)
- type ProtobufHandler
- type Serializer
Constants ¶
const ContentTypeProtobuf = "application/protobuf"
Protobuf content type constant
Variables ¶
This section is empty.
Functions ¶
func GetProtobufMessageType ¶
GetProtobufMessageType extracts the protobuf message type from a delivery
func IsProtobufMessage ¶
IsProtobufMessage checks if a delivery contains a protobuf message
func RegisterHandler ¶
func RegisterHandler[T proto.Message](mux *MessageMux, handlerFunc func(ctx context.Context, msg T) error)
RegisterHandler registers a type-safe handler for a specific protobuf message type The handler function will receive the fully unmarshaled protobuf message Usage: RegisterHandler[*pb.UserCreated](mux, func(ctx context.Context, msg *pb.UserCreated) error {...})
Types ¶
type BasicMessage ¶
BasicMessage provides a basic implementation of the Message interface
func NewMessage ¶
func NewMessage(body []byte) *BasicMessage
NewMessage creates a new basic message with the given body
func (*BasicMessage) WithContentType ¶
func (m *BasicMessage) WithContentType(contentType string) Message
WithContentType sets the content type for the message
func (*BasicMessage) WithHeader ¶
func (m *BasicMessage) WithHeader(key string, value any) Message
WithHeader adds a header to the message
type Delivery ¶
Delivery represents a RabbitMQ delivery (minimal interface for the protobuf package)
type Message ¶
type Message interface {
WithContentType(contentType string) Message
WithHeader(key string, value any) Message
}
Message represents a RabbitMQ message (minimal interface for the protobuf package)
func NewProtobufMessage ¶
NewProtobufMessage creates a new Message from a Protocol Buffers message This function automatically: 1. Serializes the protobuf message using proto.Marshal() 2. Sets the content type to "application/protobuf" 3. Sets the "x-message-type" header with the fully qualified message type name
type MessageMux ¶
type MessageMux struct {
// contains filtered or unexported fields
}
MessageMux provides automatic routing of protobuf messages to type-safe handlers
func NewMessageMux ¶
func NewMessageMux() *MessageMux
NewMessageMux creates a new message multiplexer for routing protobuf messages
func (*MessageMux) GetRegisteredTypes ¶
func (m *MessageMux) GetRegisteredTypes() []string
GetRegisteredTypes returns a list of all registered protobuf message types
func (*MessageMux) Handle ¶
func (m *MessageMux) Handle(ctx context.Context, delivery *Delivery) error
Handle is the main routing method that implements the MessageHandler interface It automatically routes messages based on their type and content
func (*MessageMux) SetDefaultHandler ¶
func (m *MessageMux) SetDefaultHandler(handlerFunc func(ctx context.Context, delivery *Delivery) error)
SetDefaultHandler sets a fallback handler for non-protobuf messages or messages without registered handlers. This handler receives the raw delivery.
func (*MessageMux) SetUnknownProtobufHandler ¶
func (m *MessageMux) SetUnknownProtobufHandler(handlerFunc func(ctx context.Context, messageType string, data []byte) error)
SetUnknownProtobufHandler sets a handler for protobuf messages that don't have a registered handler. This handler receives the message type name and raw data.
type ProtobufHandler ¶
type ProtobufHandler struct {
// contains filtered or unexported fields
}
ProtobufHandler represents a type-safe handler for a specific protobuf message type
type Serializer ¶
type Serializer struct{}
Serializer implements the rabbitmq.MessageSerializer interface for Protocol Buffers
func NewSerializer ¶
func NewSerializer() *Serializer
NewSerializer creates a new protobuf serializer
func (*Serializer) ContentType ¶
func (s *Serializer) ContentType() string
ContentType returns the content type for protobuf messages
func (*Serializer) Deserialize ¶
func (s *Serializer) Deserialize(data []byte, target any) error
Deserialize unmarshals bytes to a protobuf message