/// <summary> /// Lock /// </summary> /// <param name="client"></param> /// <param name="globalLockKey"></param> /// <param name="acquisitionTimeout">in seconds</param> /// <param name="lockTimeout">in seconds</param> public DisposableDistributedLock(IRedisClient client, string globalLockKey, int acquisitionTimeout, int lockTimeout) { myLock = new DistributedLock(); myClient = client; this.globalLockKey = globalLockKey; lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout, out lockExpire, myClient); }
/// <summary> /// Lock /// </summary> /// <param name="client"></param> /// <param name="globalLockKey"></param> /// <param name="acquisitionTimeout">in seconds</param> /// <param name="lockTimeout">in seconds</param> public DisposableDistributedLock(IRedisClient client, string globalLockKey, int acquisitionTimeout, int lockTimeout) { myLock = new DistributedLock(); myClient = client; this.globalLockKey = globalLockKey; lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout, out lockExpire, myClient); }
static void ZooKeeperReadWriteLockTest() { var rd = new Random(); for (int i = 0; i < 5; i++) { Task.Factory.StartNew(() => { var seed = rd.Next(1, 100); IDistributedLock zkLock = ((seed % 2) == 1) ? new ZooKeeperWriteLock(new ZooKeeperReadWriteLockOptions()) as IDistributedLock : new ZooKeeperReadLock(new ZooKeeperReadWriteLockOptions()) as IDistributedLock; using (zkLock) { if (zkLock.Lock()) { Thread.Sleep(1000); zkLock.UnLock(); Trace.WriteLine("UnLock"); } else { Trace.WriteLine("Get Lock Failed."); } } }); } }
/// <summary> /// Lock /// </summary> /// <param name="client"></param> /// <param name="globalLockKey"></param> /// <param name="acquisitionTimeout">in seconds</param> /// <param name="lockTimeout">in seconds</param> public DisposableDistributedLock(ConnectionMultiplexer connection, string globalLockKey, int lockTimeout) { myLock = new DistributedLock(); myConnection = connection; this.globalLockKey = globalLockKey; this.lockValue = Guid.NewGuid().ToString(); isLocked = myLock.Lock(globalLockKey, this.lockValue, lockTimeout, myConnection); }
private async Task <int> GetWorkerId() { if (!_distributedIdOption.Distributed) { return(_distributedIdOption.WorkderId == 0 ? new Random().Next(1, 1024) : _distributedIdOption.WorkderId); } //分布式需要计算WorkerId IDistributedCache distributedCache = _serviceProvider.GetService <IDistributedCache>(); if (distributedCache == null) { throw new Exception("请注入IDistributedCache:https://github.com/Coldairarrow/Colder.Components#%E5%88%86%E5%B8%83%E5%BC%8F%E7%BC%93%E5%AD%98"); } IDistributedLock distributedLock = _serviceProvider.GetService <IDistributedLock>(); if (distributedLock == null) { throw new Exception("请注入IDistributedLock:https://github.com/Coldairarrow/Colder.Components#%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81"); } for (int i = 1; i < 1024; i++) { var lockKey = $"{GetType().FullName}:WorkerIdLock:{i}"; using var _ = distributedLock.Lock(lockKey); var key = $"{GetType().FullName}:WorkerId:{i}"; var value = await distributedCache.GetStringAsync(key); if (string.IsNullOrEmpty(value)) { await distributedCache.SetStringAsync(key, DateTimeOffset.Now.ToString(), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) }); //定时刷新,心跳 _timer = new Timer(state => { distributedCache.SetString(key, DateTimeOffset.Now.ToString(), new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) }); }, null, 0, 1000); return(i); } } throw new Exception("WorkerId已用完"); }
/// <summary> /// Lock /// </summary> /// <param name="client"></param> /// <param name="globalLockKey"></param> /// <param name="acquisitionTimeout">in seconds</param> /// <param name="lockTimeout">in seconds</param> public DisposableDistributedLock(IRedisClient client, string globalLockKey, int acquisitionTimeout, int lockTimeout) { myLock = new DistributedLock(client); lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout); }
/// <summary> /// Lock /// </summary> /// <param name="client"></param> /// <param name="globalLockKey"></param> /// <param name="acquisitionTimeout">in seconds</param> /// <param name="lockTimeout">in seconds</param> public DisposableDistributedLock(IRedisClient client, string globalLockKey, int acquisitionTimeout, int lockTimeout) { myLock = new DistributedLock(client); lockState = myLock.Lock(globalLockKey, acquisitionTimeout, lockTimeout); }