-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathScopedLockProvider.cs
More file actions
80 lines (64 loc) · 2.86 KB
/
ScopedLockProvider.cs
File metadata and controls
80 lines (64 loc) · 2.86 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
74
75
76
77
78
79
80
using System;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Resilience;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
namespace Foundatio.Lock;
public class ScopedLockProvider : ILockProvider, IHaveLogger, IHaveLoggerFactory, IHaveTimeProvider, IHaveResiliencePolicyProvider
{
private string _keyPrefix;
private bool _isLocked;
private readonly object _lock = new();
public ScopedLockProvider(ILockProvider lockProvider, string? scope = null)
{
ArgumentNullException.ThrowIfNull(lockProvider);
UnscopedLockProvider = lockProvider;
_isLocked = scope != null;
Scope = !String.IsNullOrWhiteSpace(scope) ? scope.Trim() : null;
_keyPrefix = Scope != null ? String.Concat(Scope, ":") : String.Empty;
}
public ILockProvider UnscopedLockProvider { get; }
public string? Scope { get; private set; }
ILogger IHaveLogger.Logger => UnscopedLockProvider.GetLogger();
ILoggerFactory IHaveLoggerFactory.LoggerFactory => UnscopedLockProvider.GetLoggerFactory();
TimeProvider IHaveTimeProvider.TimeProvider => UnscopedLockProvider.GetTimeProvider();
IResiliencePolicyProvider IHaveResiliencePolicyProvider.ResiliencePolicyProvider => UnscopedLockProvider.GetResiliencePolicyProvider() ?? DefaultResiliencePolicyProvider.Instance;
public void SetScope(string? scope)
{
if (_isLocked)
throw new InvalidOperationException("Scope can't be changed after it has been set");
lock (_lock)
{
if (_isLocked)
throw new InvalidOperationException("Scope can't be changed after it has been set");
_isLocked = true;
Scope = !String.IsNullOrWhiteSpace(scope) ? scope.Trim() : null;
_keyPrefix = Scope != null ? String.Concat(Scope, ":") : String.Empty;
}
}
protected string GetScopedLockProviderKey(string key)
{
return String.Concat(_keyPrefix, key);
}
public Task<ILock?> AcquireAsync(string resource, TimeSpan? timeUntilExpires = null, bool releaseOnDispose = true, CancellationToken cancellationToken = default)
{
return UnscopedLockProvider.AcquireAsync(GetScopedLockProviderKey(resource), timeUntilExpires, releaseOnDispose, cancellationToken);
}
public Task<bool> IsLockedAsync(string resource)
{
return UnscopedLockProvider.IsLockedAsync(GetScopedLockProviderKey(resource));
}
public Task ReleaseAsync(string resource, string lockId)
{
return UnscopedLockProvider.ReleaseAsync(resource, lockId);
}
public Task ReleaseAsync(string resource)
{
return UnscopedLockProvider.ReleaseAsync(resource);
}
public Task RenewAsync(string resource, string lockId, TimeSpan? timeUntilExpires = null)
{
return UnscopedLockProvider.RenewAsync(resource, lockId, timeUntilExpires);
}
}