Skip to content

Commit 928b281

Browse files
committed
paralle set state
1 parent 9bc38b5 commit 928b281

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

openfeature-provider-local/src/main/java/com/spotify/confidence/ThreadLocalSwapWasmResolverApi.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.spotify.confidence.flags.resolver.v1.ResolveWithStickyRequest;
44
import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsRequest;
55
import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsResponse;
6+
import com.spotify.futures.CompletableFutures;
7+
import java.util.ArrayList;
68
import java.util.Map;
79
import java.util.concurrent.CompletableFuture;
810
import java.util.concurrent.ConcurrentHashMap;
@@ -52,18 +54,24 @@ public ThreadLocalSwapWasmResolverApi(
5254
this.numInstances = Runtime.getRuntime().availableProcessors();
5355
logger.info(
5456
"Initialized ThreadLocalSwapWasmResolverApi with {} available processors", numInstances);
57+
final var futures = new ArrayList<CompletableFuture<Void>>(numInstances);
58+
5559
IntStream.range(0, numInstances)
5660
.forEach(
57-
i -> {
58-
final var instance =
59-
new SwapWasmResolverApi(
60-
this.flagLogger,
61-
this.currentState,
62-
this.currentAccountId,
63-
this.stickyResolveStrategy,
64-
this.retryStrategy);
65-
resolverInstances.put(i, instance);
66-
});
61+
i ->
62+
futures.add(
63+
CompletableFuture.runAsync(
64+
() -> {
65+
final var instance =
66+
new SwapWasmResolverApi(
67+
this.flagLogger,
68+
this.currentState,
69+
this.currentAccountId,
70+
this.stickyResolveStrategy,
71+
this.retryStrategy);
72+
resolverInstances.put(i, instance);
73+
})));
74+
CompletableFutures.allAsList(futures).join();
6775
}
6876

6977
/**
@@ -75,10 +83,11 @@ public void updateStateAndFlushLogs(byte[] state, String accountId) {
7583
this.currentState = state;
7684
this.currentAccountId = accountId;
7785

78-
// Update all pre-initialized resolver instances
79-
resolverInstances
80-
.values()
81-
.forEach(resolver -> resolver.updateStateAndFlushLogs(state, accountId));
86+
final var futures =
87+
resolverInstances.values().stream()
88+
.map(v -> CompletableFuture.runAsync(() -> v.updateStateAndFlushLogs(state, accountId)))
89+
.toList();
90+
CompletableFutures.allAsList(futures).join();
8291
}
8392

8493
/**

0 commit comments

Comments
 (0)