Documentation
¶
Overview ¶
Package rabbitmq adapts the backend-neutral go-queue worker contract to the RabbitMQ-native policy implemented by github.com/faustbrian/go-rabbitmq-queues.
Applications that need exchanges, independent fan-out, queue-type-specific policy, native publications, or direct delivery settlement should use go-rabbitmq-queues instead. This module exists for bounded migration of go-queue job workers.
Index ¶
- Constants
- type DeadLetterConfig
- type NativeConfig
- type Option
- func WithAddr(addr string) Optiondeprecated
- func WithAutoAck(val bool) Option
- func WithDeadLetter(config DeadLetterConfig) Option
- func WithExchangeName(val string) Option
- func WithExchangeType(val string) Option
- func WithLogger(l queue.Logger) Option
- func WithNativeConfig(config NativeConfig) Option
- func WithPublishTimeout(timeout time.Duration) Option
- func WithQueue(val string) Option
- func WithReconnectConfig(config ReconnectConfig) Optiondeprecated
- func WithRequestTimeout(timeout time.Duration) Option
- func WithRoutingKey(val string) Option
- func WithRunFunc(fn func(context.Context, core.TaskMessage) error) Option
- func WithTag(val string) Option
- type ReconnectConfig
- type Worker
- func (*Worker) BackendName() string
- func (worker *Worker) Queue(task core.TaskMessage) error
- func (worker *Worker) QueueName() string
- func (worker *Worker) Request() (core.TaskMessage, error)
- func (worker *Worker) Run(ctx context.Context, task core.TaskMessage) error
- func (worker *Worker) Shutdown() error
Examples ¶
Constants ¶
const ( ExchangeDirect = "direct" ExchangeFanout = "fanout" ExchangeTopic = "topic" ExchangeHeaders = "headers" )
Predefined RabbitMQ exchange types for use in configuration. - ExchangeDirect: Direct exchange type. - ExchangeFanout: Fanout exchange type. - ExchangeTopic: Topic exchange type. - ExchangeHeaders: Headers exchange type.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DeadLetterConfig ¶
type DeadLetterConfig struct {
Exchange string
Queue string
RoutingKey string
MaxDeliveryAttempts uint32
}
DeadLetterConfig owns RabbitMQ terminal routing and bounded delivery policy.
type NativeConfig ¶
type NativeConfig struct {
Connection rabbitmqqueue.ConnectionConfig
Producer rabbitmqqueue.ProducerConfig
Consumer rabbitmqqueue.ConsumerConfig
MessageID func(core.TaskMessage) (string, error)
DeliveryMessageID func(rabbitmqqueue.Delivery, *job.Message) (string, error)
}
NativeConfig supplies explicit native connection, resource bounds, queue type, and stable application message identity to the compatibility adapter.
type Option ¶
type Option func(*options)
Option is a functional option type for configuring the options struct. It allows for flexible and composable configuration of RabbitMQ workers and queues.
func WithAddr
deprecated
func WithAutoAck ¶
WithAutoAck enables or disables automatic message acknowledgment.
Parameters: - val: true to enable auto-ack, false to disable.
Returns: - Option: Functional option to set autoAck.
func WithDeadLetter ¶
func WithDeadLetter(config DeadLetterConfig) Option
WithDeadLetter identifies the infrastructure-owned durable terminal exchange, queue, routing key, and maximum delivery attempts. The adapter publishes to the exchange and routing key but does not declare or repair the queue.
func WithExchangeName ¶
WithExchangeName sets the name of the AMQP exchange.
Parameters: - val: The exchange name.
Returns: - Option: Functional option to set the exchange name.
Exchanges are AMQP 0-9-1 entities where messages are sent to. Exchanges take a message and route it into zero or more queues.
func WithExchangeType ¶
WithExchangeType sets the type of the AMQP exchange. The compatibility adapter accepts direct and topic exchanges. Fanout and headers constants remain for source compatibility but NewWorkerE rejects them.
Parameters: - val: The exchange type (direct, fanout, topic, headers).
Returns: - Option: Functional option to set the exchange type.
The routing algorithm used depends on the exchange type and rules called bindings. AMQP 0-9-1 brokers provide four exchange types: - Direct exchange (Empty string) and amq.direct - Fanout exchange amq.fanout - Topic exchange amq.topic - Headers exchange amq.match (and amq.headers in RabbitMQ)
func WithLogger ¶
WithLogger sets a custom logger for the worker or queue.
Parameters: - l: The logger instance.
Returns: - Option: Functional option to set the logger.
func WithNativeConfig ¶
func WithNativeConfig(config NativeConfig) Option
WithNativeConfig enables the compatibility adapter with explicit native connection, resource, queue-type, and stable message-identity policy.
func WithPublishTimeout ¶
WithPublishTimeout bounds each RabbitMQ publish operation.
func WithQueue ¶
WithQueue sets the name of the queue to use.
Parameters: - val: The queue name.
Returns: - Option: Functional option to set the queue name.
func WithReconnectConfig
deprecated
func WithReconnectConfig(config ReconnectConfig) Option
WithReconnectConfig retains source compatibility with the legacy worker. NativeConfig.Connection.Recovery owns startup and runtime recovery.
Deprecated: configure WithNativeConfig instead.
func WithRequestTimeout ¶
WithRequestTimeout sets how long Request waits for a RabbitMQ delivery.
func WithRoutingKey ¶
WithRoutingKey sets the AMQP routing key.
Parameters: - val: The routing key.
Returns: - Option: Functional option to set the routing key.
func WithRunFunc ¶
WithRunFunc sets the function to execute for each task.
Parameters: - fn: The function to run for each task message.
Returns: - Option: Functional option to set the run function.
type ReconnectConfig ¶
ReconnectConfig is retained for source compatibility. NativeConfig.Connection.Recovery owns runtime recovery for compatibility-adapter workers.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker preserves the go-queue worker contract while delegating RabbitMQ policy and resource ownership to go-rabbitmq-queues.
func NewWorker ¶
NewWorker creates a compatibility worker and panics when its explicit native policy is invalid or its producer cannot be opened.
func NewWorkerE ¶
NewWorkerE creates a compatibility worker with an eagerly opened producer. The consumer remains unopened until Request is called.
Example ¶
package main
import (
"context"
"time"
"github.com/faustbrian/go-queue/core"
rabbitmq "github.com/faustbrian/go-queue/rabbitmq"
rabbitmqqueue "github.com/faustbrian/go-rabbitmq-queues"
)
func main() {
credentials := rabbitmqqueue.CredentialProviderFunc(
func(context.Context) (rabbitmqqueue.Credentials, error) {
return rabbitmqqueue.Credentials{Username: "worker", Password: []byte("owned-secret")}, nil
},
)
_, _ = rabbitmq.NewWorkerE(
rabbitmq.WithNativeConfig(rabbitmq.NativeConfig{
Connection: rabbitmqqueue.ConnectionConfig{
Endpoints: []rabbitmqqueue.Endpoint{{Host: "rabbitmq.internal", Port: 5671}},
VirtualHost: "/", Credentials: credentials,
TLS: rabbitmqqueue.TLSConfig{ServerName: "rabbitmq.internal"},
DialTimeout: 5 * time.Second, Heartbeat: 30 * time.Second,
Recovery: rabbitmqqueue.RecoveryPolicy{
MaxAttempts: 8, InitialDelay: 100 * time.Millisecond, MaxDelay: 5 * time.Second,
},
},
Producer: rabbitmqqueue.ProducerConfig{
Limits: rabbitmqqueue.DefaultLimits(), MaxOutstanding: 256,
PublishTimeout: 5 * time.Second,
},
Consumer: rabbitmqqueue.ConsumerConfig{
Limits: rabbitmqqueue.DefaultLimits(),
Queue: rabbitmqqueue.QueueReference{Name: "jobs", Type: rabbitmqqueue.QueueQuorum},
Name: "jobs-worker", Prefetch: 32, Concurrency: 8,
HandlerTimeout: time.Minute, MaxRequeues: 1,
Failure: rabbitmqqueue.Reject(false),
},
MessageID: func(core.TaskMessage) (string, error) { return "stable-job-id", nil },
}),
rabbitmq.WithQueue("jobs"),
rabbitmq.WithTag("jobs-worker"),
rabbitmq.WithExchangeName("jobs.events"),
rabbitmq.WithExchangeType(rabbitmq.ExchangeTopic),
rabbitmq.WithRoutingKey("jobs.created"),
)
}
Output:
func (*Worker) BackendName ¶
BackendName identifies RabbitMQ in lifecycle events.
func (*Worker) Queue ¶
func (worker *Worker) Queue(task core.TaskMessage) error
Queue publishes one mandatory persistent task and waits for a definitive broker confirmation.
func (*Worker) Request ¶
func (worker *Worker) Request() (core.TaskMessage, error)
Request returns one decoded task from the bounded native delivery bridge.