I am building a Spring Reactive web application. Currently the logic is all working correctly but I noticed that everything is being run 3 times instead of 1.
public Flux<PlayerPropDetail> getProps() {
Mono<List<Projections>> currentProjections = makeAnApiCall()
.filter(projection -> projection.getMinutes().compareTo(BigDecimal.ZERO) > 0).collectList();
Mono<List<Long>> todaysGameIds = currentProjections
.map(gameIds -> gameIds.stream().map(Projections::getGameId).distinct().collect(Collectors.toList())).log();
return doSomeOtherStuff(todaysGameIds, currentProjections);
}
I have added some logs and can see that doSomeOtherStuff() will get called 3 times. However, I am confused why and I am unsure of where the 3 times is coming from. The API call I am making with webclient returns a Flux with a lot of data and then I am going through that data and getting the distinct Game IDs (there is 5 distinct values there).
I am suspecting it has to do with the logic I have upstream from this method:
Flux<PlayerPropDetail> currentProps = getProps();
Flux<EvCalculationRequest> propsForEvCalculation = currentProps.flatMap(this::compareWithPreviousPropAndCreateRequests);
Flux<PlayerPropDetail> propsWithEv = propsForEvCalculation
.filter(prop -> Objects.nonNull(prop.getPropId()))
.buffer(200)
.flatMap(this::calculateValueForProp);
Flux<PlayerPropDetail> finalProps = Flux.zip(propsForEvCalculation.collectList(), currentProps.collectList(), propsWithEv.collectList())
.map(zippedPropsData -> combineAllProps(zippedPropsData))
.flatMap(Flux::fromIterable).map(nbaPropsRepository::updateProp);
return finalProps;