Documentation
¶
Index ¶
- Constants
- type DeadLetterConfig
- type Option
- func WithAddr(addr string) Option
- 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 WithPublishTimeout(timeout time.Duration) Option
- func WithQueue(val string) Option
- func WithReconnectConfig(config ReconnectConfig) Option
- 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
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 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 ¶
WithAddr sets the AMQP server URI.
Parameters: - addr: The AMQP URI to connect to.
Returns: - Option: Functional option to set the address.
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 configures the durable exchange, queue, routing key, and maximum broker delivery attempts used for terminal RabbitMQ failures.
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.
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 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 ¶
func WithReconnectConfig(config ReconnectConfig) Option
WithReconnectConfig configures connection retry timing.
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 defines the retry policy for RabbitMQ connection.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker struct implements the core.Worker interface for RabbitMQ. It manages the AMQP connection, channel, and task consumption. Fields: - conn: AMQP connection to RabbitMQ server. - channel: AMQP channel for communication. - stop: Channel to signal worker shutdown. - stopFlag: Atomic flag to indicate if the worker is stopped. - stopOnce: Ensures shutdown logic runs only once. - startOnce: Ensures consumer initialization runs only once. - opts: Configuration options for the worker. - tasks: Channel for receiving AMQP deliveries (tasks).
func NewWorker ¶
NewWorker creates and initializes a new Worker instance with the provided options. It establishes a connection to RabbitMQ, sets up the channel, and declares the exchange. If any step fails, it logs a fatal error and terminates the process.
Parameters: - opts: Variadic list of Option functions to configure the worker.
Returns: - Pointer to the initialized Worker.
func NewWorkerE ¶
NewWorkerE creates a worker and returns connection and setup errors.
func (*Worker) BackendName ¶
BackendName identifies RabbitMQ in lifecycle events.
func (*Worker) Queue ¶
func (w *Worker) Queue(job core.TaskMessage) error
Queue publishes a new task message to the RabbitMQ exchange. If the worker is stopped, it returns queue.ErrQueueShutdown.
Parameters: - job: The task message to be published.
Returns: - error: Any error encountered during publishing, or nil on success.
func (*Worker) Request ¶
func (w *Worker) Request() (core.TaskMessage, error)
Request retrieves a new task message from the queue. It starts the consumer if not already started, waits for a message, and unmarshals it into a job.Message. If no message is received within a timeout, it returns queue.ErrNoTaskInQueue.
Returns: - core.TaskMessage: The received task message, or nil if none. - error: Any error encountered, or queue.ErrNoTaskInQueue if no task is available.
func (*Worker) Run ¶
Run executes the worker's task processing function. It delegates the actual task handling to the configured runFunc.
Parameters: - ctx: Context for cancellation and timeout. - task: The task message to process.
Returns: - error: Any error returned by the runFunc.
func (*Worker) Shutdown ¶
Shutdown gracefully stops the worker. It ensures shutdown logic runs only once, cancels the consumer, and closes the AMQP connection. If the worker is already stopped, it returns queue.ErrQueueShutdown.
Returns: - error: Any error encountered during shutdown, or nil on success.