Source:
public Mono<AccessToken> getToken() { try { if (!wip.getAndSet(true)) { OffsetDateTime now = OffsetDateTime.now(); Mono<AccessToken> tokenRefresh; Mono<AccessToken> fallback; if (cache != null && !shouldRefresh.test(cache)) { tokenRefresh = Mono.empty(); fallback = Mono.just(cache); } else if (cache == null || cache.isExpired()) { if (now.isAfter(nextTokenRefresh)) { tokenRefresh = Mono.defer(tokenSupplier); } else { tokenRefresh = Mono.delay(Duration.between(now, nextTokenRefresh)) .then(Mono.defer(tokenSupplier)); } fallback = Mono.empty(); } else { if (now.isAfter(nextTokenRefresh)) { tokenRefresh = Mono.defer(tokenSupplier); } else { tokenRefresh = Mono.empty(); } fallback = Mono.just(cache); } return tokenRefresh .materialize() .flatMap(signal -> { AccessToken accessToken = signal.get(); Throwable error = signal.getThrowable(); if (signal.isOnNext() && accessToken != null) { logger.info(refreshLog(cache, now, "Acquired a new access token")); sink.next(accessToken); cache = accessToken; nextTokenRefresh = OffsetDateTime.now().plus(refreshRetryTimeout); return Mono.just(accessToken); } else if (signal.isOnError() && error != null) { logger.error(refreshLog(cache, now, "Failed to acquire a new access token")); nextTokenRefresh = OffsetDateTime.now().plus(refreshRetryTimeout); return fallback.switchIfEmpty(Mono.error(error)); } else { return fallback; } }) .doOnError(sink::error) .doOnTerminate(() -> wip.set(false)); } else { return emitterProcessor.next().filter(t -> !t.isExpired()); } } catch (Throwable t) { return Mono.error(t); } }

Target:
public Mono<AccessToken> getToken() { return Mono.defer(() -> { try { if (wip.compareAndSet(null, MonoProcessor.create())) { final MonoProcessor<AccessToken> monoProcessor = wip.get(); OffsetDateTime now = OffsetDateTime.now(); Mono<AccessToken> tokenRefresh; Mono<AccessToken> fallback; if (cache != null && !shouldRefresh.test(cache)) { tokenRefresh = Mono.empty(); fallback = Mono.just(cache); } else if (cache == null || cache.isExpired()) { if (now.isAfter(nextTokenRefresh)) { tokenRefresh = Mono.defer(tokenSupplier); } else { tokenRefresh = Mono.defer(tokenSupplier) .delaySubscription(Duration.between(now, nextTokenRefresh)); } fallback = Mono.empty(); } else { if (now.isAfter(nextTokenRefresh)) { tokenRefresh = Mono.defer(tokenSupplier); } else { tokenRefresh = Mono.empty(); } fallback = Mono.just(cache); } return tokenRefresh .materialize() .flatMap(signal -> { AccessToken accessToken = signal.get(); Throwable error = signal.getThrowable(); if (signal.isOnNext() && accessToken != null) { logger.info(refreshLog(cache, now, "Acquired a new access token")); cache = accessToken; monoProcessor.onNext(accessToken); monoProcessor.onComplete(); nextTokenRefresh = OffsetDateTime.now().plus(refreshRetryTimeout); return Mono.just(accessToken); } else if (signal.isOnError() && error != null) { logger.error(refreshLog(cache, now, "Failed to acquire a new access token")); nextTokenRefresh = OffsetDateTime.now().plus(refreshRetryTimeout); return fallback.switchIfEmpty(Mono.error(error)); } else { monoProcessor.onComplete(); return fallback; } }) .doOnError(monoProcessor::onError) .doOnTerminate(() -> wip.set(null)); } else if (cache != null && !cache.isExpired()) { return Mono.just(cache); } else { MonoProcessor<AccessToken> monoProcessor = wip.get(); if (monoProcessor == null) { return Mono.just(cache); } else { return monoProcessor.switchIfEmpty(Mono.defer(() -> Mono.just(cache))); } } } catch (Throwable t) { return Mono.error(t); } }); }