Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateTopicName ¶
ValidateTopicName ensures a topic name is valid. Topic names must be non-empty, at most 255 characters, and contain only lowercase letters, numbers, underscores, and hyphens.
Types ¶
type Consumer ¶
type Consumer interface {
// Register adds a controller to the consumer. Must be called before Start().
Register(controller Controller) error
// Start subscribes to all registered controllers' topics and begins consuming messages.
// ctx governs only the synchronous subscribe calls; consume loops run independently
// and must be terminated by calling Stop().
// Start() will only be called once at the application startup, so it does not need to be idempotent.
Start(ctx context.Context) error
// Stop gracefully shuts down all controllers with the specified timeout.
// timeoutMs is the maximum time in milliseconds to wait for graceful shutdown.
// Returns error if shutdown times out.
// Stop() will only be called once at the application shutdown, so it does not need to be idempotent.
Stop(timeoutMs int64) error
}
Consumer orchestrates multiple queue consumers. It handles subscription lifecycle, message consumption, ack/nack, and graceful shutdown for the entire pipeline. Start(), Register() and Stop() are always called in this order so they do not need to be concurrently-safe between one another, but the implementation must be thread-safe between message processing and Register()/Stop() operations.
func New ¶
func New(logger *zap.SugaredLogger, scope tally.Scope, registry TopicRegistry, processor errs.ErrorProcessor, gate consumergate.Gate) Consumer
New creates a new consumer.
registry provides queue and subscription config for topics. processor is the error-classification policy applied exactly once per failing controller return — typically errs.NewClassifierProcessor(...) for primary pipeline consumers (per-node classifier walk that preserves controller-attached framework wraps), or errs.AlwaysRetryableProcessor for narrowly-scoped consumers such as DLQ reconciliation that must redeliver on any failure. scope is used as provided so wiring can distinguish primary and DLQ consumers without introducing duplicate consumer sub-scopes. processor must not be nil; callers that genuinely want no transformation can pass errs.NewClassifierProcessor() with no classifiers.
gate is the consumer-gate implementation consulted before each delivery reaches its controller. Pass noop.New() (from platform/extension/consumergate/noop) for services that do not need runtime gating. gate must not be nil.
type Controller ¶
type Controller interface {
Process(ctx context.Context, delivery Delivery) error
// Name returns the controller name for logging and metrics.
Name() string
// TopicKey returns the topic key this controller subscribes to.
TopicKey() TopicKey
// ConsumerGroup returns the consumer group for offset tracking.
// Multiple controllers can share a consumer group to load-balance across workers.
// Different consumer groups consume independently.
ConsumerGroup() string
}
Controller processes queue deliveries. Controllers contain business logic and are registered with the Consumer. The Controller interface enables clean separation of concerns: - Controller focuses on business logic (deserialize, process, return error status) - Consumer handles infrastructure (subscription, ack/nack, metrics, lifecycle) Controllers may emit domain event counters, but must not duplicate the consumer-owned Process lifecycle metrics. The implementation of the controller should be idempotent and stateless. The controller is expected to be retried for the same message multiple times and should process side effects gracefully. The implementation must be thread-safe.
type Delivery ¶
type Delivery interface {
// Message returns the delivered message.
Message() entityqueue.Message
// ExtendVisibilityTimeout extends the time before this message becomes
// visible to other consumers. Use when processing takes longer than expected.
ExtendVisibilityTimeout(ctx context.Context, durationMillis int64) error
// Hold records intent to postpone this delivery: when Process then returns
// nil, the framework postpones the message for delayMs instead of acking.
// The postponed message is a barrier — its partition is not consumed past
// it until it redelivers, in order — and the redelivery does not count
// toward the retry limit. Recording has no side effects; the last call
// wins; a negative delay is clamped to 0. If Process returns an error, the
// failure outcome wins and the recorded hold is discarded. Must be called
// from the Process goroutine before returning.
Hold(delayMs int64)
// DeliveryID returns a backend-specific identifier for this delivery.
DeliveryID() string
// Attempt returns how many times this message has been delivered.
// Starts at 1 for first delivery. A postponed redelivery restarts at 1.
Attempt() int
// ReceivedAt returns when this delivery was received (Unix milliseconds).
ReceivedAt() int64
// Metadata returns backend-specific delivery metadata.
Metadata() map[string]string
}
Delivery is the consumer package's view of a queue delivery. It exists to hide Ack/Nack from controllers — the Consumer framework handles those automatically based on the error returned from Process(). Controllers only see message data, metadata, ExtendVisibilityTimeout (a business-level concern for long-running processing), and Hold (a business-level concern for backing off).
To signal outcome from Process():
- Return nil to ack the message (success).
- Return an error to nack the message for retry.
- Return a non-retryable error to reject a poison pill message (removes it from the queue).
- Call Hold(delayMs) and return nil to postpone the message (redeliver later, partition waits).
type TopicConfig ¶
type TopicConfig struct {
// Key is the fixed pipeline stage identifier.
Key TopicKey
// Name is the actual queue topic name (e.g. "request", "my-custom-request").
Name string
// Queue is the queue backend for this topic.
Queue extqueue.Queue
// Subscription is the subscription configuration for this topic.
// Leave at zero value for publish-only topics.
Subscription extqueue.SubscriptionConfig
}
TopicConfig combines all configuration for a single pipeline topic: the fixed key, the actual queue topic name, the queue backend, and (optionally) subscription settings.
type TopicKey ¶
type TopicKey string
TopicKey identifies a pipeline stage. It is a fixed key used to look up queue backends, topic names, and subscription configs in the TopicRegistry. The actual queue topic name is provided separately via TopicConfig.Name so that library consumers can choose their own naming conventions.
type TopicRegistry ¶
type TopicRegistry struct {
// contains filtered or unexported fields
}
TopicRegistry provides queue, topic name, and subscription config for topics. Each topic can have a different queue backend and topic name.
func NewTopicRegistry ¶
func NewTopicRegistry(configs []TopicConfig) (TopicRegistry, error)
NewTopicRegistry creates a new TopicRegistry from a list of TopicConfigs. Returns an error if any topic name is invalid, or if two configs share a topic key — a duplicate key would silently shadow the earlier entry (last write wins on the key→queue/name maps), routing publishes and subscriptions registered against one topic onto another.
func (TopicRegistry) Queue ¶
func (r TopicRegistry) Queue(key TopicKey) (extqueue.Queue, bool)
Queue returns the queue backend for the given topic key. Returns ok=false if no queue is registered for this key.
func (TopicRegistry) SubscriptionConfig ¶
func (r TopicRegistry) SubscriptionConfig(key TopicKey, consumerGroup string) (extqueue.SubscriptionConfig, bool)
SubscriptionConfig returns the subscription configuration for the given topic key and consumer group. Returns ok=false if no configuration is registered.