public async Task Lock_EphemeralAcquireRelease() { const string keyName = "test/lock/ephemerallock"; var sessionId = await _client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete }); var l = await _client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None); Assert.True(l.IsHeld); await _client.Session.Destroy(sessionId.Response); await TimeoutUtils.WaitFor(() => !l.IsHeld, "Expected lock to be lost when session destroyed"); Assert.Null((await _client.KV.Get(keyName)).Response); }
public async Task Lock_EphemeralAcquireRelease() { const string keyName = "test/lock/ephemerallock"; var sessionId = await _client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete }); var l = await _client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None); Assert.True(l.IsHeld); await _client.Session.Destroy(sessionId.Response); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.False(l.IsHeld); Assert.Null((await _client.KV.Get(keyName)).Response); }
public void Lock_Disposable() { var client = new ConsulClient(); const string keyName = "test/lock/disposable"; using (var l = client.AcquireLock(keyName)) { Assert.True(l.IsHeld); } }
public async Task Lock_AcquireTimeout() { using (var client = new ConsulClient()) { const string keyName = "test/lock/acquiretimeout"; var distributedLock = await client.AcquireLock(keyName); try { using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5))) { await Assert.ThrowsAsync <LockNotHeldException>(() => client.AcquireLock(keyName, cts.Token)); } } finally { await distributedLock.Release(); } } }
public async Task Lock_EphemeralAcquireRelease() { var client = new ConsulClient(); const string keyName = "test/lock/ephemerallock"; var sessionId = await client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete }); using (var l = client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None)) { Assert.True(l.IsHeld); await client.Session.Destroy(sessionId.Response); } Assert.Null((await client.KV.Get(keyName)).Response); }
public async Task Lock_EphemeralAcquireRelease() { var client = new ConsulClient(); const string keyName = "test/lock/ephemerallock"; var sessionId = await client.Session.Create(new SessionEntry { Behavior = SessionBehavior.Delete }); using (var l = client.AcquireLock(new LockOptions(keyName) { Session = sessionId.Response }, CancellationToken.None)) { Assert.True(l.IsHeld); await client.Session.Destroy(sessionId.Response); } Assert.Null((await client.KV.Get(keyName)).Response); }
public async Task Lock_Disposable() { var client = new ConsulClient(); const string keyName = "test/lock/disposable"; var l = await client.AcquireLock(keyName); try { Assert.True(l.IsHeld); } finally { await l.Release(); } }
public void Lock_Disposable() { var client = new ConsulClient(); const string keyName = "test/lock/disposable"; using (var l = client.AcquireLock(keyName)) { Assert.True(l.IsHeld); } }
protected async Task TryAccquireLockAsync(CancellationToken token) { if (!token.IsCancellationRequested) { try { if (_distributedLock == null) { var client = new ConsulClient(); var se = new SessionEntry() { Checks = new List <string>() { // Default health check for the consul agent. It is very recommended to keep this. "serfHealth", // "myServiceHealthCheck" // Any additional health check. }, Name = "myServicSession", // Optional TTL check, to achieve sliding expiration. It is very recommended to use it. TTL = TimeSpan.FromSeconds(30) }; var sessionId = (await client.Session.Create(se).ConfigureAwait(false)).Response; if (se.TTL.HasValue) { _sessionRenewCts = new CancellationTokenSource(); _sessionRenewTask = client.Session.RenewPeriodic(se.TTL.Value, sessionId, WriteOptions.Default, _sessionRenewCts.Token); } _distributedLock = await client .AcquireLock( new LockOptions(_key) { Session = sessionId, LockTryOnce = true, LockWaitTime = TimeSpan.FromSeconds(3) }, token).ConfigureAwait(false); } else { if (!_distributedLock.IsHeld) { if (_sessionRenewTask.IsCompleted) { Task.WaitAny( _sessionRenewTask); //Awaits the task without throwing, cleaner than try catch. _distributedLock = null; } else { await _distributedLock.Acquire(token).ConfigureAwait(false); } } } } catch (LockMaxAttemptsReachedException ex) { _logger.Warning(ex, ex.Message); } catch (Exception ex) { _logger.Error(ex, ex.Message); } finally { if (_distributedLock == null && _sessionRenewCts != null) { _sessionRenewCts.Cancel(); _sessionRenewCts.Dispose(); } HandleLockStatusChange(_distributedLock?.IsHeld == true); // Retrigger the timer after an 10 seconds delay (in this example) _timer.Change(10000, Timeout.Infinite); } } }