Azure 是否有开箱即用的密钥分布式锁定方式?

See*_*arp 7 c# locking azure azure-webjobs azure-functions

我希望能够编写类似的代码

using (await LockAsync(x.Id))
{
    // here goes code that is unsafe with respect to x.Id
    // because it can be executed in multiple threads on
    // the same instance or on multiple instances (e.g.
    // multiple Azure Functions
}
Run Code Online (Sandbox Code Playgroud)

我知道有一些开箱即用的方法可以在不使用密钥的情况下锁定整个执行上下文(例如[ActivityTrigger]在函数中),并且我知道我可以使用 blob 租约滚动自己的带钥匙的锁,但是有没有一种开箱即用的方法钥匙锁?

R.T*_*tov 2

可以使用Redis分布式锁。

RedLock.net包正是您所需要的:

var resource = "the-thing-we-are-locking-on";
var expiry = TimeSpan.FromSeconds(30);


using (var redLock = await redlockFactory.CreateLockAsync(resource, expiry)) // there are also non async Create() methods
{
   // make sure we got the lock
   if (redLock.IsAcquired)
   {
    // do stuff
   }
}
Run Code Online (Sandbox Code Playgroud)