msg

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 1 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package msg defines Msg, the multipart ZMTP message exchanged by every gozmq socket. The root package re-exports it as github.com/exergy-dev/gozmq.Msg, so applications normally reach these methods through that alias and never import this package directly. It exists as its own package so the method set is documented in one place and so the internal engine layers can reference it without an import cycle through the root package.

Frame ownership

By default frames are ordinary Go memory. A *Msg returned by Socket.RecvMsg owns freshly allocated frames that belong to the caller: they may be kept, sliced or re-sent for as long as the caller likes, and the garbage collector reclaims them. For such a plain message Msg.Release and Msg.Retain do nothing.

Lent frames

A message can instead carry lent frames: memory that must go back to its owner once nobody uses it. NewWithRelease builds one, and a socket created with gozmq.WithRecvBufferPool(true) receives its large frames into pooled buffers this way. Msg.Lent tells the two kinds apart. The contract for a lent message is reference counting with explicit calls:

  • Whoever creates or receives a lent message holds one reference to it. Msg.Retain adds a reference; Msg.Release drops one.
  • Its frames stay valid until the last reference is released — the message's own, plus one held by every message [Msg.Derive]d from it. After that the memory is handed back (for the pool, reused by later receives), so neither the message nor any frame slice taken from it may be used again.
  • Releasing more references than were held panics; it never hands the memory back early, because each message gives its hold on the frames up exactly once.
  • There is no finalizer. A lent message that is dropped without Release is simply collected by the garbage collector: safe, just not recycled.
  • Socket.SendMsg takes its own reference for the send, released once the message is written (or, for an inproc peer, by the receiver's Release), so sending a message and then releasing it is always correct, whatever the order relative to the actual write.
  • Wrapping lent frames in a new message with FromBytes or FromFrames drops the connection to their owner; use Msg.Derive instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Msg

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

Msg is a ZMTP message: an ordered sequence of binary frames. The zero value is an empty message.

Frames are ordinary Go memory owned by whoever holds them unless the message is Msg.Lent; see the package doc. Frames passed in (FromBytes, AppendFrame) are retained by reference, so the caller must not mutate them while the message is in flight.

func FromBytes

func FromBytes(b []byte) *Msg

FromBytes returns a single-frame Msg wrapping b without copying.

func FromFrames

func FromFrames(frames ...[]byte) *Msg

FromFrames returns a Msg holding the provided frames, in order. The frame list is copied, so the caller may reuse the slice passed in; the frames themselves are retained by reference.

func New

func New() *Msg

New returns an empty Msg.

func NewWithRelease

func NewWithRelease(release func(), frames ...[]byte) *Msg

NewWithRelease returns a lent Msg holding frames (the list is copied as in FromFrames) and one reference to them. release is called exactly once, after the last reference is released: this message's own and those of every message derived from it (see Msg.Derive), including the ones sockets take while sending it. Until then the frames must stay untouched. release may run on any goroutine, including a socket's internal writer, so it must not block. A nil release makes a plain message, like FromFrames.

func (*Msg) AppendFrame

func (m *Msg) AppendFrame(b []byte)

AppendFrame appends a frame to the message. The frame is retained by reference — callers must not mutate b after calling.

func (*Msg) Bytes

func (m *Msg) Bytes() []byte

Bytes returns the first frame for single-frame messages. Panics if the message has anything other than one frame; use SingleFrame for a safe single-frame accessor or Frames for multipart.

func (*Msg) Derive

func (m *Msg) Derive(frames ...[]byte) *Msg

Derive returns a new message holding frames — typically some of m's frames, possibly with others added, as when stripping or adding a routing envelope. If m is lent the new message is too and keeps m's lent frames valid until it is itself released, independently of m. m is not modified and keeps its own reference, so the caller still releases it as usual. For a plain m, Derive is FromFrames.

Deriving from a lent message the caller no longer holds a reference to panics.

func (*Msg) Frames

func (m *Msg) Frames() [][]byte

Frames returns the backing frame slice.

func (*Msg) Lent

func (m *Msg) Lent() bool

Lent reports whether m's frames are lent memory that Release gives back (see the package doc): true for NewWithRelease messages, messages derived from them, and messages whose frames a socket received into pooled buffers. For every other message Release and Retain do nothing.

func (*Msg) NumFrames

func (m *Msg) NumFrames() int

NumFrames reports the number of frames in the message.

func (*Msg) Release

func (m *Msg) Release()

Release drops one reference to a lent message. When m's last reference goes, m gives up its hold on the frames, and once no message holds them they are handed back to their owner: neither m nor its frames may be used after the caller's final Release. Releasing more references than were held panics (without handing the frames back early). For a plain message Release does nothing, so calling it unconditionally on every received message is always correct.

func (*Msg) Retain

func (m *Msg) Retain()

Retain adds a reference to a lent message, to be dropped by a matching Release; use it to hand the same *Msg to another goroutine that releases it independently. It panics if m has already been fully released. For a plain message it does nothing.

func (*Msg) SingleFrame

func (m *Msg) SingleFrame() ([]byte, bool)

SingleFrame returns the only frame in m. The boolean is false when m is empty or multipart.

func (*Msg) TotalSize

func (m *Msg) TotalSize() int

TotalSize returns the sum of all frame lengths in bytes.

Jump to

Keyboard shortcuts

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