Documentation
¶
Index ¶
- Constants
- Variables
- func GetBatch(ctx context.Context, js jetstream.JetStream, stream string, batch int, ...) (iter.Seq2[*jetstream.RawStreamMsg, error], error)
- func GetLastMsgsFor(ctx context.Context, js jetstream.JetStream, stream string, subjects []string, ...) (iter.Seq2[*jetstream.RawStreamMsg, error], error)
- type BatchAck
- type BatchFlowControl
- type BatchMsgOpt
- func WithBatchExpectLastSequence(seq uint64) BatchMsgOpt
- func WithBatchExpectLastSequenceForSubject(seq uint64, subject string) BatchMsgOpt
- func WithBatchExpectLastSequencePerSubject(seq uint64) BatchMsgOpt
- func WithBatchExpectStream(stream string) BatchMsgOpt
- func WithBatchMsgTTL(dur time.Duration) BatchMsgOpt
- type BatchPublisher
- type BatchPublisherOpt
- type GetBatchOpt
- type GetLastForOpt
- type PublishMsgBatchOpt
Constants ¶
const ( // Batch publish error codes JSErrCodeBatchPublishNotEnabled jetstream.ErrorCode = 10174 JSErrCodeBatchPublishMissingSeq jetstream.ErrorCode = 10175 JSErrCodeBatchPublishIncomplete jetstream.ErrorCode = 10176 JSErrCodeBatchPublishUnsupportedHeader jetstream.ErrorCode = 10177 JSErrCodeBatchPublishExceedsLimit jetstream.ErrorCode = 10199 )
const ( // BatchIDHeader contains the batch ID for a message in a batch publish. BatchIDHeader = "Nats-Batch-Id" // BatchSeqHeader contains the sequence number of a message within a batch. BatchSeqHeader = "Nats-Batch-Sequence" // BatchCommitHeader signals the final message in a batch when set to "1". BatchCommitHeader = "Nats-Batch-Commit" )
Variables ¶
var ( // ErrBatchPublishNotEnabled is returned when batch publish is not enabled on the stream. ErrBatchPublishNotEnabled jetstream.JetStreamError = &jsError{apiErr: &jetstream.APIError{ErrorCode: JSErrCodeBatchPublishNotEnabled, Description: "batch publish not enabled on stream", Code: 400}} // ErrBatchPublishIncomplete is returned when batch publish is incomplete and was abandoned. ErrBatchPublishIncomplete jetstream.JetStreamError = &jsError{apiErr: &jetstream.APIError{ErrorCode: JSErrCodeBatchPublishIncomplete, Description: "batch publish is incomplete and was abandoned", Code: 400}} // ErrBatchPublishMissingSeq is returned when batch publish sequence is missing. ErrBatchPublishMissingSeq jetstream.JetStreamError = &jsError{apiErr: &jetstream.APIError{ErrorCode: JSErrCodeBatchPublishMissingSeq, Description: "batch publish sequence is missing", Code: 400}} // ErrBatchPublishExceedsLimit is returned when batch publish sequence exceeds server limit (default 1000). ErrBatchPublishExceedsLimit jetstream.JetStreamError = &jsError{apiErr: &jetstream.APIError{ErrorCode: JSErrCodeBatchPublishExceedsLimit, Description: "batch publish sequence exceeds server limit (default 1000)", Code: 400}} // ErrBatchPublishUnsupportedHeader is returned when batch publish uses unsupported headers (Nats-Expected-Last-Msg-Id or Nats-Msg-Id). ErrBatchPublishUnsupportedHeader jetstream.JetStreamError = &jsError{apiErr: &jetstream.APIError{ErrorCode: JSErrCodeBatchPublishUnsupportedHeader, Description: "batch publish unsupported header used (Nats-Expected-Last-Msg-Id or Nats-Msg-Id)", Code: 400}} // ErrBatchClosed is returned when attempting to use a batch that has been closed. ErrBatchClosed = &jsError{message: "batch publisher closed"} // ErrInvalidBatchAck is returned when JetStream ack from batch publish is // invalid. ErrInvalidBatchAck jetstream.JetStreamError = &jsError{message: "invalid jetstream batch publish response"} )
var ( // ErrBatchUnsupported is returned when the server does not support batch // get (batch get is not supported by nats server >=2.11.0). ErrBatchUnsupported = errors.New("batch get not supported by server") // ErrInvalidResponse is returned when the response from the server is // invalid. ErrInvalidResponse = errors.New("invalid stream response") // ErrNoMessages is returned when there are no messages to fetch given the // provided options. ErrNoMessages = errors.New("no messages") // ErrInvalidOption is returned when an invalid option is provided. ErrInvalidOption = errors.New("invalid option") // ErrSubjectRequired is returned when no subjects are provided in GetLastMsgsFor. ErrSubjectRequired = errors.New("at least one subject is required") )
Functions ¶
func GetBatch ¶
func GetBatch(ctx context.Context, js jetstream.JetStream, stream string, batch int, opts ...GetBatchOpt) (iter.Seq2[*jetstream.RawStreamMsg, error], error)
GetBatch fetches a batch of messages from the specified stream. The batch size is determined by the `batch` parameter. The function returns an iterator that can be used to iterate over the messages. Any error received during iteration will terminate the loop. The iterator will return an error if there are no messages to fetch.
func GetLastMsgsFor ¶
func GetLastMsgsFor(ctx context.Context, js jetstream.JetStream, stream string, subjects []string, opts ...GetLastForOpt) (iter.Seq2[*jetstream.RawStreamMsg, error], error)
GetLastMsgsFor fetches the last messages for the specified subjects from the specified stream. The function returns an iterator that can be used to iterate over the messages. Any error received during iteration will terminate the loop. It can be configured to fetch messages up to a certain stream sequence number or time.
Types ¶
type BatchAck ¶ added in v0.2.0
type BatchAck struct {
// Stream is the stream name the message was published to.
Stream string `json:"stream"`
// Sequence is the stream sequence number of the message.
Sequence uint64 `json:"seq"`
// Domain is the domain the message was published to.
Domain string `json:"domain,omitempty"`
// Value is the counter value for the stream.
// This is only set when publishing to a stream with [StreamConfig.AllowMsgCounter] enabled.
Value string `json:"val,omitempty"`
// BatchID is the unique identifier for the batch.
BatchID string `json:"batch_id,omitempty"`
// BatchSize is the number of messages in the batch.
BatchSize int `json:"batch_size,omitempty"`
}
BatchAck is the acknowledgment for a batch publish operation.
type BatchFlowControl ¶ added in v0.2.0
type BatchFlowControl struct {
// AckFirst waits for an ack on the first message in the batch.
// Default: true
AckFirst bool
// AckEvery waits for an ack every N messages (0 = disabled).
// Default: 0
AckEvery int
// AckTimeout is the timeout for waiting for acks when flow control is enabled.
// Default: timeout from JetStream context.
AckTimeout time.Duration
}
BatchFlowControl configures flow control for batch publishing.
type BatchMsgOpt ¶ added in v0.2.0
type BatchMsgOpt func(*batchMsgOpts) error
BatchMsgOpt is an option for configuring batch message publishing.
func WithBatchExpectLastSequence ¶ added in v0.2.0
func WithBatchExpectLastSequence(seq uint64) BatchMsgOpt
WithBatchExpectLastSequence sets the expected sequence number the last message on a stream should have. If the last message has a different sequence number server will reject the message and publish will fail.
func WithBatchExpectLastSequenceForSubject ¶ added in v0.2.0
func WithBatchExpectLastSequenceForSubject(seq uint64, subject string) BatchMsgOpt
WithBatchExpectLastSequenceForSubject sets the sequence and subject for which the last sequence number should be checked. If the last message on a subject has a different sequence number server will reject the message and publish will fail.
func WithBatchExpectLastSequencePerSubject ¶ added in v0.2.0
func WithBatchExpectLastSequencePerSubject(seq uint64) BatchMsgOpt
WithBatchExpectLastSequencePerSubject sets the expected sequence number the last message on a subject the message is published to. If the last message on a subject has a different sequence number server will reject the message and publish will fail.
func WithBatchExpectStream ¶ added in v0.2.0
func WithBatchExpectStream(stream string) BatchMsgOpt
WithBatchExpectStream sets the expected stream the message should be published to. If the message is published to a different stream server will reject the message and publish will fail.
func WithBatchMsgTTL ¶ added in v0.2.0
func WithBatchMsgTTL(dur time.Duration) BatchMsgOpt
WithBatchMsgTTL sets per msg TTL for batch messages. Requires [StreamConfig.AllowMsgTTL] to be enabled.
type BatchPublisher ¶ added in v0.2.0
type BatchPublisher interface {
// Add publishes a message to the batch with the given subject and data.
// It is an IO operation and the message will be published immediately
// and persisted upon commit.
Add(subject string, data []byte, opts ...BatchMsgOpt) error
// AddMsg publishes a message to the batch.
AddMsg(msg *nats.Msg, opts ...BatchMsgOpt) error
// Commit publishes the final message with the given subject and data,
// and commits the batch. Returns a BatchAck containing the acknowledgment
// from the server.
Commit(ctx context.Context, subject string, data []byte, opts ...BatchMsgOpt) (*BatchAck, error)
// CommitMsg publishes the final message and commits the batch.
// Returns a BatchAck containing the acknowledgment from the server.
CommitMsg(ctx context.Context, msg *nats.Msg, opts ...BatchMsgOpt) (*BatchAck, error)
// Discard cancels the batch without committing.
// The server will abandon the batch after a timeout.
Discard() error
// Size returns the number of messages added to the batch so far.
Size() int
// IsClosed returns true if the batch has been committed or discarded.
IsClosed() bool
}
BatchPublisher provides methods for publishing messages to a stream in batches. Messages are published immediately with batch headers, and the batch is committed with the final message which includes a commit header.
func NewBatchPublisher ¶ added in v0.2.0
func NewBatchPublisher(js jetstream.JetStream, opts ...BatchPublisherOpt) (BatchPublisher, error)
NewBatchPublisher creates a new batch publisher for publishing messages in batches.
type BatchPublisherOpt ¶ added in v0.2.0
type BatchPublisherOpt interface {
// contains filtered or unexported methods
}
BatchPublisherOpt is a functional option for configuring a BatchPublisher.
type GetBatchOpt ¶
type GetBatchOpt func(*getBatchOpts) error
GetBatchOpt is a function that can be used to configure the behavior of the GetBatch function.
func GetBatchMaxBytes ¶
func GetBatchMaxBytes(maxBytes int) GetBatchOpt
GetBatchMaxBytes sets the maximum number of bytes to fetch. The server will try to fetch messages until the maximum number of bytes is reached (or the batch size is reached).
func GetBatchSeq ¶
func GetBatchSeq(seq uint64) GetBatchOpt
GetBatchSeq sets the sequence number from which to start fetching messages.
func GetBatchStartTime ¶
func GetBatchStartTime(startTime time.Time) GetBatchOpt
GetBatchStartTime sets the start time from which to fetch messages.
func GetBatchSubject ¶
func GetBatchSubject(subj string) GetBatchOpt
GetBatchSubject sets the subject from which to start fetching messages. It may include wildcards.
type GetLastForOpt ¶
type GetLastForOpt func(*getLastBatchOpts) error
GetLastForOpt is a function that can be used to configure the behavior of the GetLastMsgsFor function.
func GetLastMsgsBatchSize ¶
func GetLastMsgsBatchSize(batch int) GetLastForOpt
GetLastMsgsBatchSize sets the optional batch size for fetching messages from multiple subjects.
func GetLastMsgsUpToSeq ¶
func GetLastMsgsUpToSeq(seq uint64) GetLastForOpt
GetLastMsgsUpToSeq sets the sequence number up to which to fetch messages (inclusive).
func GetLastMsgsUpToTime ¶
func GetLastMsgsUpToTime(tm time.Time) GetLastForOpt
GetLastMsgsUpToTime sets the time up to which to fetch messages.
type PublishMsgBatchOpt ¶ added in v0.2.0
type PublishMsgBatchOpt interface {
// contains filtered or unexported methods
}
PublishMsgBatchOpt is a functional option for configuring PublishMsgBatch.