public async Task <RegisterMigrationResponse> Register(RegisterMigrationRequest request) { var source = new CancellationTokenSource(); var result = MigrationRegistryResult.Ok; source.CancelAfter(MigrationLockTimeoutDuration); var @lock = await _locker.AcquireAsync(request.Migration.Character.ToString(), cancellationToken : source.Token); if (@lock != null) { if (await _cache.ExistsAsync(request.Migration.Character.ToString())) { result = MigrationRegistryResult.FailedAlreadyRegistered; } if (result == MigrationRegistryResult.Ok) { await _cache.SetAsync(request.Migration.Character.ToString(), request.Migration, MigrationTimeoutDuration); } await @lock.ReleaseAsync(); } else { result = MigrationRegistryResult.FailedTimeout; } return(new RegisterMigrationResponse { Result = result }); }
public async Task <InviteRegisterResponse> Register(InviteRegisterRequest request) { var source = new CancellationTokenSource(); var result = InviteServiceResult.Ok; source.CancelAfter(InviteLockTimeoutDuration); var @lock = await _locker.AcquireAsync(request.Invite.Invited.ToString(), cancellationToken : source.Token); if (@lock != null) { var key = $"{request.Invite.Invited}:{request.Invite.Type}"; if (await _cache.ExistsAsync(key)) { result = InviteServiceResult.FailedAlreadyInvited; } if (result == InviteServiceResult.Ok) { await _cache.SetAsync(key, request.Invite, InviteTimeoutDuration); } await @lock.ReleaseAsync(); } else { result = InviteServiceResult.FailedTimeout; } return(new InviteRegisterResponse { Result = result }); }
public async Task <PartyLoadByIDResponse> LoadByID(PartyLoadByIDRequest request) { var source = new CancellationTokenSource(); source.CancelAfter(PartyLockTimeoutDuration); var @lock = await _locker.AcquireAsync(PartyLockKey, cancellationToken : source.Token); if (@lock != null) { var party = await _repository.Retrieve(request.Id); await @lock.ReleaseAsync(); return(new PartyLoadByIDResponse { Result = PartyServiceResult.Ok, Party = party?.ToContract() }); } return(new PartyLoadByIDResponse { Result = PartyServiceResult.FailedTimeout }); }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = new CancellationToken()) { var wi = (RemoveBotEventsWorkItem)workItem; string cacheKey = $"{nameof(RemoveBotEventsWorkItem)}:{wi.OrganizationId}:{wi.ProjectId}"; return(_lockProvider.AcquireAsync(cacheKey, TimeSpan.FromMinutes(15), cancellationToken)); }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = new CancellationToken()) { var wi = (ThrottleBotsWorkItem)workItem; string cacheKey = $"{nameof(ThrottleBotsWorkItemHandler)}:{wi.OrganizationId}:{wi.ClientIpAddress}"; return(_lockProvider.AcquireAsync(cacheKey, TimeSpan.FromMinutes(15), new CancellationToken(true))); }
protected override Task <ILock> GetQueueEntryLockAsync(IQueueEntry <SampleQueueWorkItem> queueEntry, CancellationToken cancellationToken = default(CancellationToken)) { if (_lockProvider != null) { return(_lockProvider.AcquireAsync("job", TimeSpan.FromMilliseconds(100), TimeSpan.Zero)); } return(base.GetQueueEntryLockAsync(queueEntry, cancellationToken)); }
protected override async Task <ILock> GetQueueEntryLockAsync(IQueueEntry <SampleQueueWorkItem> queueEntry, CancellationToken cancellationToken = new CancellationToken()) { if (_lockProvider != null) { return(await _lockProvider.AcquireAsync("job", TimeSpan.FromMilliseconds(100), TimeSpan.Zero).AnyContext()); } return(await base.GetQueueEntryLockAsync(queueEntry, cancellationToken).AnyContext()); }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = default) { var reindexWorkItem = workItem as ReindexWorkItem; if (reindexWorkItem == null) { return(null); } return(_lockProvider.AcquireAsync(String.Join(":", "reindex", reindexWorkItem.Alias, reindexWorkItem.OldIndex, reindexWorkItem.NewIndex), TimeSpan.FromMinutes(20), cancellationToken)); }
public async Task <IActionResult> Index() { //ILockProvider locker = // new ThrottlingLockProvider(new RedisHybridCacheClient(new RedisCacheClientOptions()), 1, TimeSpan.FromSeconds(90)); var locked = await _lockProvider.IsLockedAsync("Test"); if (!locked) { var result = await _lockProvider.AcquireAsync("Test", TimeSpan.FromSeconds(90)); } return(View()); }
public async Task <StartSessionResponse> Start(StartSessionRequest request) { var source = new CancellationTokenSource(); var result = SessionRegistryResult.Ok; source.CancelAfter(SessionLockTimeoutDuration); var session = request.Session; var @lock = await _locker.AcquireAsync(session.Account.ToString(), cancellationToken : source.Token); if (@lock != null) { var timeout = DateTime.UtcNow.Add(SessionTimeoutDuration); if (await _sessionAccountCache.ExistsAsync(request.Session.Account.ToString())) { result = SessionRegistryResult.FailedAlreadyStarted; } if (result == SessionRegistryResult.Ok) { await _sessionAccountCache.SetAsync(session.Account.ToString(), session, timeout); if (session.Character.HasValue) { await _sessionCharacterCache.SetAsync(session.Character.Value.ToString(), session, timeout); } } await @lock.ReleaseAsync(); } else { result = SessionRegistryResult.FailedTimeout; } return(new StartSessionResponse { Result = result }); }
public static async Task <bool> TryUsingAsync(this ILockProvider locker, string name, Func <Task> work, TimeSpan?lockTimeout = null, CancellationToken cancellationToken = default(CancellationToken)) { using (var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext()) { if (l != null) { await work().AnyContext(); return(true); } } return(false); }
private async Task <bool> DoLockedWork(ILockProvider locker) { using ( var l = await locker.AcquireAsync("DoLockedWork", TimeSpan.FromMinutes(1), TimeSpan.Zero).AnyContext()) { if (l == null) { return(false); } Thread.Sleep(200); return(true); } }
public static async Task <ILock> AcquireAsync(this ILockProvider provider, IEnumerable <string> resources, TimeSpan?timeUntilExpires = null, CancellationToken cancellationToken = default) { if (resources == null) { throw new ArgumentNullException(nameof(resources)); } var resourceList = resources.Distinct().ToArray(); if (resourceList.Length == 0) { return(new EmptyLock()); } var logger = provider.GetLogger(); if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace("Acquiring {LockCount} locks {Resource}", resourceList.Length, resourceList); } var sw = Stopwatch.StartNew(); var locks = await Task.WhenAll(resourceList.Select(r => provider.AcquireAsync(r, timeUntilExpires, cancellationToken))); sw.Stop(); // if any lock is null, release any acquired and return null (all or nothing) var acquiredLocks = locks.Where(l => l != null).ToArray(); var unacquiredResources = resourceList.Except(locks.Select(l => l?.Resource)).ToArray(); if (unacquiredResources.Length > 0) { if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace("Unable to acquire all {LockCount} locks {Resource} releasing acquired locks", unacquiredResources.Length, unacquiredResources); } await Task.WhenAll(acquiredLocks.Select(l => l.ReleaseAsync())).AnyContext(); return(null); } if (logger.IsEnabled(LogLevel.Trace)) { logger.LogTrace("Acquired {LockCount} locks {Resource} after {Duration:g}", resourceList.Length, resourceList, sw.Elapsed); } return(new DisposableLockCollection(locks, String.Join("+", locks.Select(l => l.LockId)), sw.Elapsed, logger)); }
public static async Task <bool> TryUsingAsync(this ILockProvider locker, string name, Func <Task> work, TimeSpan?lockTimeout = null, CancellationToken cancellationToken = default) { var l = await locker.AcquireAsync(name, lockTimeout, cancellationToken).AnyContext(); if (l == null) { return(false); } try { await work().AnyContext(); } finally { await l.ReleaseAsync().AnyContext(); } return(true); }
public static async Task <bool> TryUsingAsync(this ILockProvider locker, string resource, Func <CancellationToken, Task> work, TimeSpan?timeUntilExpires = null, CancellationToken cancellationToken = default) { var l = await locker.AcquireAsync(resource, timeUntilExpires, cancellationToken).AnyContext(); if (l == null) { return(false); } try { await work(cancellationToken).AnyContext(); } finally { await l.ReleaseAsync().AnyContext(); } return(true); }
public static async Task <bool> TryUsingAsync(this ILockProvider locker, string name, Func <CancellationToken, Task> work, TimeSpan?lockTimeout = null, TimeSpan?acquireTimeout = null) { using (var cancellationTokenSource = acquireTimeout.ToCancellationTokenSource()) { var l = await locker.AcquireAsync(name, lockTimeout, cancellationTokenSource.Token).AnyContext(); if (l == null) { return(false); } try { await work(cancellationTokenSource.Token).AnyContext(); } finally { await l.ReleaseAsync().AnyContext(); } } return(true); }
public static async Task <bool> TryUsingAsync(this ILockProvider locker, IEnumerable <string> resources, Func <Task> work, TimeSpan?timeUntilExpires, TimeSpan?acquireTimeout) { using (var cancellationTokenSource = acquireTimeout.ToCancellationTokenSource()) { var l = await locker.AcquireAsync(resources, timeUntilExpires, cancellationTokenSource.Token).AnyContext(); if (l == null) { return(false); } try { await work().AnyContext(); } finally { await l.ReleaseAsync().AnyContext(); } } return(true); }
public static async Task LockAsync(this ILockProvider lockProvider, string name, Func <Task> func, TimeSpan timeout, CancellationToken?cancellationToken = null, bool continueOnCapturedContext = false) { var @lock = await lockProvider.AcquireAsync(name, timeout, cancellationToken ?? CancellationToken.None) .ConfigureAwait(continueOnCapturedContext); try { await func().ConfigureAwait(continueOnCapturedContext); } finally { await @lock.ReleaseAsync() .ConfigureAwait(continueOnCapturedContext); } }
public static async Task <TResult> LockAsync <TResult>(this ILockProvider lockProvider, string name, Func <TResult> func, TimeSpan timeout, CancellationToken cancellationToken, bool continueOnCapturedContext = false) { TResult result; var @lock = await lockProvider.AcquireAsync(name, timeout, cancellationToken) .ConfigureAwait(continueOnCapturedContext); try { result = func(); } finally { await @lock.ReleaseAsync() .ConfigureAwait(continueOnCapturedContext); } return(result); }
protected override Task <ILock> GetLockAsync(CancellationToken cancellationToken = default) { return(_lockProvider.AcquireAsync(nameof(CloseInactiveSessionsJob), TimeSpan.FromMinutes(15), new CancellationToken(true))); }
public static async Task <ILock> AcquireAsync(this ILockProvider provider, string name, TimeSpan?lockTimeout = null, TimeSpan?acquireTimeout = null) { using (var cancellationTokenSource = acquireTimeout.ToCancellationTokenSource(TimeSpan.FromSeconds(30))) { return(await provider.AcquireAsync(name, lockTimeout, cancellationTokenSource.Token).AnyContext()); } }
protected override Task <ILock> GetJobLockAsync() { return(_lockProvider.AcquireAsync(nameof(DownloadGeoIPDatabaseJob), TimeSpan.FromHours(2), new CancellationToken(true))); }
protected override Task <ILock> GetLockAsync(CancellationToken cancellationToken = default) { return(_lockProvider.AcquireAsync(nameof(DailySummaryJob), TimeSpan.FromHours(1), new CancellationToken(true))); }
protected override Task <ILock> GetLockAsync(CancellationToken cancellationToken = default(CancellationToken)) { return(_lockProvider.AcquireAsync(nameof(RetentionLimitsJob), TimeSpan.FromHours(2), new CancellationToken(true))); }
private async Task<bool> DoLockedWork(ILockProvider locker) { using ( var l = await locker.AcquireAsync("DoLockedWork", TimeSpan.FromMinutes(1), TimeSpan.Zero).AnyContext()) { if (l == null) return false; Thread.Sleep(200); return true; } }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = new CancellationToken()) { var cacheKey = $"{nameof(RemoveProjectWorkItemHandler)}:{((RemoveProjectWorkItem)workItem).ProjectId}"; return(_lockProvider.AcquireAsync(cacheKey, TimeSpan.FromMinutes(15), new CancellationToken(true))); }
protected override Task <ILock> GetJobLockAsync() { return(_locker.AcquireAsync(nameof(ThrottledJob), acquireTimeout: TimeSpan.Zero)); }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = new CancellationToken()) { string cacheKey = $"{nameof(OrganizationNotificationWorkItemHandler)}:{((OrganizationNotificationWorkItem)workItem).OrganizationId}"; return(_lockProvider.AcquireAsync(cacheKey, TimeSpan.FromMinutes(15), new CancellationToken(true))); }
protected override Task <ILock> GetJobLockAsync() { return(_locker.AcquireAsync(nameof(WithLockingJob), TimeSpan.FromSeconds(1), TimeSpan.Zero)); }
protected override Task <ILock> GetLockAsync(CancellationToken cancellationToken = default) { return(_lockProvider.AcquireAsync(nameof(StackEventCountJob), TimeSpan.FromHours(2), new CancellationToken(true))); }
public override Task <ILock> GetWorkItemLockAsync(object workItem, CancellationToken cancellationToken = new CancellationToken()) { return(_lockProvider.AcquireAsync(nameof(UserMaintenanceWorkItemHandler), TimeSpan.FromMinutes(15), new CancellationToken(true))); }