-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJSONFileClient.cs
More file actions
102 lines (89 loc) · 4.4 KB
/
JSONFileClient.cs
File metadata and controls
102 lines (89 loc) · 4.4 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Splitio.Domain;
using Splitio.Services.Cache.Classes;
using Splitio.Services.Cache.Interfaces;
using Splitio.Services.EngineEvaluator;
using Splitio.Services.Events.Interfaces;
using Splitio.Services.Impressions.Classes;
using Splitio.Services.Impressions.Interfaces;
using Splitio.Services.InputValidation.Interfaces;
using Splitio.Services.Parsing;
using Splitio.Services.SegmentFetcher.Classes;
using Splitio.Services.Shared.Classes;
using Splitio.Services.SplitFetcher.Classes;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Splitio.Services.Client.Classes
{
public class JSONFileClient : SplitClient
{
private readonly IFeatureFlagCache _featureFlagCache;
private readonly ISegmentCache _segmentCache;
public JSONFileClient(string splitsFilePath,
string segmentsFilePath,
ConfigurationOptions config,
ISegmentCache segmentCacheInstance = null,
IFeatureFlagCache featureFlagCacheInstance = null,
IImpressionsLog impressionsLog = null,
bool isLabelsEnabled = true,
IEventsLog eventsLog = null,
ITrafficTypeValidator trafficTypeValidator = null,
IImpressionsManager impressionsManager = null,
IRuleBasedSegmentCache ruleBasedSegmentCache = null
) : base("localhost")
{
_segmentCache = segmentCacheInstance ?? new InMemorySegmentCache(new ConcurrentDictionary<string, Segment>(), _internalEventsTask);
var rbsCache = ruleBasedSegmentCache ?? new InMemoryRuleBasedSegmentCache(new ConcurrentDictionary<string, RuleBasedSegment>(), _internalEventsTask);
var segmentFetcher = new JSONFileSegmentFetcher(segmentsFilePath, _segmentCache);
var splitChangeFetcher = new JSONFileSplitChangeFetcher(splitsFilePath);
var task = splitChangeFetcher.FetchAsync(new FetchOptions());
task.Wait();
var result = task.Result;
var parsedSplits = new ConcurrentDictionary<string, ParsedSplit>();
_splitParser = new FeatureFlagParser(_segmentCache, segmentFetcher);
foreach (var split in result.FeatureFlags.Data)
{
parsedSplits.TryAdd(split.name, _splitParser.Parse(split, rbsCache));
}
BuildEventsManager();
BuildStatusAndTaskManager();
BuildFallbackCalculator(config.FallbackTreatments);
BuildFlagSetsFilter(new HashSet<string>());
_featureFlagCache = featureFlagCacheInstance ?? new InMemorySplitCache(new ConcurrentDictionary<string, ParsedSplit>(parsedSplits), _flagSetsFilter, _internalEventsTask);
_impressionsLog = impressionsLog;
_eventsLog = eventsLog;
_trafficTypeValidator = trafficTypeValidator;
_blockUntilReadyService = new NoopBlockUntilReadyService();
_manager = new SplitManager(_featureFlagCache, _blockUntilReadyService);
_evaluator = new Evaluator.Evaluator(_featureFlagCache, new Splitter(), null, _fallbackTreatmentCalculator);
_uniqueKeysTracker = new NoopUniqueKeysTracker();
_impressionsCounter = new NoopImpressionsCounter();
_impressionsObserver = new NoopImpressionsObserver();
_impressionsManager = impressionsManager ?? new ImpressionsManager(impressionsLog, null, _impressionsCounter, false, ImpressionsMode.Debug, null, _tasksManager, _uniqueKeysTracker, _impressionsObserver, isLabelsEnabled, _propertiesValidator);
BuildClientExtension();
}
#region Public Methods
public void RemoveSplitFromCache(string splitName)
{
_featureFlagCache.Update(new List<ParsedSplit>(), new List<string> { splitName }, -1);
}
public void RemoveKeyFromSegmentCache(string segmentName, List<string> keys)
{
_segmentCache.RemoveFromSegment(segmentName, keys);
}
public override void Destroy()
{
if (_statusManager.IsDestroyed()) return;
_factoryInstantiationsService.Decrease(ApiKey);
_statusManager.SetDestroy();
_featureFlagCache.Clear();
_segmentCache.Clear();
}
public override Task DestroyAsync()
{
Destroy();
return Task.FromResult(0);
}
#endregion
}
}