|
| 1 | +package com.launchdarkly.sdk.server.integrations.reactor; |
| 2 | + |
| 3 | +import com.launchdarkly.sdk.EvaluationDetail; |
| 4 | +import com.launchdarkly.sdk.LDContext; |
| 5 | +import com.launchdarkly.sdk.LDValue; |
| 6 | +import com.launchdarkly.sdk.server.FeatureFlagsState; |
| 7 | +import com.launchdarkly.sdk.server.FlagsStateOption; |
| 8 | +import com.launchdarkly.sdk.server.LDClient; |
| 9 | +import com.launchdarkly.sdk.server.LDConfig; |
| 10 | +import com.launchdarkly.sdk.server.interfaces.BigSegmentStoreStatusProvider; |
| 11 | +import com.launchdarkly.sdk.server.interfaces.DataSourceStatusProvider; |
| 12 | +import com.launchdarkly.sdk.server.interfaces.DataStoreStatusProvider; |
| 13 | +import com.launchdarkly.sdk.server.interfaces.FlagTracker; |
| 14 | + |
| 15 | +import java.util.concurrent.Callable; |
| 16 | + |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | +import reactor.core.scheduler.Scheduler; |
| 19 | + |
| 20 | +/** |
| 21 | + * A thin wrapper of the {@link LDClient} that aims to adapt it to reactive stream programming. |
| 22 | + * |
| 23 | + * Methods that are potentially long running or that use IO have been wrapped to return {@link Mono}s and will be |
| 24 | + * executed on the scheduler provided. Methods that do not have a risk of blocking have not been wrapped and are |
| 25 | + * pass through. |
| 26 | + */ |
| 27 | +public final class LDReactorClient implements LDReactorClientInterface { |
| 28 | + |
| 29 | + private final LDClient wrappedClient; |
| 30 | + private final Scheduler scheduler; |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a client that uses the provided scheduler to execute functionality in a non-blocking manner. |
| 34 | + * |
| 35 | + * @param sdkKey the SDK key for your LaunchDarkly environment |
| 36 | + * @param scheduler that will execute wrapped client methods |
| 37 | + */ |
| 38 | + public LDReactorClient(String sdkKey, Scheduler scheduler) { |
| 39 | + this.wrappedClient = new LDClient(sdkKey); |
| 40 | + this.scheduler = scheduler; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a client that uses the provided scheduler to execute functionality in a non-blocking manner. |
| 45 | + * |
| 46 | + * @param sdkKey the SDK key for your LaunchDarkly environment |
| 47 | + * @param config a client configuration object |
| 48 | + * @param scheduler that will execute wrapped client methods |
| 49 | + */ |
| 50 | + public LDReactorClient(String sdkKey, LDConfig config, Scheduler scheduler) { |
| 51 | + this.wrappedClient = new LDClient(sdkKey, config); |
| 52 | + this.scheduler = scheduler; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean isInitialized() { |
| 57 | + return wrappedClient.isInitialized(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void track(String eventName, LDContext context) { |
| 62 | + wrappedClient.track(eventName, context); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void trackData(String eventName, LDContext context, LDValue data) { |
| 67 | + wrappedClient.trackData(eventName, context, data); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void trackMetric(String eventName, LDContext context, LDValue data, double metricValue) { |
| 72 | + wrappedClient.trackMetric(eventName, context, data, metricValue); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void identify(LDContext context) { |
| 77 | + wrappedClient.identify(context); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Mono<FeatureFlagsState> allFlagsState(LDContext context, FlagsStateOption... options) { |
| 82 | + return Mono.fromCallable(() -> wrappedClient.allFlagsState(context, options)).subscribeOn(this.scheduler); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Mono<Boolean> boolVariation(String featureKey, LDContext context, boolean defaultValue) { |
| 87 | + return Mono.fromCallable(() -> wrappedClient.boolVariation(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Mono<Integer> intVariation(String featureKey, LDContext context, int defaultValue) { |
| 92 | + return Mono.fromCallable(() -> wrappedClient.intVariation(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Mono<Double> doubleVariation(String featureKey, LDContext context, double defaultValue) { |
| 97 | + return Mono.fromCallable(() -> wrappedClient.doubleVariation(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Mono<String> stringVariation(String featureKey, LDContext context, String defaultValue) { |
| 102 | + return Mono.fromCallable(() -> wrappedClient.stringVariation(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Mono<LDValue> jsonValueVariation(String featureKey, LDContext context, LDValue defaultValue) { |
| 107 | + return Mono.fromCallable(() -> wrappedClient.jsonValueVariation(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Mono<EvaluationDetail<Boolean>> boolVariationDetail(String featureKey, LDContext context, boolean defaultValue) { |
| 112 | + return Mono.fromCallable(() -> wrappedClient.boolVariationDetail(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Mono<EvaluationDetail<Integer>> intVariationDetail(String featureKey, LDContext context, int defaultValue) { |
| 117 | + return Mono.fromCallable(() -> wrappedClient.intVariationDetail(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Mono<EvaluationDetail<Double>> doubleVariationDetail(String featureKey, LDContext context, double defaultValue) { |
| 122 | + return Mono.fromCallable(() -> wrappedClient.doubleVariationDetail(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public Mono<EvaluationDetail<String>> stringVariationDetail(String featureKey, LDContext context, String defaultValue) { |
| 127 | + return Mono.fromCallable(() -> wrappedClient.stringVariationDetail(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Mono<EvaluationDetail<LDValue>> jsonValueVariationDetail(String featureKey, LDContext context, LDValue defaultValue) { |
| 132 | + return Mono.fromCallable(() -> wrappedClient.jsonValueVariationDetail(featureKey, context, defaultValue)).subscribeOn(this.scheduler); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean isFlagKnown(String featureKey) { |
| 137 | + return wrappedClient.isFlagKnown(featureKey); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public FlagTracker getFlagTracker() { |
| 142 | + return wrappedClient.getFlagTracker(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public BigSegmentStoreStatusProvider getBigSegmentStoreStatusProvider() { |
| 147 | + return wrappedClient.getBigSegmentStoreStatusProvider(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public DataStoreStatusProvider getDataStoreStatusProvider() { |
| 152 | + return wrappedClient.getDataStoreStatusProvider(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public DataSourceStatusProvider getDataSourceStatusProvider() { |
| 157 | + return wrappedClient.getDataSourceStatusProvider(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public Mono<Void> close() { |
| 162 | + return Mono.fromCallable((Callable<Void>) () -> { |
| 163 | + wrappedClient.close(); |
| 164 | + return null; |
| 165 | + }).subscribeOn(this.scheduler); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void flush() { |
| 170 | + wrappedClient.flush(); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public boolean isOffline() { |
| 175 | + return wrappedClient.isOffline(); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public String secureModeHash(LDContext context) { |
| 180 | + return wrappedClient.secureModeHash(context); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String version() { |
| 185 | + return wrappedClient.version(); |
| 186 | + } |
| 187 | +} |
0 commit comments