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 ¶
const ( DefaultCount = 1 DefaultTimeout = 30 * time.Second )
Defaults for a receive with no count or timeout.
const DefaultInboxBytes = 64 << 20
DefaultInboxBytes is how much unread message data a session holds.
Variables ¶
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") )
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 ¶
Types ¶
type Header ¶
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 ¶
NewInbox returns an inbox that holds up to limit bytes of unread bodies (DefaultInboxBytes when limit is not positive).
func (*Inbox) Fail ¶
Fail ends the inbox: a Wait returns the messages held and then err. The first failure counts.
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.
type Opener ¶
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 ¶
HeaderValue returns the value of the first connection header called name (case-insensitive).
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 ¶
Normalized returns r with its defaults filled in.
type Result ¶
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.
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. |