This repository was archived by the owner on Mar 15, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathPostgresDistributedLock.cs
More file actions
73 lines (61 loc) · 3.42 KB
/
Copy pathPostgresDistributedLock.cs
File metadata and controls
73 lines (61 loc) · 3.42 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
using Medallion.Threading.Internal;
using Medallion.Threading.Internal.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 PostgresDistributedLock : IInternalDistributedLock<PostgresDistributedLockHandle>
{
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 PostgresDistributedLock(PostgresAdvisoryLockKey key, string connectionString, Action<PostgresConnectionOptionsBuilder>? options = null)
: this(key, CreateInternalLock(key, connectionString, options))
{
}
/// <summary>
/// Constructs a lock with the given <paramref name="key"/> (effectively the lock name) and <paramref name="connection"/>.
/// </summary>
public PostgresDistributedLock(PostgresAdvisoryLockKey key, IDbConnection connection)
: this(key, CreateInternalLock(key, connection))
{
}
private PostgresDistributedLock(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 IDistributedLock.Name => this.Key.ToString();
ValueTask<PostgresDistributedLockHandle?> IInternalDistributedLock<PostgresDistributedLockHandle>.InternalTryAcquireAsync(TimeoutValue timeout, CancellationToken cancellationToken) =>
this._internalLock.TryAcquireAsync(timeout, PostgresAdvisoryLock.ExclusiveLock, cancellationToken, contextHandle: null).Wrap(h => new PostgresDistributedLockHandle(h));
internal static IDbDistributedLock CreateInternalLock(PostgresAdvisoryLockKey key, string connectionString, Action<PostgresConnectionOptionsBuilder>? options)
{
if (connectionString == null) { throw new ArgumentNullException(nameof(connectionString)); }
var (keepaliveCadence, useMultiplexing) = PostgresConnectionOptionsBuilder.GetOptions(options);
if (useMultiplexing)
{
return new OptimisticConnectionMultiplexingDbDistributedLock(key.ToString(), connectionString, PostgresMultiplexedConnectionLockPool.Instance, keepaliveCadence);
}
return new DedicatedConnectionOrTransactionDbDistributedLock(key.ToString(), () => new PostgresDatabaseConnection(connectionString), useTransaction: false, keepaliveCadence);
}
internal static IDbDistributedLock CreateInternalLock(PostgresAdvisoryLockKey key, IDbConnection connection)
{
if (connection == null) { throw new ArgumentNullException(nameof(connection)); }
return new DedicatedConnectionOrTransactionDbDistributedLock(key.ToString(), () => new PostgresDatabaseConnection(connection));
}
}
}