コード例 #1
0
        public static LockResult TryLockAll(
            this ImmutableArray <IRedlockInstance> instances,
            ILogger logger,
            string resource,
            string nonce,
            TimeSpan lockTimeToLive,
            IReadOnlyDictionary <string, string>?metadata = null
            )
        {
            if (instances.IsDefaultOrEmpty)
            {
                return(LockResult.Empty);
            }

            var lockedCount       = 0;
            var lockResultBuilder = LockResultBuilder.Start(instances, lockTimeToLive);

            Parallel.ForEach(instances, instance =>
            {
                if (instance.TryLockSafe(logger, resource, nonce, lockTimeToLive, metadata))
                {
                    Interlocked.Increment(ref lockedCount);
                }
            });
            return(lockResultBuilder.End(lockedCount));
        }
コード例 #2
0
        public static LockResult TryExtendAll(
            this ImmutableArray <IRedlockInstance> instances,
            ILogger logger,
            string resource,
            string nonce,
            TimeSpan lockTimeToLive
            )
        {
            if (instances.IsDefaultOrEmpty)
            {
                return(LockResult.Empty);
            }

            var lockedCount       = 0;
            var lockResultBuilder = LockResultBuilder.Start(instances, lockTimeToLive);

            Parallel.ForEach(instances, i =>
            {
                if (i.TryExtendSafe(logger, resource, nonce, lockTimeToLive))
                {
                    Interlocked.Increment(ref lockedCount);
                }
            });
            return(lockResultBuilder.End(lockedCount));
        }
コード例 #3
0
        public static async Task <LockResult> TryExtendAllAsync(
            this ImmutableArray <IRedlockInstance> instances,
            ILogger logger,
            string resource,
            string nonce,
            TimeSpan lockTimeToLive
            )
        {
            if (instances.IsDefaultOrEmpty)
            {
                return(LockResult.Empty);
            }

            var lockResultBuilder = LockResultBuilder.Start(instances, lockTimeToLive);
            var tasks             = instances.Select(
                async x => await x.TryExtendSafeAsync(logger, resource, nonce, lockTimeToLive).ConfigureAwait(false) ? 1 : 0
                );

            return(lockResultBuilder.End((await Task.WhenAll(tasks).ConfigureAwait(false)).Sum()));
        }