|
9 | 9 |
|
10 | 10 | namespace Foundatio.Lock; |
11 | 11 |
|
| 12 | +/// <summary> |
| 13 | +/// Provides distributed locking to coordinate access to shared resources across processes. |
| 14 | +/// </summary> |
12 | 15 | public interface ILockProvider |
13 | 16 | { |
| 17 | + /// <summary> |
| 18 | + /// Acquires a lock on the specified resource, waiting until available or cancellation. |
| 19 | + /// </summary> |
| 20 | + /// <param name="resource">The resource identifier to lock. Use consistent naming across processes.</param> |
| 21 | + /// <param name="timeUntilExpires"> |
| 22 | + /// How long the lock is held before automatic release. Defaults to 20 minutes. |
| 23 | + /// For long-running operations, call <see cref="ILock.RenewAsync"/> periodically. |
| 24 | + /// </param> |
| 25 | + /// <param name="releaseOnDispose">If true, the lock is released when disposed.</param> |
| 26 | + /// <param name="cancellationToken">Token to cancel the acquisition attempt.</param> |
| 27 | + /// <returns>An <see cref="ILock"/> representing the acquired lock, or null if acquisition was cancelled.</returns> |
14 | 28 | Task<ILock> AcquireAsync(string resource, TimeSpan? timeUntilExpires = null, bool releaseOnDispose = true, CancellationToken cancellationToken = default); |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Checks whether a resource is currently locked. |
| 32 | + /// </summary> |
| 33 | + /// <param name="resource">The resource identifier to check.</param> |
| 34 | + /// <returns>True if the resource is locked; otherwise, false.</returns> |
15 | 35 | Task<bool> IsLockedAsync(string resource); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Releases a specific lock on a resource. |
| 39 | + /// </summary> |
| 40 | + /// <param name="resource">The resource identifier.</param> |
| 41 | + /// <param name="lockId">The unique identifier of the lock to release.</param> |
16 | 42 | Task ReleaseAsync(string resource, string lockId); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Releases any lock on a resource regardless of lock ID. |
| 46 | + /// Use with caution as this may release locks held by other processes. |
| 47 | + /// </summary> |
| 48 | + /// <param name="resource">The resource identifier.</param> |
17 | 49 | Task ReleaseAsync(string resource); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Extends the expiration time of an existing lock. |
| 53 | + /// </summary> |
| 54 | + /// <param name="resource">The resource identifier.</param> |
| 55 | + /// <param name="lockId">The unique identifier of the lock to renew.</param> |
| 56 | + /// <param name="timeUntilExpires">The new expiration duration from now.</param> |
18 | 57 | Task RenewAsync(string resource, string lockId, TimeSpan? timeUntilExpires = null); |
19 | 58 | } |
20 | 59 |
|
| 60 | +/// <summary> |
| 61 | +/// Represents an acquired lock on a resource. Dispose to release the lock. |
| 62 | +/// </summary> |
21 | 63 | public interface ILock : IAsyncDisposable |
22 | 64 | { |
| 65 | + /// <summary> |
| 66 | + /// Extends the lock expiration to prevent automatic release during long-running operations. |
| 67 | + /// </summary> |
| 68 | + /// <param name="timeUntilExpires">The new expiration duration from now.</param> |
23 | 69 | Task RenewAsync(TimeSpan? timeUntilExpires = null); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Explicitly releases the lock, allowing other processes to acquire it. |
| 73 | + /// </summary> |
24 | 74 | Task ReleaseAsync(); |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Gets the unique identifier for this lock instance. |
| 78 | + /// </summary> |
25 | 79 | string LockId { get; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets the resource identifier this lock is held on. |
| 83 | + /// </summary> |
26 | 84 | string Resource { get; } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Gets the UTC time when this lock was acquired. |
| 88 | + /// </summary> |
27 | 89 | DateTime AcquiredTimeUtc { get; } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Gets the duration spent waiting to acquire this lock. |
| 93 | + /// </summary> |
28 | 94 | TimeSpan TimeWaitedForLock { get; } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Gets the number of times this lock has been renewed. |
| 98 | + /// </summary> |
29 | 99 | int RenewalCount { get; } |
30 | 100 | } |
31 | 101 |
|
|
0 commit comments