Example #1
0
        public async Task <IDistributedLock> CreateLockAsync(string name, TimeSpan expiry, TimeSpan wait, TimeSpan retry, CancellationToken cancellationToken, Action expirationAction = null)
        {
            IDistributedLock result = null;
            PeriodicAction   timer  = null;

            timer = new PeriodicAction(async ct =>
            {
                var dlock = await TryAquireLock(name, expiry, expirationAction);
                if (dlock != null)
                {
                    result = dlock;
                    timer?.Stop();
                }
            }, Convert.ToInt32(retry.TotalMilliseconds), wait);
            cancellationToken.Register(() => timer.Stop());
            using (var scope = TraceContext.Begin())
            {
                _logger.TraceVerboseEvent($"Starting trying to acquire lock {name} for {wait} each {retry}");
                await timer.Start();

                return(result ?? new EfDistributedLock(name));
            }
        }