-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathPostgresDistributedReaderWriterLock.cs
More file actions
67 lines (58 loc) · 2.87 KB
/
Copy pathPostgresDistributedReaderWriterLock.cs
File metadata and controls
67 lines (58 loc) · 2.87 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
using Medallion.Threading.Internal;
using Medallion.Threading.Internal.Data;
using System.Data;
#if NET7_0_OR_GREATER
using System.Data.Common;
#endif
namespace Medallion.Threading.Postgres;
/// <summary>
/// Implements a distributed lock using Postgres advisory locks
/// (see https://www.postgresql.org/docs/12/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
/// </summary>
public sealed partial class PostgresDistributedReaderWriterLock : IInternalDistributedReaderWriterLock<PostgresDistributedReaderWriterLockHandle>
{
private readonly IDbDistributedLock _internalLock;
/// <summary>
/// Constructs a lock with the given <paramref name="key"/> (effectively the lock name), <paramref name="connectionString"/>,
/// and <paramref name="options"/>
/// </summary>
public PostgresDistributedReaderWriterLock(PostgresAdvisoryLockKey key, string connectionString, Action<PostgresConnectionOptionsBuilder>? options = null)
: this(key, PostgresDistributedLock.CreateInternalLock(key, connectionString, options))
{
}
/// <summary>
/// Constructs a lock with the given <paramref name="key"/> (effectively the lock name) and <paramref name="connection"/>.
/// </summary>
public PostgresDistributedReaderWriterLock(PostgresAdvisoryLockKey key, IDbConnection connection)
: this(key, PostgresDistributedLock.CreateInternalLock(key, connection))
{
}
#if NET7_0_OR_GREATER
/// <summary>
/// Constructs a lock with the given <paramref name="key"/> (effectively the lock name) and <paramref name="dbDataSource"/>,
/// and <paramref name="options"/>.
///
/// Not compatible with connection multiplexing.
/// </summary>
public PostgresDistributedReaderWriterLock(PostgresAdvisoryLockKey key, DbDataSource dbDataSource, Action<PostgresConnectionOptionsBuilder>? options = null)
: this(key, PostgresDistributedLock.CreateInternalLock(key, dbDataSource, options))
{
}
#endif
private PostgresDistributedReaderWriterLock(PostgresAdvisoryLockKey key, IDbDistributedLock internalLock)
{
this.Key = key;
this._internalLock = internalLock;
}
/// <summary>
/// The <see cref="PostgresAdvisoryLockKey"/> that uniquely identifies the lock on the database
/// </summary>
public PostgresAdvisoryLockKey Key { get; }
string IDistributedReaderWriterLock.Name => this.Key.ToString();
ValueTask<PostgresDistributedReaderWriterLockHandle?> IInternalDistributedReaderWriterLock<PostgresDistributedReaderWriterLockHandle>.InternalTryAcquireAsync(
TimeoutValue timeout,
CancellationToken cancellationToken,
bool isWrite) =>
this._internalLock.TryAcquireAsync(timeout, isWrite ? PostgresAdvisoryLock.ExclusiveLock : PostgresAdvisoryLock.SharedLock, cancellationToken, contextHandle: null)
.Wrap(h => new PostgresDistributedReaderWriterLockHandle(h));
}