Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*
* @author Rossen Stoyanchev
* @author Rob Winch
* @author Mengqi Xu
* @since 5.0
*/
public class InMemoryWebSessionStore implements WebSessionStore {
Expand All @@ -51,6 +52,8 @@ public class InMemoryWebSessionStore implements WebSessionStore {

private int maxSessions = 10000;

private Duration defaultMaxIdleTime = Duration.ofMinutes(30);

private Clock clock = Clock.system(ZoneId.of("GMT"));

private final Map<String, InMemoryWebSession> sessions = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -78,6 +81,23 @@ public int getMaxSessions() {
return this.maxSessions;
}

/**
* Set the default maximum idle time for sessions.
* <p>By default, set to 30 minutes.
* @param maxIdleTime the default max idle time
*/
public void setDefaultMaxIdleTime(Duration maxIdleTime) {
Assert.notNull(maxIdleTime, "maxIdleTime is required");
this.defaultMaxIdleTime = maxIdleTime;
}

/**
* Return the default maximum idle time for sessions.
*/
public Duration getDefaultMaxIdleTime() {
return this.defaultMaxIdleTime;
}

/**
* Configure the {@link Clock} to use to set the {@code lastAccessTime} on
* every created session and to calculate if the session has expired.
Expand Down Expand Up @@ -119,7 +139,7 @@ public Mono<WebSession> createWebSession() {
Instant now = this.clock.instant();
this.expiredSessionChecker.checkIfNecessary(now);

return Mono.<WebSession>fromSupplier(() -> new InMemoryWebSession(now))
return Mono.<WebSession>fromSupplier(() -> new InMemoryWebSession(now, this.defaultMaxIdleTime))
.subscribeOn(Schedulers.boundedElastic())
.publishOn(Schedulers.parallel());
}
Expand Down Expand Up @@ -180,14 +200,15 @@ private class InMemoryWebSession implements WebSession {

private volatile Instant lastAccessTime;

private volatile Duration maxIdleTime = Duration.ofMinutes(30);
private volatile Duration maxIdleTime;

private final AtomicReference<State> state = new AtomicReference<>(State.NEW);


public InMemoryWebSession(Instant creationTime) {
public InMemoryWebSession(Instant creationTime, Duration maxIdleTime) {
this.creationTime = creationTime;
this.lastAccessTime = this.creationTime;
this.maxIdleTime = maxIdleTime;
}

@Override
Expand Down