codec

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

README

Codec Best Practices

Guidelines for using and extending the x/codec module in production.

Goals

  • Efficient, safe protobuf serialization with 4‑byte big‑endian length framing.
  • Clear size limits to prevent memory abuse and DoS.
  • Simple extensibility via a registry while keeping a secure default.

Usage

  • Default codec: use NewRegistry().Default() (Protobuf, 10MB limit).
  • Streaming: prefer EncodeStream/DecodeStream for network I/O to avoid large intermediate buffers.
  • Reuse instances: codecs are goroutine‑safe and can be reused across handlers.

Size Limits

  • Set maxMessageSize to match transport limits (see x/transport configs). Keep it tight (10–50MB typical).
  • Always check encode/decode errors; never assume messages fit.
  • Reject empty or oversized frames before allocation.

Performance

  • Zero‑copy minded: internal pools reduce allocations; returned Encode slices are independent and safe to use.
  • Batch higher‑level messages when appropriate to amortize framing overhead.
  • Use streaming decode with a buffered reader when reading from sockets.

Safety & Robustness

  • Validate lengths first, then read payloads (Decode/DecodeStream already enforce this order).
  • Treat all input as untrusted: do not bypass size checks; avoid custom unmarshalling without bounds.
  • Propagate errors; don’t log protobuf payloads directly (may contain sensitive data).

Extensibility

  • Implement Codec (and optionally StreamCodec) for alternative formats.
  • Register with a unique name in a local Registry created via NewRegistry().
  • Keep wire‑compatibility stable; avoid changing framing for existing codecs.

Testing

  • Round‑trip tests: EncodeDecode on representative messages.
  • Stream tests: EncodeStream/DecodeStream through bytes.Buffer.
  • Boundary tests: empty messages, truncated frames, and maxMessageSize exceedance.
  • Concurrency sanity: basic parallel round‑trips to ensure safety.

Interop

  • Transport layer (x/transport/tcp) uses the same 4‑byte length framing; align maxMessageSize across layers.
  • Protobuf schema evolution rules apply: use optional/added fields carefully to maintain backward compatibility.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encode(msg proto.Message) ([]byte, error)
	Decode(data []byte, msg proto.Message) error
	MaxMessageSize() int
}

Codec defines the message encoding/decoding interface

type ProtobufCodec

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

ProtobufCodec implements length-prefixed protobuf encoding

func NewProtobufCodec

func NewProtobufCodec(maxMessageSize int) *ProtobufCodec

NewProtobufCodec creates a new protobuf codec

func (*ProtobufCodec) Decode

func (c *ProtobufCodec) Decode(data []byte, msg proto.Message) error

Decode unmarshals a length-prefixed message

func (*ProtobufCodec) DecodeStream

func (c *ProtobufCodec) DecodeStream(r io.Reader, msg proto.Message) error

DecodeStream reads a length-prefixed message from stream

func (*ProtobufCodec) Encode

func (c *ProtobufCodec) Encode(msg proto.Message) ([]byte, error)

Encode marshals a message with length prefix

func (*ProtobufCodec) EncodeStream

func (c *ProtobufCodec) EncodeStream(w io.Writer, msg proto.Message) error

EncodeStream writes a length-prefixed message to stream

func (*ProtobufCodec) MaxMessageSize

func (c *ProtobufCodec) MaxMessageSize() int

MaxMessageSize returns the maximum message size

type Registry

type Registry interface {
	Register(name string, codec Codec)
	Get(name string) (Codec, bool)
	Default() Codec
}

Registry manages multiple codec implementations

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new codec registry

type StreamCodec

type StreamCodec interface {
	Codec
	DecodeStream(r io.Reader, msg proto.Message) error
	EncodeStream(w io.Writer, msg proto.Message) error
}

StreamCodec extends Codec for streaming operations

Jump to

Keyboard shortcuts

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