Documentation
¶
Overview ¶
Package jsqueue runs JetStream work queues whose handlers can take longer than the consumer's AckWait.
TFBuddy's hook handlers plan every workspace of a merge request before they return, which can take minutes. Two things used to turn that into repeated plans:
- A message whose handler outlived AckWait was redelivered to another replica, which planned the whole merge request again.
- The old consumers were push consumers. JetStream pushed every waiting message to the replicas at once, and a message's AckWait clock ran while it sat in a busy replica's buffer, so a burst of merge requests timed out and was redelivered before anyone had started on it.
Consume pulls one message at a time through nats.go's jetstream package and pulls the next only once the handler has returned, so waiting messages stay in the stream with no clock running. What the library does not do is keep a long-running message in progress, or turn the handler's result into an ack, a delayed retry or a drop; that is the code here.
Index ¶
Constants ¶
const ( // DefaultAckWait is how long JetStream waits for an ack or a progress // signal before it redelivers. It only bounds how long a crashed worker // holds a message: a live handler extends it every heartbeat. DefaultAckWait = time.Minute // DefaultMaxDeliver caps the deliveries of one message, the first included. DefaultMaxDeliver = 5 // DefaultNakDelay is how long JetStream waits before it redelivers a // message whose handler returned an error. DefaultNakDelay = 30 * time.Second // DefaultMaxInProgress is how long a handler may keep a message before the // heartbeat stops, so a hung handler cannot hold a message forever. DefaultMaxInProgress = 30 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
func Consume ¶
func Consume[T any, I Event[T]](js jetstream.JetStream, q Queue, opts Options, fn Handler[T]) (jetstream.ConsumeContext, error)
Consume handles q's messages with fn, one at a time: the next message is pulled only once fn has returned, so several replicas calling Consume share the queue and each takes the next message when it is free. Stop the returned ConsumeContext to end it.
Types ¶
type Event ¶
Event is a message type that can be decoded from a JetStream payload. The gongs message types used for publishing satisfy it.
type Handler ¶
Handler processes one decoded message. A nil return acks the message. An error wrapping utils.ErrPermanent terminates it. Any other error asks JetStream to redeliver it after Options.NakDelay, up to Options.MaxDeliver deliveries in all.
type Options ¶
type Options struct {
AckWait time.Duration
MaxDeliver int
NakDelay time.Duration
MaxInProgress time.Duration
// Heartbeat is how often a running handler extends the ack deadline.
// It defaults to a third of AckWait.
Heartbeat time.Duration
}
Options tune a queue. Zero values take the defaults above.
type Queue ¶
type Queue struct {
Stream string
Subject string
// Durable names the pull consumer.
Durable string
// LegacyPushDurable names the push consumer this queue replaces. A push
// consumer with this name is deleted before the pull consumer is created,
// because a work-queue stream allows one consumer per subject. Messages it
// had not acked stay in the stream and go to the pull consumer.
LegacyPushDurable string
}
Queue names a durable pull consumer on a stream.