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

KTOR-7194 Deferred session fetching for public endpoints #4609

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bjhham
Copy link
Contributor

@bjhham bjhham commented Jan 15, 2025

Subsystem
Server, Sessions

Motivation
KTOR-7194 SessionStorage.read() is called for non-authenticated routes and static assets

Solution
This change provides an option in the sessions configuration deferred = true that defers session retrieval until it is used in the request.

This works by introducing a CurrentSession implementation that uses lazy retrieval to avoid unnecessary calls to the backing storage.

@bjhham bjhham force-pushed the bjhham/deferred-session-retrieval branch from 38f8ec5 to 9e7c373 Compare January 15, 2025 13:00
@bjhham bjhham force-pushed the bjhham/deferred-session-retrieval branch from 9e7c373 to 0f5ad71 Compare January 15, 2025 13:16
@bjhham bjhham requested a review from e5l January 16, 2025 07:18
*
* Note: this is only available for JVM in Ktor 3.0
*/
public var deferred: Boolean = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mark it with OptIn and look for a better explaining name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have a system property (like io.ktor.server.sessions.lazycreate) for this instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, this way we don't need to change the API 👍

/**
* When set to true, sessions will be lazily retrieved from storage.
*
* Note: this is only available for JVM in Ktor 3.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For JVM and Native?

import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class SessionAuthJvmTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this test be moved to the jvmAndPosix source-set?

Comment on lines +76 to +78
assertFailsWith<IllegalStateException> {
client.get("/authenticated", withCookie).status
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a successful call after the failed one? Maybe move one of the calls made above

bjhham and others added 2 commits January 16, 2025 16:06
…c/io/ktor/server/sessions/Sessions.kt

Co-authored-by: Osip Fatkullin <[email protected]>
…ix/src/io/ktor/server/sessions/BlockingDeferredSessionData.kt

Co-authored-by: Osip Fatkullin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants