Simplify initialization of WebSession Mono

The DefaultWebSessionManager now uses Mono.defer to protect the call
to getSession from parsing session cookies immediately. This allows
pre-initializing the Mono<WebSession> upfront vs using a lock.
master
Rossen Stoyanchev 8 years ago
parent 4e2802338a
commit 2f2546c8a4
  1. 17
      spring-web-reactive/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java
  2. 5
      spring-web-reactive/src/main/java/org/springframework/web/server/session/DefaultWebSessionManager.java

@ -39,15 +39,9 @@ public class DefaultServerWebExchange implements ServerWebExchange {
private final ServerHttpResponse response;
private final WebSessionManager sessionManager;
private final Map<String, Object> attributes = new ConcurrentHashMap<>();
private final Object createSessionLock = new Object();
private Mono<WebSession> sessionMono;
private final Mono<WebSession> sessionMono;
public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
@ -58,7 +52,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
Assert.notNull(response, "'sessionManager' is required.");
this.request = request;
this.response = response;
this.sessionManager = sessionManager;
this.sessionMono = sessionManager.getSession(this).cache();
}
@ -84,13 +78,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
@Override
public Mono<WebSession> getSession() {
if (this.sessionMono == null) {
synchronized (this.createSessionLock) {
if (this.sessionMono == null) {
this.sessionMono = this.sessionManager.getSession(this).cache();
}
}
}
return this.sessionMono;
}

@ -99,12 +99,13 @@ public class DefaultWebSessionManager implements WebSessionManager {
@Override
public Mono<WebSession> getSession(ServerWebExchange exchange) {
return Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange))
return Mono.defer(() ->
Flux.fromIterable(getSessionIdResolver().resolveSessionIds(exchange))
.concatMap(this.sessionStore::retrieveSession)
.next()
.then(session -> validateSession(exchange, session))
.otherwiseIfEmpty(createSession(exchange))
.map(session -> extendSession(exchange, session));
.map(session -> extendSession(exchange, session)));
}
protected Mono<WebSession> validateSession(ServerWebExchange exchange, WebSession session) {

Loading…
Cancel
Save