Exemplo n.º 1
0
        public async ValueTask <ILock> GetLock(string key)
        {
            if (_disposed)
            {
                throw new LockException("LockProvider is disposed");
            }

            StartMonitoring();

            var @lock = new RedisLock(key, this);

            //await _db.sScriptEvaluateAsync(_script, new RedisKey[] { new RedisKey(key) });
            var adquired = await _db.LockTakeAsync(key, @lock.Token, _expire);

            if (adquired)
            {
                _logger.LogInformation("Lock on {Key} adquired", @lock.Key);
                @lock.AdquireLock();
                lock (_locks)
                {
                    _locks.Add(@lock);
                }
                return(@lock);
            }

            lock (_locks)
            {
                _locks.Add(@lock);
            }

            _logger.LogInformation("Lock on {Key} pending", @lock.Key);
            return(await @lock.CreateTask());
        }
Exemplo n.º 2
0
        private void Extend(RedisLock @lock)
        {
            var result = _db.LockExtend(@lock.Key, @lock.Token, _expire);

            if (!result)
            {
                _logger.LogInformation("Log on {Key} expired or the process doesn't own the lock", @lock.Key);

                lock (_locks)
                {
                    _locks.Remove(@lock);
                }

                throw new LockException($"Lock {@lock.Key} Expired or the process doesn't own the lock");
            }
            @lock.AdquireLock();
        }