Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow choosing the nack method #521

Open
babariviere opened this issue Nov 29, 2024 · 0 comments
Open

Allow choosing the nack method #521

babariviere opened this issue Nov 29, 2024 · 0 comments

Comments

@babariviere
Copy link

Hello, I would like to be able to choose the Nack method used depending on the message processing.

Example

// file: watermill/message.go

type Message strut {}

func (m *Message) Nack(method NackMethod) {
	m.nack <- method
}

// file: watermill-amqp/subscriber.go

// Here we can handle the method

// file: handler.go

func handler(msg *message.Message) error {
    // ...

	msg.Nack(message.NackRequeue)
	msg.Nack(message.NackDiscard)
	msg.Nack(message.NackDelay(5*time.Minute))
}

With this example implementation, nack will return a method and the subscriber can:

  • either handle it and support the method
  • default to it's default nack method if it doesn't support discard or delay for example

Of course this is just an example, but with method like this, we should be able to override the nack method used.

This can be used in subscriber like:

  • amqp with amqp.NackDiscard and amqp.NackRequeue
  • nats with nats.Term(), nats.InProgress(), nats.Nak() and nats.NakWithDelay()

Middleware example

To make this easier to use, we can make use of it via middleware. For example, an handler can return an error, and the middleware will choose the best method for the ack.

func handler(msg *message.Message) error {
	return ErrShouldRequeue
}

func middleware(h message.HandlerFunc) message.HandlerFunc {
	return func(msg *message.Message) error {
		err := h(msg)
		if errors.Is(err, ErrShouldRequeue) {
			msg.Nack(message.NackRequeue)
		}
		return err
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant