protected override async Task<Lease> AcquireLease() { var now = DateTimeOffset.UtcNow; var resource = leasableResources .Where(l => l.LeaseLastReleased + waitInterval < now) .OrderBy(l => l.LeaseLastReleased) .FirstOrDefault(l => !workInProgress.ContainsKey(l)); if (resource == null) { return null; } var lease = new Lease(resource, resource.DefaultLeaseDuration); if (workInProgress.TryAdd(resource, lease)) { lease.LeasableResource.LeaseLastGranted = now; return lease; } return null; }
protected override async Task ReleaseLease(Lease lease) { lease.NotifyCompleted(); if (!workInProgress.Values.Any(l => l.GetHashCode().Equals(lease.GetHashCode()))) { Debug.WriteLine("[Distribute] ReleaseLease (failed): " + lease); return; } Lease _; if (workInProgress.TryRemove(lease.LeasableResource, out _)) { lease.LeasableResource.LeaseLastReleased = DateTimeOffset.UtcNow; Debug.WriteLine("[Distribute] ReleaseLease: " + lease); } }
protected abstract Task ReleaseLease(Lease lease);