-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathDistributedLockProviderExtensions.cs
More file actions
75 lines (61 loc) · 5.2 KB
/
Copy pathDistributedLockProviderExtensions.cs
File metadata and controls
75 lines (61 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// AUTO-GENERATED
using Medallion.Threading.Internal;
namespace Medallion.Threading;
/// <summary>
/// Productivity helper methods for <see cref="IDistributedLockProvider" />
/// </summary>
public static class DistributedLockProviderExtensions
{
# region Single Lock Methods
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> and then
/// <see cref="IDistributedLock.TryAcquire(TimeSpan, CancellationToken)" />.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireLock(this IDistributedLockProvider provider, string name, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateLock(name).TryAcquire(timeout, cancellationToken);
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> and then
/// <see cref="IDistributedLock.Acquire(TimeSpan?, CancellationToken)" />.
/// </summary>
public static IDistributedSynchronizationHandle AcquireLock(this IDistributedLockProvider provider, string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateLock(name).Acquire(timeout, cancellationToken);
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> and then
/// <see cref="IDistributedLock.TryAcquireAsync(TimeSpan, CancellationToken)" />.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireLockAsync(this IDistributedLockProvider provider, string name, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateLock(name).TryAcquireAsync(timeout, cancellationToken);
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> and then
/// <see cref="IDistributedLock.AcquireAsync(TimeSpan?, CancellationToken)" />.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireLockAsync(this IDistributedLockProvider provider, string name, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
(provider ?? throw new ArgumentNullException(nameof(provider))).CreateLock(name).AcquireAsync(timeout, cancellationToken);
# endregion
# region Composite Lock Methods
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.TryAcquire(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle? TryAcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
SyncViaAsync.Run(static s => s.provider.TryAcquireAllLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken));
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.Acquire(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static IDistributedSynchronizationHandle AcquireAllLocks(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
SyncViaAsync.Run(static s => s.provider.AcquireAllLocksAsync(s.names, s.timeout, s.cancellationToken), (provider, names, timeout, cancellationToken));
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.TryAcquireAsync(TimeSpan, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle?> TryAcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan timeout = default, CancellationToken cancellationToken = default) =>
provider.TryAcquireAllLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrDefault();
/// <summary>
/// Equivalent to calling <see cref="IDistributedLockProvider.CreateLock(string)" /> for each name in <paramref name="names" /> and then
/// <see cref="IDistributedLock.AcquireAsync(TimeSpan?, CancellationToken)" /> on each created instance, combining the results into a composite handle.
/// </summary>
public static ValueTask<IDistributedSynchronizationHandle> AcquireAllLocksAsync(this IDistributedLockProvider provider, IReadOnlyList<string> names, TimeSpan? timeout = null, CancellationToken cancellationToken = default) =>
provider.TryAcquireAllLocksInternalAsync(names, timeout, cancellationToken).GetHandleOrTimeout();
# endregion
}