Help us shape the future of Go! Take the 2026 Go Developer Survey

transport

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package transport runs one async operation against a message server: it connects, subscribes, sends, collects the messages it was asked to wait for, and disconnects. Each protocol is a subpackage that implements Transport; transport/all wires them together.

Connections are one-shot unless the transport also implements Opener: then a flow can keep a Session open across steps (WebSocket, MQTT 5, core NATS).

Index

Constants

View Source
const (
	DefaultCount   = 1
	DefaultTimeout = 30 * time.Second
)

Defaults for a receive with no count or timeout.

View Source
const DefaultInboxBytes = 64 << 20

DefaultInboxBytes is how much unread message data a session holds.

Variables

View Source
var (
	// ErrTimeout means the receive timed out before enough messages arrived.
	ErrTimeout = errors.New("receive timed out")
	// ErrTooLarge means the messages kept exceeded Receive.MaxBytes.
	ErrTooLarge = errors.New("received messages exceed the size limit")
)
View Source
var ErrInboxFull = errors.New("a connection kept open held more unread messages than the limit")

ErrInboxFull means messages arrived faster than any step collected them and the session stopped keeping them.

Functions

func IsText

func IsText(b []byte) bool

IsText reports whether a body should be sent as a text frame.

func JoinURL

func JoinURL(server, channel string) string

JoinURL puts a channel after a server URL: "wss://h" and "events" give "wss://h/events"; an empty channel leaves the server alone.

Types

type Header struct{ Name, Value string }

Header is one message header (or connection header), kept in order.

type Inbox

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

Inbox holds the messages a session's reader goroutine receives until a step asks for them. Push and Fail are safe to call from that goroutine while another one waits.

func NewInbox

func NewInbox(limit int64) *Inbox

NewInbox returns an inbox that holds up to limit bytes of unread bodies (DefaultInboxBytes when limit is not positive).

func (*Inbox) Fail

func (b *Inbox) Fail(err error)

Fail ends the inbox: a Wait returns the messages held and then err. The first failure counts.

func (*Inbox) Push

func (b *Inbox) Push(m Message)

Push adds a received message. Past the limit the message is dropped and the inbox fails with ErrInboxFull, after the messages already held.

func (*Inbox) Wait

func (b *Inbox) Wait(ctx context.Context, r Receive) ([]Message, error)

Wait collects messages as r says, taking them out of the inbox.

type Message

type Message struct {
	Direction string // "sent" or "received"
	At        time.Time
	Headers   []Header
	Body      []byte
	// Channel, on a message sent over a Session, is where to send it; empty
	// means the channel the connection was opened on.
	Channel   string
	Key       string // Kafka key
	Partition int32  // Kafka partition
	Offset    int64  // Kafka offset
	Sequence  uint64 // JetStream stream sequence
}

Message is one message, sent or received. Fields a protocol has no word for stay zero.

func Wait

func Wait(ctx context.Context, r Receive, in <-chan Message, fail <-chan error) ([]Message, error)

Wait collects messages from in until the receive is satisfied, the timeout passes, fail reports an error, or ctx ends. The result is the messages kept; with ErrTimeout or ErrTooLarge it is the partial list. in must be fed by a goroutine the caller stops after Wait returns.

type Opener

type Opener interface {
	Open(ctx context.Context, op Operation) (Session, error)
}

Opener is implemented by a Transport that can keep a connection open. The session subscribes at once, whether or not the opening step receives.

type Operation

type Operation struct {
	Server  string
	Channel string
	Send    *Message
	Receive *Receive
	// Headers are connection headers (the WebSocket handshake, the SSE
	// request); message headers are Send.Headers.
	Headers []Header
	// Binding holds the protocol's options by name.
	Binding map[string]string
	TLS     *tls.Config
	Clock   func() time.Time // nil → time.Now
}

Operation is one run: where to go and what to do there.

func (Operation) HeaderValue

func (op Operation) HeaderValue(name string) string

HeaderValue returns the value of the first connection header called name (case-insensitive).

func (Operation) Now

func (op Operation) Now() time.Time

Now is the operation's clock.

type Receive

type Receive struct {
	Count   int
	Timeout time.Duration
	// Match, when set, decides whether a message counts; a message that does
	// not match is dropped. An error stops the receive.
	Match func(Message) (bool, error)
	// MaxBytes, when positive, stops the receive once the bodies it kept add
	// up to more than this.
	MaxBytes int64
	// OnMessage, when set, is called with each message kept, as it is kept
	// (so a caller can show progress while Count is still being waited for).
	// It runs on the goroutine that called Wait and must return quickly.
	OnMessage func(Message)
	// OnKept, when set, is called after OnMessage with each message kept. An
	// error stops the receive: Wait returns the messages kept so far, this one
	// included, and the error. It is how a hook can stop a run at the first
	// message that fails its check. It runs on the goroutine that called Wait.
	OnKept func(Message) error
}

