Skip to content

Commit f00cee9

Browse files
committed
chore: wait for SSE client termination before completing client close future
Add some javadoc
1 parent 92d32f6 commit f00cee9

4 files changed

Lines changed: 81 additions & 9 deletions

File tree

src/main/java/fr/maif/IzanamiClient.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,22 @@ public IzanamiClient(
3535
this(connectionInformation, errorStrategy, cacheConfiguration, httpClient, duration, idsToPreload, Optional.empty());
3636
}
3737

38+
/**
39+
* Constructor
40+
* @param connectionInformation information about remote Izanami instance
41+
* @param errorStrategy default error strategy to use in case client fails to fetch remote Izanami. This can be overrided at request level.
42+
* @param cacheConfiguration cache configuration to use
43+
* @param httpClient httpClient to use
44+
* @param callTimeout timeout for remote instance http calls
45+
* @param idsToPreload flag ids to preload, preloading id prevent from payin the cost of querying remote Izanami first time flags are needed
46+
* @param castStrategy default strategy to use to cast non-boolean values in boolean when needed. Possible values are STRICT (trying to cast non boolean value to boolean value will fail) and LAX (empty string, numeric 0 and null are false, everything else is true).
47+
*/
3848
public IzanamiClient(
3949
IzanamiConnectionInformation connectionInformation,
4050
Optional<FeatureClientErrorStrategy> errorStrategy,
4151
Optional<FeatureCacheConfiguration> cacheConfiguration,
4252
Optional<IzanamiHttpClient> httpClient,
43-
Optional<Duration> duration,
53+
Optional<Duration> callTimeout,
4454
Set<String> idsToPreload,
4555
Optional<BooleanCastStrategy> castStrategy
4656
) {
@@ -49,7 +59,7 @@ public IzanamiClient(
4959
errorStrategy.orElseGet(FeatureClientErrorStrategy::nullValueStrategy),
5060
cacheConfiguration.orElseGet(() -> FeatureCacheConfiguration.newBuilder().enabled(false).build()),
5161
httpClient.orElseGet(IzanamiHttpClient.DefaultIzanamiHttpClient::new),
52-
duration.orElse(Duration.ofSeconds(10L)),
62+
callTimeout.orElse(Duration.ofSeconds(10L)),
5363
castStrategy.orElse(BooleanCastStrategy.LAX)
5464
);
5565

@@ -77,6 +87,10 @@ public IzanamiClient(
7787
}
7888
}
7989

