Skip to content

Commit fbbc8a6

Browse files
author
Abel Salgado Romero
authored
Synchronize cache update (#3408)
Fixes gh-3407
1 parent a54297b commit fbbc8a6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/CachingRouteLocator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ public void onApplicationEvent(RefreshRoutesEvent event) {
8989
.onErrorResume(s -> Mono.just(List.of()));
9090

9191
scopedRoutes.subscribe(scopedRoutesList -> {
92-
Flux.concat(Flux.fromIterable(scopedRoutesList), getNonScopedRoutes(event))
93-
.sort(AnnotationAwareOrderComparator.INSTANCE).materialize().collect(Collectors.toList())
94-
.subscribe(this::publishRefreshEvent, this::handleRefreshError);
92+
updateCache(Flux.concat(Flux.fromIterable(scopedRoutesList), getNonScopedRoutes(event))
93+
.sort(AnnotationAwareOrderComparator.INSTANCE));
9594
}, this::handleRefreshError);
9695
}
9796
else {
9897
final Mono<List<Route>> allRoutes = fetch().collect(Collectors.toList());
99-
100-
allRoutes.subscribe(
101-
list -> Flux.fromIterable(list).materialize().collect(Collectors.toList())
102-
.subscribe(this::publishRefreshEvent, this::handleRefreshError),
103-
this::handleRefreshError);
98+
allRoutes.subscribe(list -> updateCache(Flux.fromIterable(list)), this::handleRefreshError);
10499
}
105100
}
106101
catch (Throwable e) {
107102
handleRefreshError(e);
108103
}
109104
}
110105

106+
private synchronized void updateCache(Flux<Route> routes) {
107+
routes.materialize().collect(Collectors.toList()).subscribe(this::publishRefreshEvent,
108+
this::handleRefreshError);
109+
}
110+
111111
private void publishRefreshEvent(List<Signal<Route>> signals) {
112112
cache.put(CACHE_KEY, signals);
113113
applicationEventPublisher.publishEvent(new RefreshRoutesResultEvent(this));

0 commit comments

Comments
 (0)