/// <summary> /// Main /// </summary> public static async Task Main(string[] args) { // --> #1 AcquireLock with retry. using (ILockObject outerLockObject = _lockFactory.AcquireLock(_lockKey, TimeSpan.FromSeconds(3))) using (ILockObject innerLockObject = _lockFactory.AcquireLock(_lockKey, TimeSpan.FromSeconds(5), 2, TimeSpan.FromSeconds(2))) Console.WriteLine($"Did I get a lock? -> {innerLockObject.IsAcquired}"); // --> #2 AcquireLockAsync with retry. using (ILockObject outerLockObject = await _lockFactory.AcquireLockAsync(_lockKey, TimeSpan.FromSeconds(3))) using (ILockObject innerLockObject = await _lockFactory.AcquireLockAsync(_lockKey, TimeSpan.FromSeconds(5), 2, TimeSpan.FromSeconds(2))) Console.WriteLine($"Did I get a lock? -> {innerLockObject.IsAcquired}"); // --> #3 AcquireLockAsync with retry + timeout. try { using (CancellationTokenSource ctSource = new CancellationTokenSource(TimeSpan.FromSeconds(3.5))) { using (ILockObject outerLockObject = await _lockFactory.AcquireLockAsync(_lockKey, TimeSpan.FromSeconds(3))) using (ILockObject innerLockObject = await _lockFactory.AcquireLockAsync(_lockKey, TimeSpan.FromSeconds(5), 2, TimeSpan.FromSeconds(2), ctSource.Token)) Console.WriteLine($"Did I get a lock? -> {innerLockObject.IsAcquired}"); } } catch (OperationCanceledException) { Console.WriteLine("I expected for an OperationCanceledException."); } // --> #4 Dinner is ready to eat. Parallel.For(0, 5, x => personEat(x)); Console.WriteLine("End of the dinner."); }
public override void FeatureDecoratorHandler(T context) { //Trace message context.Logger.Trace("Acquiring lock"); try { using (_lockFactory.AcquireLock(_keySelector(context), _lockWaitMillis)) { context.Logger.Trace("Lock acquired, passing message to next filter"); //trace message Lock Acquired Handler?.Handle(context); context.Logger.Trace("Lock releasing"); } } catch (Exception e) { context.Logger.Error("ERROR while acquiring lock, Item skipped.", e); } }