Receive says what to wait for. Zero Count means 1, zero Timeout 30s.

func (Receive) Normalized

func (r Receive) Normalized() Receive

Normalized returns r with its defaults filled in.

type Registry

type Registry map[string]Transport

Registry looks transports up by protocol name.

func (Registry) Lookup

func (r Registry) Lookup(protocol string) (Transport, bool)

Lookup returns the transport for a protocol.

type Result

type Result struct {
	Sent     *Message
	Received []Message
	Connect  time.Duration
	Total    time.Duration
}

Result is what a run did. It is also returned with ErrTimeout and ErrTooLarge, holding what was collected before the receive stopped.

type Session

type Session interface {
	// Send puts one message on the connection and returns it as sent.
	Send(ctx context.Context, m Message) (Message, error)
	// Receive collects messages as Receive says, from those already held and
	// those still to come. A timeout returns what was kept with ErrTimeout and
	// leaves the connection open.
	Receive(ctx context.Context, r Receive) ([]Message, error)
	// Close ends the connection and every goroutine it started. Closing twice
	// is harmless.
	Close() error
}

Session is a connection kept open across several steps of a flow: one step opens it, later steps send over it and collect what arrived, and it closes when the run ends. Messages that arrive between steps are held, so a reply that comes before the step that waits for it is not lost.

A session serves one step at a time: Send and Receive wait for each other.

type Transport

type Transport interface {
	Run(ctx context.Context, op Operation) (Result, error)
}

Transport runs operations for one protocol. Run must return promptly when ctx ends and leave no goroutine behind.

Directories

Path Synopsis
Package all wires every protocol into one registry.
Package all wires every protocol into one registry.
Package amqp is the AMQP 0-9-1 transport (RabbitMQ and compatible brokers).
Package amqp is the AMQP 0-9-1 transport (RabbitMQ and compatible brokers).
Package amqp10 is the AMQP 1.0 transport: ActiveMQ Artemis and Classic, Apache Qpid, IBM MQ, RabbitMQ 4 and other brokers that speak the ISO standard protocol.
Package amqp10 is the AMQP 1.0 transport: ActiveMQ Artemis and Classic, Apache Qpid, IBM MQ, RabbitMQ 4 and other brokers that speak the ISO standard protocol.
internal
awscfg
Package awscfg builds the AWS configuration the SQS and SNS transports share: the provider's default credential lookup, or the explicit credentials of the request.
Package awscfg builds the AWS configuration the SQS and SNS transports share: the provider's default credential lookup, or the explicit credentials of the request.
pull
Package pull is the shared shape of the queue-like transports (Pub/Sub, SQS, Service Bus): a goroutine pulls messages and holds each one's handle, the receive keeps some of them, and when the run ends the kept handles are acknowledged and every other handle is given back to the service.
Package pull is the shared shape of the queue-like transports (Pub/Sub, SQS, Service Bus): a goroutine pulls messages and holds each one's handle, the receive keeps some of them, and when the run ends the kept handles are acknowledged and every other handle is given back to the service.
Package kafka is the Kafka transport.
Package kafka is the Kafka transport.
Package mqtt is the MQTT transport: MQTT 5 by default, and MQTT 3.1.1 or 3.1 when the binding version asks for it (for brokers and devices that speak only those).
Package mqtt is the MQTT transport: MQTT 5 by default, and MQTT 3.1.1 or 3.1 when the binding version asks for it (for brokers and devices that speak only those).
Package nats is the NATS transport: core publish and subscribe, and JetStream when the binding names a stream.
Package nats is the NATS transport: core publish and subscribe, and JetStream when the binding names a stream.
Package pubsub is the Google Cloud Pub/Sub transport: publish to a topic, pull from a subscription.
Package pubsub is the Google Cloud Pub/Sub transport: publish to a topic, pull from a subscription.
Package redis is the Redis transport.
Package redis is the Redis transport.
Package servicebus is the Azure Service Bus transport: send to a queue or topic, receive from a queue or a topic subscription.
Package servicebus is the Azure Service Bus transport: send to a queue or topic, receive from a queue or a topic subscription.
Package sns is the AWS SNS transport.
Package sns is the AWS SNS transport.
Package sqs is the AWS SQS transport: send to a queue, long-poll a queue.
Package sqs is the AWS SQS transport: send to a queue, long-poll a queue.
Package sse is the Server-Sent Events transport.
Package sse is the Server-Sent Events transport.
Package ws is the WebSocket transport.
Package ws is the WebSocket transport.

Jump to

Keyboard shortcuts

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