-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathFeatureRepositoryImpl.java
More file actions
106 lines (93 loc) · 4.04 KB
/
Copy pathFeatureRepositoryImpl.java
File metadata and controls
106 lines (93 loc) · 4.04 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
103
104
105
106
package io.getunleash.repository;
import io.getunleash.FeatureDefinition;
import io.getunleash.UnleashContext;
import io.getunleash.UnleashException;
import io.getunleash.engine.FlatResponse;
import io.getunleash.engine.UnleashEngine;
import io.getunleash.engine.VariantDef;
import io.getunleash.engine.YggdrasilInvalidInputException;
import io.getunleash.event.EventDispatcher;
import io.getunleash.event.GatedEventEmitter;
import io.getunleash.util.UnleashConfig;
import io.getunleash.util.UnleashScheduledExecutor;
import java.util.Optional;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FeatureRepositoryImpl implements FeatureRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureRepositoryImpl.class);
private final BackupHandler featureBackupHandler;
private final ToggleBootstrapProvider bootstrapper;
private final FetchWorker fetcher;
private final GatedEventEmitter eventDispatcher;
private final UnleashEngine engine;
public FeatureRepositoryImpl(UnleashConfig unleashConfig, UnleashEngine engine) {
this(unleashConfig, new FeatureBackupHandlerFile(unleashConfig), engine);
}
FeatureRepositoryImpl(
UnleashConfig unleashConfig, BackupHandler featureBackupHandler, UnleashEngine engine) {
GatedEventEmitter readyOnceGate = new GatedEventEmitter(new EventDispatcher(unleashConfig));
this.featureBackupHandler = featureBackupHandler;
this.engine = engine;
this.eventDispatcher = readyOnceGate;
this.fetcher = new AdaptiveFetcher(unleashConfig, featureBackupHandler, engine);
this.bootstrapper = unleashConfig.getToggleBootstrapProvider();
this.initCollections(unleashConfig.getScheduledExecutor());
}
FeatureRepositoryImpl(
UnleashConfig unleashConfig,
BackupHandler featureBackupHandler,
UnleashEngine engine,
FetchWorker fetcher,
ToggleBootstrapProvider bootstrapHandler,
EventDispatcher eventDispatcher) {
GatedEventEmitter readyOnceGate = new GatedEventEmitter(eventDispatcher);
this.featureBackupHandler = featureBackupHandler;
this.engine = engine;
this.bootstrapper = bootstrapHandler;
this.eventDispatcher = readyOnceGate;
this.fetcher = fetcher;
this.initCollections(unleashConfig.getScheduledExecutor());
}
private void initCollections(UnleashScheduledExecutor executor) {
Optional<String> features = this.featureBackupHandler.read();
if (features.isEmpty() && this.bootstrapper != null) {
features = this.bootstrapper.read();
}
if (features.isPresent()) {
try {
this.engine.takeState(features.get());
} catch (YggdrasilInvalidInputException e) {
LOGGER.error("Error when initializing feature toggles", e);
eventDispatcher.error(new UnleashException("Failed to read backup file:", e));
}
}
fetcher.start();
}
@Override
public FlatResponse<Boolean> isEnabled(String toggleName, UnleashContext context) {
try {
return this.engine.isEnabled(toggleName, YggdrasilAdapters.adapt(context));
} catch (YggdrasilInvalidInputException e) {
LOGGER.error("Error when checking feature toggle {}", toggleName, e);
return null;
}
}
@Override
public FlatResponse<VariantDef> getVariant(String toggleName, UnleashContext context) {
try {
return this.engine.getVariant(toggleName, YggdrasilAdapters.adapt(context));
} catch (YggdrasilInvalidInputException e) {
LOGGER.error("Error when checking feature toggle {}", toggleName, e);
return null;
}
}
@Override
public Stream<FeatureDefinition> listKnownToggles() {
return this.engine.listKnownToggles().stream().map(FeatureDefinition::new);
}
@Override
public void shutdown() {
this.fetcher.stop();
}
}