Пример #1
0
        /// <summary>
        /// Try Aquire the lock. If success it will return a successful <see cref="LockLeaseResult"/>.
        /// If the lockmanger throws a <seealso cref="LockManagerLockException"/> it will catch that and return a failed <see cref="LockLeaseResult"/>.
        /// In case of failure it will retry sometimes before calling it a day.
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="name"></param>
        /// <param name="retryCount"></param>
        /// <param name="retryAfter"></param>
        /// <returns></returns>
        public static async Task <LockLeaseResult> TryAquireLockWithRetryPolicy(this ILockManager manager, string name, int retryCount = 3, int retryAfter = 3)
        {
            var policy = Policy.HandleResult <LockLeaseResult>(lockLeaseResult => !lockLeaseResult.Ok)
                         .WaitAndRetryAsync(retryCount, retryAttempt => TimeSpan.FromSeconds(retryAfter));

            return(await policy.ExecuteAsync(async() => await manager.TryAquireLock(name)));
        }