Skip to content

Commit b4e9af9

Browse files
quaffmarcusdacoregio
authored andcommitted
Avoid blocking on session id generation
see #2393
1 parent 21ab473 commit b4e9af9

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

spring-session-core/src/main/java/org/springframework/session/ReactiveMapSessionRepository.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121

2222
import reactor.core.publisher.Mono;
23+
import reactor.core.scheduler.Schedulers;
2324

2425
import org.springframework.session.events.SessionDeletedEvent;
2526
import org.springframework.session.events.SessionExpiredEvent;
@@ -37,6 +38,7 @@
3738
* </p>
3839
*
3940
* @author Rob Winch
41+
* @author Yanming Zhou
4042
* @since 2.0
4143
*/
4244
public class ReactiveMapSessionRepository implements ReactiveSessionRepository<MapSession> {
@@ -98,11 +100,17 @@ public Mono<Void> deleteById(String id) {
98100

99101
@Override
100102
public Mono<MapSession> createSession() {
101-
return Mono.defer(() -> {
102-
MapSession result = new MapSession(this.sessionIdGenerator);
103-
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
104-
return Mono.just(result);
105-
});
103+
// @formatter:off
104+
return Mono.fromSupplier(() -> this.sessionIdGenerator.generate())
105+
.subscribeOn(Schedulers.boundedElastic())
106+
.publishOn(Schedulers.parallel())
107+
.map((sessionId) -> {
108+
MapSession result = new MapSession(sessionId);
109+
result.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
110+
result.setSessionIdGenerator(this.sessionIdGenerator);
111+
return result;
112+
});
113+
// @formatter:on
106114
}
107115

108116
/**

0 commit comments

Comments
 (0)