-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3979d3e
commit a4c595a
Showing
4 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Weasel.Core; | ||
|
||
internal interface IAdvisoryLock: IAsyncDisposable | ||
{ | ||
bool HasLock(int lockId); | ||
Task<bool> TryAttainLockAsync(int lockId, CancellationToken token); | ||
Task ReleaseLockAsync(int lockId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Data; | ||
using JasperFx.Core; | ||
using Microsoft.Extensions.Logging; | ||
using Npgsql; | ||
using Weasel.Core; | ||
using Weasel.Core.Migrations; | ||
|
||
namespace Weasel.Postgresql; | ||
|
||
internal class AdvisoryLock : IAdvisoryLock | ||
{ | ||
private readonly PostgresqlDatabase _database; | ||
private readonly ILogger _logger; | ||
private NpgsqlConnection _conn; | ||
private readonly List<int> _locks = new(); | ||
|
||
public AdvisoryLock(PostgresqlDatabase database, ILogger logger) | ||
{ | ||
_database = database; | ||
_logger = logger; | ||
} | ||
|
||
public bool HasLock(int lockId) | ||
{ | ||
return _conn is not { State: ConnectionState.Closed } && _locks.Contains(lockId); | ||
} | ||
|
||
public async Task<bool> TryAttainLockAsync(int lockId, CancellationToken token) | ||
{ | ||
if (_conn == null) | ||
{ | ||
_conn = _database.CreateConnection(); | ||
await _conn.OpenAsync(token).ConfigureAwait(false); | ||
} | ||
|
||
if (_conn.State == ConnectionState.Closed) | ||
{ | ||
try | ||
{ | ||
await _conn.DisposeAsync().ConfigureAwait(false); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error trying to clean up and restart an advisory lock connection"); | ||
} | ||
finally | ||
{ | ||
_conn = null; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
|
||
var attained = await _conn.TryGetGlobalLock(lockId, cancellation: token).ConfigureAwait(false); | ||
if (attained == AttainLockResult.Success) | ||
{ | ||
_locks.Add(lockId); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public async Task ReleaseLockAsync(int lockId) | ||
{ | ||
if (!_locks.Contains(lockId)) return; | ||
|
||
if (_conn == null || _conn.State == ConnectionState.Closed) | ||
{ | ||
_locks.Remove(lockId); | ||
return; | ||
} | ||
|
||
var cancellation = new CancellationTokenSource(); | ||
cancellation.CancelAfter(1.Seconds()); | ||
|
||
await _conn.ReleaseGlobalLock(lockId, cancellation: cancellation.Token).ConfigureAwait(false); | ||
_locks.Remove(lockId); | ||
|
||
if (!_locks.Any()) | ||
{ | ||
await _conn.CloseAsync().ConfigureAwait(false); | ||
await _conn.DisposeAsync().ConfigureAwait(false); | ||
_conn = null; | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (_conn == null) return; | ||
|
||
try | ||
{ | ||
foreach (var i in _locks) | ||
{ | ||
await _conn.ReleaseGlobalLock(i, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
await _conn.CloseAsync().ConfigureAwait(false); | ||
await _conn.DisposeAsync().ConfigureAwait(false); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error trying to dispose of advisory locks for database {Identifier}", | ||
_database.Identifier); | ||
} | ||
finally | ||
{ | ||
await _conn.DisposeAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters