消息锁定和锁定更新在 Azure 服务总线中如何工作?

How does message locking and lock renewal work in Azure Service Bus?

我有点困惑,试图确定服务总线中窥视锁定的工作方式。特别是我将 Microsoft.Azure.ServiceBus 与 Azure Functions 和 ServiceBusTrigger 一起使用。

据我所知,消息被锁定的时间是在队列本身上设置的,默认为 30 秒,但可以设置为最多 5 分钟。

当从队列中查看消息时,此锁启动。

然后有一个名为 maxAutoRenewDuration 的设置,当使用 Azure Functions 时,它在 Extensions:ServiceBus:messageHandlerOptions 下的 host.json 文件中设置。这允许客户端自动请求一个或多个锁的扩展,直到达到 maxAutoRenewDuration。达到此限制后,将不会请求续订并解除锁定。

续订是尽力而为,无法保证,因此理想情况下,您尝试设计一种消息通常在队列上指定的锁定期限内处理的设计。

到目前为止我做对了吗?

我还有问题

  1. 存在并限制了 maxAutoRenewDuration 可以设置的值。我读过的一篇文章似乎建议可以将其设置为确保我的消息得到处理所需的任何值 (link). The Microsoft Documentation though states that the maximum value for this is also limited to 5 minutes (link)。

The maxAutoRenewDuration is configurable in host.json, which maps to OnMessageOptions.MaxAutoRenewDuration. The maximum allowed for this setting is 5 minutes according to the Service Bus documentation

哪个是正确的?我知道默认锁定持续时间最长为 5 分钟,但这似乎也适用于 maxAutoRenewDuration?

  1. 我在一些文章(例如 link)中读到过一个名为 MaxLockDuration 的设置。这只是指队列本身设置的锁定时间吗?

  2. 我还遗漏了什么吗?队列上设置的锁定持续时间和代码中的 maxAutoRenewDuration 是我在处理锁定和续订时需要考虑的主要事项吗?

谢谢

艾伦

我理解你的困惑。 official doc explanation of maxAutoRenewDuration seems wrong. There is already an open doc issue https://github.com/MicrosoftDocs/azure-docs/issues/62110 and also a reference https://github.com/Azure/azure-functions-host/issues/6500.

确定您的问题:

  • #1:如上所述,存在打开文档问题。
  • #2:MaxLockDuration 是服务总线队列端设置,它基本上表示如果您从队列中查看锁定一条消息,则该消息会在该持续时间内为消费者锁定。因此,除非您在此期间内完成消息处理或更新锁,否则锁将过期。
  • #3:@sean-feldman 在线程 中的精彩解释应该回答这个问题。

What it does is extends the message lease with the broker, "re-locking" it for the competing consumer that is currently handling the message. MaxAutoRenewDuration should be set to the "possibly maximum processing time a lease will be required".