question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async locking based on key

See original GitHub issue

Sometimes, I have situations where I want to lock certain areas for a particular user only (e.g. some business logic must be run in one piece for a certain user). I’m using neosmart AsyncLock in some software solutions and therefore - instead of inventing some new nugets - I was asking myself if it would be possible to extend AsyncLock.LockAsync method with a key parameter.

using (await _lock.LockAsync(userId))
{
   // Protected for User with userId
}

I’m not sure if you let other people touch your code (you know what I mean…) and I also have not a very clear picture of how I could integrate such a feature into AsyncLock. But I guess we could use some kind of thread-safe dictionary, which adds key-value pairs of T (generic-typed key) and value (the lock handle, some SemaphoreSlim or whatever you use in AsyncLock).

Let me know what you think about it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mqudsicommented, Aug 19, 2021

Note that AsyncLock and ScopedMutex are fundamentally different under the hood (one recurses, one doesn’t; one requires synchronization over a global dictionary, one doesn’t). Adding NeoSmart.Synchronization will pull in NeoSmart.AsyncLock so you don’t need to add both to your project, if you care about that.

1reaction
mqudsicommented, Aug 15, 2021

Yes, this is an extremely important type of locking to support, especially in contexts where idempotency is a must (e.g. when charging an order). I actually already published a nuget for just that when .NET Core was first introduced to work around the lack of named mutexes supporting .WaitAsync().

It’s part of the “NeoSmart Web Toolkit” available at https://github.com/neosmart/web and on NuGet as NeoSmart.Web. You want NeoSmart.Web.ScopedMutex (direct link to docs).

I’m pretty sure this sample is exactly what you are looking for:

using (await ScopedMutex::CreateAsync("myguid"))
{
	// Exclusive access granted, any other calls to Create()
	// or CreateAsync() will block until `mutex` is disposed!

	foo.DoSomethingCritical();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Asynchronous locking based on a key
I wrote a class designed to perform an asynchronous lock based upon a key generated by a hashed url but it seems I...
Read more >
amoerie/keyed-semaphores: 🔑🔒 Lock your c# threads by key
LockAsync (string key) covers most use cases, this sample snippet shows how it works ... of the array based approach is that nested...
Read more >
Locking async methods in C#. You are building a stateless ...
As I said at the beginning, we need to lock based on an item in the cache. First, we need to store our...
Read more >
SemaphoreSlim locking based on a key : r/dotnet
I have written a library called AsyncKeyedLock at https://github.com/MarkCiliaVincenti/AsyncKeyedLock to tackle this common problem. I had ...
Read more >
C# – Asynchronous locking based on a key
I wrote a class designed to perform an asynchronous lock based upon a key generated by a hashed url but it seems I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found