Conditionally lock resources

Viewed 178

I have a consumer class that is a Singleton. Is it possible in any way to conditionally lock resources. Currently the external resources is "globally" locked with the AsyncEx (https://github.com/StephenCleary/AsyncEx) framework. But I would prefer to lock it based on the Key in the input message so messages with different Keys should not lock the resource.

public class Consumer
{
    private readonly AsyncLock _mutex = new AsyncLock();

    protected async Task Process(Message msg)
    {
       **UNBLOCKED CODE**

       using (await _mutex.LockAsync())
       {
          await GetData(msg.Key);
       }

       **UNBLOCKED CODE**
    }
}

Any suggestions?

1 Answers
Related