private async Task AcquireLock(IDistributedLock consulLock, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { if (!consulLock.IsHeld) { if (_currentRole != NodeRole.Follower) { _applicationEvents.Publish(new NodeRoleTransitioned(NodeRole.Follower)); _currentRole = NodeRole.Follower; } consulLock.Acquire(cancellationToken); } while (!cancellationToken.IsCancellationRequested && consulLock.IsHeld) { if (_currentRole != NodeRole.Leader) { _applicationEvents.Publish(new NodeRoleTransitioned(NodeRole.Leader)); _currentRole = NodeRole.Leader; } await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).ConfigureAwait(false); } } }
[MethodImpl(MethodImplOptions.NoInlining)] // need to isolate for GC private WeakReference TestCleanupHelper(IDistributedLock lock1, IDistributedLock lock2) { var handle = lock1.Acquire(); Assert.IsNull(lock2.TryAcquireAsync().Result); return(new WeakReference(handle)); }
protected override void SelectLeader() { try { var task = _lock.Acquire(); Task.WaitAll(task); } catch (AggregateException) { } catch (LockMaxAttemptsReachedException) { // Ignore because lock delay might be in effect. } }
private async Task TryAcquireLock(CancellationToken token) { if (token.IsCancellationRequested) { return; } try { //distributedLock = Lock.Create(key, LockTTL); await _distributedLock.Acquire(); } catch (Exception) { //Log } finally { bool lockHeld = _distributedLock?.IsHeld == true; HandleLockStatusChange(lockHeld); timer.Change(RefreshTime, Timeout.InfiniteTimeSpan); } }
public static void Acquire(this IDistributedLock mutexLock, TimeSpan timeout) { mutexLock.Acquire((int)timeout.TotalMilliseconds); }
public static void Acquire(this IDistributedLock mutexLock) { mutexLock.Acquire(0); }
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); } } }