|
| 1 | +using Medallion.Threading.Internal; |
| 2 | +using System.Data; |
| 3 | + |
| 4 | +namespace Medallion.Threading.Postgres; |
| 5 | + |
| 6 | +public partial class PostgresDistributedLock |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Attempts to acquire a transaction-scoped advisory lock synchronously with an externally owned transaction. Usage: |
| 10 | + /// <code> |
| 11 | + /// var transaction = /* create a DB transaction */ |
| 12 | + /// |
| 13 | + /// var isLockAcquired = myLock.TryAcquireWithTransaction(..., transaction, ...) |
| 14 | + /// |
| 15 | + /// if (isLockAcquired != null) |
| 16 | + /// { |
| 17 | + /// /* we have the lock! */ |
| 18 | + /// |
| 19 | + /// // Commit or Rollback the transaction, which in turn will release the lock |
| 20 | + /// } |
| 21 | + /// </code> |
| 22 | + /// |
| 23 | + /// NOTE: The owner of the transaction is the responsible party for it - the owner must commit or rollback the transaction in order to release the acquired lock. |
| 24 | + /// </summary> |
| 25 | + /// <param name="key">The postgres advisory lock key which will be used to acquire the lock.</param> |
| 26 | + /// <param name="transaction">The externally owned transaction which will be used to acquire the lock. The owner of the transaction must commit or rollback it for the lock to be released.</param> |
| 27 | + /// <param name="timeout">How long to wait before giving up on the acquisition attempt. Defaults to 0.</param> |
| 28 | + /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> |
| 29 | + /// <returns>Whether the lock has been acquired</returns> |
| 30 | + public static bool TryAcquireWithTransaction(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan timeout = default, CancellationToken cancellationToken = default) => |
| 31 | + SyncViaAsync.Run(state => TryAcquireWithTransactionAsyncInternal(state.key, state.transaction, state.timeout, state.cancellationToken), (key, transaction, timeout, cancellationToken)); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Acquires a transaction-scoped advisory lock synchronously, failing with <see cref="TimeoutException"/> if the attempt times out. Usage: |
| 35 | + /// <code> |
| 36 | + /// var transaction = /* create a DB transaction */ |
| 37 | + /// |
| 38 | + /// myLock.AcquireWithTransaction(..., transaction, ...) |
| 39 | + /// |
| 40 | + /// /* we have the lock! */ |
| 41 | + /// |
| 42 | + /// // Commit or Rollback the transaction, which in turn will release the lock |
| 43 | + /// </code> |
| 44 | + /// |
| 45 | + /// NOTE: The owner of the transaction is the responsible party for it - the owner must commit or rollback the transaction in order to release the acquired lock. |
| 46 | + /// </summary> |
| 47 | + /// <param name="key">The postgres advisory lock key which will be used to acquire the lock.</param> |
| 48 | + /// <param name="transaction">The externally owned transaction which will be used to acquire the lock. The owner of the transaction must commit or rollback it for the lock to be released.</param> |
| 49 | + /// <param name="timeout">How long to wait before giving up on the acquisition attempt. Defaults to <see cref="Timeout.InfiniteTimeSpan"/></param> |
| 50 | + /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> |
| 51 | + public static void AcquireWithTransaction(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => |
| 52 | + SyncViaAsync.Run(state => AcquireWithTransactionAsyncInternal(state.key, state.transaction, state.timeout, state.cancellationToken), (key, transaction, timeout, cancellationToken)); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Attempts to acquire a transaction-scoped advisory lock asynchronously with an externally owned transaction. Usage: |
| 56 | + /// <code> |
| 57 | + /// var transaction = /* create a DB transaction */ |
| 58 | + /// |
| 59 | + /// var isLockAcquired = await myLock.TryAcquireWithTransactionAsync(..., transaction, ...) |
| 60 | + /// |
| 61 | + /// if (isLockAcquired != null) |
| 62 | + /// { |
| 63 | + /// /* we have the lock! */ |
| 64 | + /// |
| 65 | + /// // Commit or Rollback the transaction, which in turn will release the lock |
| 66 | + /// } |
| 67 | + /// </code> |
| 68 | + /// |
| 69 | + /// NOTE: The owner of the transaction is the responsible party for it - the owner must commit or rollback the transaction in order to release the acquired lock. |
| 70 | + /// </summary> |
| 71 | + /// <param name="key">The postgres advisory lock key which will be used to acquire the lock.</param> |
| 72 | + /// <param name="transaction">The externally owned transaction which will be used to acquire the lock. The owner of the transaction must commit or rollback it for the lock to be released.</param> |
| 73 | + /// <param name="timeout">How long to wait before giving up on the acquisition attempt. Defaults to 0.</param> |
| 74 | + /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> |
| 75 | + /// <returns>Whether the lock has been acquired</returns> |
| 76 | + public static ValueTask<bool> TryAcquireWithTransactionAsync(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan timeout = default, CancellationToken cancellationToken = default) => |
| 77 | + TryAcquireWithTransactionAsyncInternal(key, transaction, timeout, cancellationToken); |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Acquires a transaction-scoped advisory lock asynchronously, failing with <see cref="TimeoutException"/> if the attempt times out. Usage: |
| 81 | + /// <code> |
| 82 | + /// var transaction = /* create a DB transaction */ |
| 83 | + /// |
| 84 | + /// await myLock.AcquireWithTransaction(..., transaction, ...) |
| 85 | + /// |
| 86 | + /// /* we have the lock! */ |
| 87 | + /// |
| 88 | + /// // Commit or Rollback the transaction, which in turn will release the lock |
| 89 | + /// </code> |
| 90 | + /// |
| 91 | + /// NOTE: The owner of the transaction is the responsible party for it - the owner must commit or rollback the transaction in order to release the acquired lock. |
| 92 | + /// </summary> |
| 93 | + /// <param name="key">The postgres advisory lock key which will be used to acquire the lock.</param> |
| 94 | + /// <param name="transaction">The externally owned transaction which will be used to acquire the lock. The owner of the transaction must commit or rollback it for the lock to be released.</param> |
| 95 | + /// <param name="timeout">How long to wait before giving up on the acquisition attempt. Defaults to <see cref="Timeout.InfiniteTimeSpan"/></param> |
| 96 | + /// <param name="cancellationToken">Specifies a token by which the wait can be canceled</param> |
| 97 | + public static ValueTask AcquireWithTransactionAsync(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan? timeout = null, CancellationToken cancellationToken = default) => |
| 98 | + AcquireWithTransactionAsyncInternal(key, transaction, timeout, cancellationToken); |
| 99 | + |
| 100 | + internal static ValueTask<bool> TryAcquireWithTransactionAsyncInternal(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan timeout, CancellationToken cancellationToken) |
| 101 | + { |
| 102 | + if (key == null) { throw new ArgumentNullException(nameof(key)); } |
| 103 | + if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } |
| 104 | + |
| 105 | + return TryAcquireAsync(); |
| 106 | + |
| 107 | + async ValueTask<bool> TryAcquireAsync() |
| 108 | + { |
| 109 | + var connection = new PostgresDatabaseConnection(transaction); |
| 110 | + |
| 111 | + await using (connection.ConfigureAwait(false)) |
| 112 | + { |
| 113 | + var lockAcquiredCookie = await PostgresAdvisoryLock.ExclusiveLock.TryAcquireAsync(connection, key.ToString(), timeout, cancellationToken).ConfigureAwait(false); |
| 114 | + |
| 115 | + return lockAcquiredCookie != null; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + internal static ValueTask AcquireWithTransactionAsyncInternal(PostgresAdvisoryLockKey key, IDbTransaction transaction, TimeSpan? timeout, CancellationToken cancellationToken) |
| 121 | + { |
| 122 | + if (key == null) { throw new ArgumentNullException(nameof(key)); } |
| 123 | + if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } |
| 124 | + |
| 125 | + return AcquireAsync(); |
| 126 | + |
| 127 | + async ValueTask AcquireAsync() |
| 128 | + { |
| 129 | + var connection = new PostgresDatabaseConnection(transaction); |
| 130 | + |
| 131 | + await using (connection.ConfigureAwait(false)) |
| 132 | + { |
| 133 | + await PostgresAdvisoryLock.ExclusiveLock.TryAcquireAsync(connection, key.ToString(), timeout, cancellationToken).ThrowTimeoutIfNull().ConfigureAwait(false); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments