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.
This commit is contained in:
@@ -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))
|
||||
.concatMap(this.sessionStore::retrieveSession)
|
||||
.next()
|
||||
.then(session -> validateSession(exchange, session))
|
||||
.otherwiseIfEmpty(createSession(exchange))
|
||||
.map(session -> extendSession(exchange, session));
|
||||
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)));
|
||||
}
|
||||
|
||||
protected Mono<WebSession> validateSession(ServerWebExchange exchange, WebSession session) {
|
||||
|
||||
Reference in New Issue
Block a user