-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding AdvisoryLock for both Postgresql and Sql Server. Bumps to 7.7.0
- Loading branch information
1 parent
a5604ea
commit 1fca939
Showing
6 changed files
with
168 additions
and
6 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
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
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,115 @@ | ||
using System.Data; | ||
using JasperFx.Core; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Logging; | ||
using Weasel.Core; | ||
|
||
namespace Weasel.SqlServer; | ||
|
||
internal class AdvisoryLock : IAdvisoryLock | ||
{ | ||
private readonly Func<SqlConnection> _source; | ||
private readonly ILogger _logger; | ||
private readonly string _databaseName; | ||
private SqlConnection _conn; | ||
private readonly List<int> _locks = new(); | ||
|
||
public AdvisoryLock(Func<SqlConnection> source, ILogger logger, string databaseName) | ||
{ | ||
_source = source; | ||
_logger = logger; | ||
_databaseName = databaseName; | ||
} | ||
|
||
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 = _source(); | ||
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.ToString(), cancellation: token).ConfigureAwait(false); | ||
if (attained) | ||
{ | ||
_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.ToString(), 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.ToString(), 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}", | ||
_databaseName); | ||
} | ||
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