Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maxInactiveInterval from JdkMongoSessionConverter is always from MongoSession #2910

Closed
Lucasark opened this issue Apr 2, 2024 · 3 comments
Closed
Assignees
Labels
Milestone

Comments

@Lucasark
Copy link

Lucasark commented Apr 2, 2024

This attrb is required for create JdkMongoSessionConverter, however is never used to set in the interval.

I know I can set this interval from "@EnableMongoHttpSession", but it is can make confusing when read the documentation

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 2, 2024
@marcusdacoregio
Copy link
Contributor

Hi @Lucasark.

The maxInactiveInterval is used if the interval attribute is not present in the Mongo Document when converting it to a MongoSession. I don't quite understand what is the problem here. Would you mind providing more details on where the problem is or what would be the expected behavior?

@marcusdacoregio marcusdacoregio self-assigned this Apr 3, 2024
@marcusdacoregio marcusdacoregio added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 3, 2024
@Lucasark
Copy link
Author

Lucasark commented Apr 5, 2024

@marcusdacoregio If I just write this:

@EnableMongoHttpSession
@Configuration(proxyBeanMethods = false)
public class SessionConfig {

    @Bean
    public JdkMongoSessionConverter jdkMongoSessionConverter() {
        return new JdkMongoSessionConverter(Duration.ofMinutes(1));
    }

}

JdkMongoSessionConverter parameter is same of nothing, is not used. I need explicit in EnableMongoHttpSession, even the JdkMongoSessionConverter require 1 parameter

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 5, 2024
@maxedenharter0507
Copy link

maxedenharter0507 commented Apr 9, 2024

@marcusdacoregio

I recognized the same behavior with the ReactiveMongoSessionRepository and I think the problem is that the MongoSession will be created with a constructor without interval, and AFTERWARDS the maxInactiveInterval will be set. But the constructor already called the setLastAccessedTime method, which will recalculate the expireAt with MapSession.DEFAULT_MAX_INACTIVE_INTERVAL (30m).

To keep it short, every session will have a expireAt date of creationDate + 30m after construction. Setting the maxInteractiveInterval will only have an effect on later setLastAccessedTime calls.

A simple solution would be to use directly the constructor with sessionIdGenerator and maxInactiveInterval instead of calling the constructor without maxInactiveInterval and calling all the setters afterwards.

Code in the ReactiveMongoSessionRepository:

@Override
public Mono<MongoSession> createSession() {
	// @formatter:off
	return Mono.fromSupplier(() -> this.sessionIdGenerator.generate())
	      .map(MongoSession::new)
	      .doOnNext((mongoSession) -> mongoSession.setMaxInactiveInterval(this.defaultMaxInactiveInterval))
	      .doOnNext(
		(mongoSession) -> mongoSession.setSessionIdGenerator(this.sessionIdGenerator))
	      .doOnNext((mongoSession) -> publishEvent(new SessionCreatedEvent(this, mongoSession)))
	      .switchIfEmpty(Mono.just(new MongoSession(this.sessionIdGenerator)))
	      .subscribeOn(Schedulers.boundedElastic())
	      .publishOn(Schedulers.parallel());
       // @formatter:on
}

https://github.com/spring-projects/spring-session/blob/main/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/ReactiveMongoSessionRepository.java#L103-L104

Constructor:

MongoSession(String id, long maxInactiveIntervalInSeconds) {
	this.id = id;
	this.originalSessionId = id;
	this.intervalSeconds = maxInactiveIntervalInSeconds;
	setLastAccessedTime(Instant.ofEpochMilli(this.createdMillis));
}

https://github.com/spring-projects/spring-session/blob/main/spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/MongoSession.java#L78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

4 participants