-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolicyBucket.cs
50 lines (39 loc) · 1.59 KB
/
PolicyBucket.cs
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
using Bet.Extensions.Resilience.Abstractions.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
using Polly.Registry;
namespace Bet.Extensions.Resilience.Abstractions;
public class PolicyBucket<TPolicy, TOptions> : IPolicyBucketLabel
where TPolicy : IsPolicy
where TOptions : PolicyOptions
{
private readonly IOptionsFactory<PolicyBucketOptions<TOptions>> _optionsFactory;
private readonly IOptionsMonitor<TOptions> _optionsMonitor;
private readonly Dictionary<string, PolicyLoader<TPolicy, TOptions>> _policies;
private readonly IPolicyRegistry<string> _policyRegistry;
private readonly ILoggerFactory _loggerFactory;
public PolicyBucket(
IOptionsFactory<PolicyBucketOptions<TOptions>> optionsFactory,
IOptionsMonitor<TOptions> optionsMonitor,
IPolicyRegistry<string> policyRegistry,
ILoggerFactory loggerFactory)
{
_optionsFactory = optionsFactory;
_optionsMonitor = optionsMonitor;
_policies = new Dictionary<string, PolicyLoader<TPolicy, TOptions>>();
_policyRegistry = policyRegistry;
_loggerFactory = loggerFactory;
}
public IsPolicy GetPolicy(string policyName)
{
if (_policies.ContainsKey(policyName))
{
return _policies[policyName].GetPolicy();
}
var options = _optionsFactory.Create(policyName);
var policy = new PolicyLoader<TPolicy, TOptions>(options, _optionsMonitor, _policyRegistry, _loggerFactory);
_policies.Add(policyName, policy);
return policy.GetPolicy();
}
}