codelipenghui commented on code in PR #15454:
URL: https://github.com/apache/pulsar/pull/15454#discussion_r867353431
##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/FutureUtil.java:
##########
@@ -55,6 +57,36 @@ public static CompletableFuture<Object>
waitForAny(Collection<? extends Completa
return CompletableFuture.anyOf(futures.toArray(new
CompletableFuture[0]));
}
+ public static CompletableFuture<Optional<Object>> waitForAny(Collection<?
extends CompletableFuture<?>> futures,
+ Predicate<Object>
tester) {
+ return waitForAny(futures).thenCompose(v -> {
+ if (tester.test(v)) {
+ return CompletableFuture.completedFuture(Optional.of(v));
+ }
+ Collection<CompletableFuture<?>> doneFutures = futures.stream()
+ .filter(f -> f.isDone())
+ .collect(Collectors.toList());
+ futures.removeAll(doneFutures);
+ Optional<?> value = doneFutures.stream()
+ .filter(f -> !f.isCompletedExceptionally())
+ .map(CompletableFuture::join)
+ .filter(tester)
+ .findFirst();
+ if (!value.isPresent()) {
+ if (futures.size() == 0) {
+ return CompletableFuture.completedFuture(Optional.empty());
+ }
+ return waitForAny(futures, tester);
+ }
+ futures.forEach(f -> {
+ if (!f.isDone()) {
+ f.cancel(true);
+ }
+ });
Review Comment:
We should also cancel the remaining futures before line 64?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]