90+
/**
91+
* Close underlying SSE client if SSE client is used, otherwise do nothing.
92+
* @return a CompletableFuture that complete when SSE client is closed, or immediately if there is no SSE client
93+
*/
8094
public CompletableFuture<Void> close() {
8195
if(this.featureService instanceof SSEFeatureService) {
8296
return ((SSEFeatureService)this.featureService).disconnect();
@@ -116,22 +130,52 @@ public CompletableFuture<Boolean> checkFeatureActivation(SingleFeatureRequest re
116130
return featureService.featureStates(request);
117131
}
118132

133+
/**
134+
* Retrieve string value of the flag given request
135+
* @param request request to match
136+
* @return a CompletableFuture that will resolve with requested feature value.
137+
* If feature does not have a string value error strategy will be used to determine value to return.
138+
*/
119139
public CompletableFuture<String> stringValue(SingleFeatureRequest request) {
120140
return featureService.stringFeatureValue(request);
121141
}
122142

143+
/**
144+
* Retrieve number value of the flag given request
145+
* @param request request to match
146+
* @return a CompletableFuture that will resolve with requested feature value.
147+
* If feature does not have a number value error strategy will be used to determine value to return.
148+
*/
123149
public CompletableFuture<BigDecimal> numberValue(SingleFeatureRequest request) {
124150
return featureService.numberFeatureValue(request);
125151
}
126152

153+
/**
154+
* Retrieve boolean value of the flag given request
155+
* @param request request to match
156+
* @return a CompletableFuture that will resolve with requested feature value. If feature does not have a boolean value:
157+
* <ul>
158+
* <li>if a LAX BooleanCastStrategy was specified, value will be casted to boolean (empty string, numeric 0 and null are false, everything else is true).</li>
159+
* <li>if STRICT BooleanCastStrategy was specified (or if no strategy was specified) error strategy will be used to determine value to return.</li>
160+
* </ul>
161+
*/
127162
public CompletableFuture<Boolean> booleanValue(SingleFeatureRequest request) {
128163
return featureService.booleanFeatureValue(request);
129164
}
130165

166+
/**
167+
* Return multiple feature values.
168+
* @param request feature request
169+
* @return a completable future containing feature values wrapped inside a {@see fr.maif.features.results.IzanamiResult}
170+
*/
131171
public CompletableFuture<IzanamiResult> featureValues(FeatureRequest request) {
132172
return featureService.featureValues(request);
133173
}
134174

175+
/**
176+
* Indicate when client is loaded. A loaded client has fetch ids to preload (if provided). If no ids were provided, client is ready immediately after its instantiation.
177+
* @return a CompletableFuture that resolve when client has loaded id to preload (if any).
178+
*/
135179
public CompletableFuture<Void> isLoaded() {
136180
return loader;
137181
}
@@ -211,7 +255,8 @@ public IzanamiClientBuilder withPreloadedFeatures(String... ids) {
211255
}
212256

213257
/**
214-
* Specify boolean cast strategy to use for this client.
258+
* Default strategy to use to cast non-boolean values in boolean when needed.
259+
* Possible values are STRICT (trying to cast non-boolean value to boolean value will fail) and LAX (empty string, numeric 0 and null are false, everything else is true).
215260
* This strategy can be overridden both at query and query feature levels.
216261
* @param strategy boolean cast strategy to use
217262
* @return updated builder

src/main/java/fr/maif/features/results/IzanamiResult.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.math.BigDecimal;
88
import java.util.Map;
99

10+
/**
11+
* Represent result of multiple flag evaluation.
12+
*/
1013
public class IzanamiResult {
1114
public final Map<String, Result> results;
1215
private final BooleanCastStrategy castStrategy;
@@ -18,14 +21,34 @@ public IzanamiResult(Map<String, Result> results, BooleanCastStrategy castStrate
1821
this.defaultStrategy = defaultStrategy;
1922
}
2023

24+
/**
25+
* Retrieve string value of the flag with the given id
26+
* @param feature feature id
27+
* @return string value of given flag. If feature does not have a string value error strategy will be used to determine value to return.
28+
*/
2129
public String stringValue(String feature) {
2230
return results.getOrDefault(feature, new Error(defaultStrategy, new IzanamiError("This feature hasn't been requested"))).stringValue();
2331
}
2432

33+
34+
/**
35+
* Retrieve boolean value of the flag with given id
36+
* @param feature feature id
37+
* @return boolean value of given flag. If feature does not have a boolean value:
38+
* <ul>
39+
* <li>if a LAX BooleanCastStrategy was specified, value will be casted to boolean (empty string, numeric 0 and null are false, everything else is true).</li>
40+
* <li>if STRICT BooleanCastStrategy was specified (or if no strategy was specified) error strategy will be used to determine value to return.</li>
41+
* </ul>
42+
*/
2543
public Boolean booleanValue(String feature) {
2644
return results.getOrDefault(feature, new Error(defaultStrategy, new IzanamiError("This feature hasn't been requested"))).booleanValue(castStrategy);
2745
}
2846

47+
/**
48+
* Retrieve number value of the flag with the given id
49+
* @param feature feature id
50+
* @return number value of given flag. If feature does not have a number value error strategy will be used to determine value to return.
51+
*/
2952
public BigDecimal numberValue(String feature) {
3053
return results.getOrDefault(feature, new Error(defaultStrategy, new IzanamiError("This feature hasn't been requested"))).numberValue();
3154
}

src/main/java/fr/maif/requests/SSEFeatureService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ private void processEvent(IzanamiEvent event) {
5757
}
5858

5959
public CompletableFuture<Void> disconnect() {
60-
this.sseClient.close();
61-
return CompletableFuture.completedFuture(null);
60+
return this.sseClient.close();
6261
}
6362

6463
@Override

src/main/java/fr/maif/requests/events/SSEClient.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,16 @@ public CompletableFuture<Void> disconnect() {
263263
}
264264
}
265265

266-
public void close() {
266+
public CompletableFuture<Void> close() {
267267
closed.set(true);
268-
disconnect();
269-
lifeProbeExecutorService.shutdown();
270-
executorService.shutdown();
268+
return disconnect().handle((useless, error) -> {
269+
if(Objects.nonNull(error)) {
270+
LOGGER.debug("Failed to disconnect SSE client", error);
271+
}
272+
lifeProbeExecutorService.shutdown();
273+
executorService.shutdown();
274+
return useless;
275+
});
271276
}
272277

273278

0 commit comments

Comments
 (0)