确认在NSQ中的消息

huangapple go评论79阅读模式
英文:

Acknowledge messages in NSQ

问题

我是新手,对NSQ不太了解,想知道是否可以确认消息已被消费(就像Google PubSub一样)。我正在使用Golang,在阅读NSQ文档(https://github.com/nsqio/nsq/blob/v1.2.1/nsqd/channel.go#L350)时,我发现了FinishMessage方法,根据注释,它应该“成功丢弃正在处理的消息”。这是否可以视为确认?如果在NSQ中未确认消息,如何确保消息在被消费后不会再次被消费?

谢谢。

英文:

I'm new to NSQ and was wondering whether it is possible to acknowledge that a message was consumed (just like in Google PubSub). I'm using Golang, and while going through the NSQ documentation (https://github.com/nsqio/nsq/blob/v1.2.1/nsqd/channel.go#L350) I've found the method FinishMessage which according to its comment, it's supposed to successfully discards an in-flight message. Would that be considered the same as acknowledged? If a message is not acknowledged in NSQ, how is it ensured it's gone once consumed so it won't be consumed over again?

Thanks

答案1

得分: 1

NSQ在将消息发送给消费者后,会自动从队列中删除该消息。如果消费者在设定的超时时间内没有响应,NSQ会自动将消息重新放入队列中。

是的,根据你使用的编程语言客户端的不同,你需要发送'finish'指令。这里是Golang的文档链接:https://pkg.go.dev/github.com/nsqio/go-nsq#Finish

但是由于NSQ的设计方式,如果预期的操作不成功,你需要从你的应用程序中重新将消息放入队列中。

这是他们网站上的一段摘录:

NSQ保证消息至少会被传递一次,尽管重复的消息是可能的。消费者应该预期到这一点,并进行去重或执行幂等操作。

这个保证是作为协议的一部分来执行的,工作原理如下(假设客户端已成功连接并订阅了一个主题):

  1. 客户端表示准备好接收消息
  2. NSQ发送一条消息并在本地临时存储数据(以防重新放入队列或超时)
  3. 客户端回复FIN(完成)或REQ(重新放入队列),表示成功或失败。如果客户端没有回复,NSQ将在可配置的持续时间后超时并自动重新放入消息。

你可以在这里阅读更多信息:https://nsq.io/overview/design.html

希望我的回答对你有帮助。

英文:

NSQ automatically removes a message from the queue after sending it to a consumer. And automatically re-queues the message if there is no response from the consumer after a set timeout.

And yes, depending on which programming language client you are using you have to send 'finish'.
Here are the docs for Golang
https://pkg.go.dev/github.com/nsqio/go-nsq#Finish

But because of the way NSQ is designed you have to re-queue a message from your app if the intended action was not successful.

Here is an excerpt from their website

>NSQ guarantees that a message will be delivered at least once, though duplicate messages are possible. Consumers should expect this and de-dupe or perform idempotent operations.

>This guarantee is enforced as part of the protocol and works as follows (assume the client has successfully connected and subscribed to a topic):

>1. client indicates they are ready to receive messages
> 2. NSQ sends a message and temporarily stores the data locally (in the event of re-queue or timeout)
> 3. client replies FIN (finish) or REQ (re-queue) indicating success or failure respectively. If client does not reply NSQ will timeout after
> a configurable duration and automatically re-queue the message)

You can read more here: https://nsq.io/overview/design.html

Hope my answer helps you.

huangapple
  • 本文由 发表于 2022年4月9日 07:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/71804016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定