From b4e9af9ca463512f4240655aaeba141d70bf8353 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 11 Aug 2023 09:01:00 +0800 Subject: [PATCH] Avoid blocking on session id generation see https://github.com/spring-projects/spring-session/issues/2393 --- .../session/ReactiveMapSessionRepository.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java b/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java index 70fc9edf..141349f9 100644 --- a/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java +++ b/spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 the original author or authors. + * Copyright 2014-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.time.Duration; import java.util.Map; import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; @@ -37,6 +38,7 @@ import org.springframework.util.Assert; *

* * @author Rob Winch + * @author Yanming Zhou * @since 2.0 */ public class ReactiveMapSessionRepository implements ReactiveSessionRepository { @@ -98,11 +100,17 @@ public class ReactiveMapSessionRepository implements ReactiveSessionRepository createSession() { - return Mono.defer(() -> { - MapSession result = new MapSession(this.sessionIdGenerator); - result.setMaxInactiveInterval(this.defaultMaxInactiveInterval); - return Mono.just(result); - }); + // @formatter:off + return Mono.fromSupplier(() -> this.sessionIdGenerator.generate()) + .subscribeOn(Schedulers.boundedElastic()) + .publishOn(Schedulers.parallel()) + .map((sessionId) -> { + MapSession result = new MapSession(sessionId); + result.setMaxInactiveInterval(this.defaultMaxInactiveInterval); + result.setSessionIdGenerator(this.sessionIdGenerator); + return result; + }); + // @formatter:on } /**