|
| 1 | +package goqu |
| 2 | + |
| 3 | +import "context" |
| 4 | + |
| 5 | +// Consumer represents an entity that consumes messages from a queue. |
| 6 | +type Consumer interface { |
| 7 | + // Consume consumes messages from the queue and passes them to the provided handler. |
| 8 | + // It takes a context, an InboundMessageHandler, and a map of metadata as parameters. |
| 9 | + // It returns an error if there was a problem consuming the messages. |
| 10 | + Consume(ctx context.Context, handler InboundMessageHandler, meta map[string]interface{}) (err error) |
| 11 | + |
| 12 | + // Stop stops the consumer from consuming messages. |
| 13 | + // It takes a context as a parameter and returns an error if there was a problem stopping the consumer. |
| 14 | + Stop(ctx context.Context) (err error) |
| 15 | +} |
| 16 | + |
| 17 | +type InboundMessageHandler interface { |
| 18 | + HandleMessage(ctx context.Context, m InboundMessage) (err error) |
| 19 | +} |
| 20 | + |
| 21 | +type InboundMessageHandlerFunc func(ctx context.Context, m InboundMessage) (err error) |
| 22 | + |
| 23 | +func (mhf InboundMessageHandlerFunc) HandleMessage(ctx context.Context, m InboundMessage) (err error) { |
| 24 | + return mhf(ctx, m) |
| 25 | +} |
| 26 | + |
| 27 | +type InboundMessageHandlerMiddlewareFunc func(next InboundMessageHandlerFunc) InboundMessageHandlerFunc |
| 28 | + |
| 29 | +type InboundMessage struct { |
| 30 | + Message |
| 31 | + RetryCount int64 `json:"retryCount"` |
| 32 | + Metadata map[string]interface{} `json:"metadata"` |
| 33 | + // Ack is used for confirming the message. It will drop the message from the queue. |
| 34 | + Ack func(ctx context.Context) (err error) `json:"-"` |
| 35 | + // Nack is used for rejecting the message. It will requeue the message to be re-delivered again. |
| 36 | + Nack func(ctx context.Context) (err error) `json:"-"` |
| 37 | + // MoveToDeadLetterQueue is used for rejecting the message same with Nack, but instead of requeueing the message, |
| 38 | + // Read how to configure dead letter queue in each queue provider. |
| 39 | + // eg RabbitMQ: https://www.rabbitmq.com/docs/dlx |
| 40 | + MoveToDeadLetterQueue func(ctx context.Context) (err error) `json:"-"` |
| 41 | + // Requeue is used to put the message back to the tail of the queue after a delay. |
| 42 | + Requeue func(ctx context.Context, delayFn DelayFn) (err error) `json:"-"` |
| 43 | +} |
0 commit comments