Repository navigation
fix(client): scope default cache partition to the connection #1194
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
Open
e5l
wants to merge
4
commits into
devcrocod/sep-2575-stateless-mcp
Choose a base branch
from
fix/sep2575-cache-connection-scope
base: devcrocod/sep-2575-stateless-mcp
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a0d72d2
test(client): confine cached results to their server and principal
e5l 59f9e13
fix(client): scope default cache partition to the connection
e5l f7b5d59
test(client): give each sharing client its own server instance
e5l 0deb00d
docs: trim cache partition comments
e5l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,8 @@ import kotlin.coroutines.CoroutineContext | |
| import kotlin.coroutines.cancellation.CancellationException | ||
| import kotlin.time.Clock | ||
| import kotlin.time.ExperimentalTime | ||
| import kotlin.uuid.ExperimentalUuidApi | ||
| import kotlin.uuid.Uuid | ||
|
|
||
| private val logger = KotlinLogging.logger {} | ||
|
|
||
|
|
@@ -121,9 +123,11 @@ private val logger = KotlinLogging.logger {} | |
| * @property responseCache Where to keep results a server marked reusable. Off by default; | ||
| * [InMemoryResponseCacheStore] caches for the lifetime of this client. Consulted only on the | ||
| * request-scoped lifecycle, the only one whose results carry caching directives. | ||
| * @property cachePartition The authorization context this client fetches under, which | ||
| * [CacheScope.Private] entries are confined to. Only meaningful for a store shared across | ||
| * principals; the default scopes such entries to this client instance. | ||
| * @property cachePartition The namespace this client files cached results under, public and | ||
| * [CacheScope.Private] alike. Clients configured with the same partition share entries, so it | ||
| * must name both the server and the authorization context, such as the server URL and the | ||
| * access token's subject. Empty, the default, confines entries to the current connection: | ||
| * neither another client nor this one after a reconnect is served them. | ||
| */ | ||
| public class ClientOptions( | ||
| public val capabilities: ClientCapabilities = ClientCapabilities(), | ||
|
|
@@ -218,6 +222,8 @@ public open class Client(private val clientInfo: Implementation, options: Client | |
| @OptIn(ExperimentalMcpApi::class) | ||
| private val cachePartition: String = options.cachePartition | ||
|
|
||
| private val connectionCachePartition: AtomicRef<String> = atomic(cachePartition) | ||
|
|
||
| private val roots = atomic(persistentMapOf<String, Root>()) | ||
|
|
||
| private val _protocolVersion: AtomicRef<String?> = atomic(null) | ||
|
|
@@ -284,9 +290,11 @@ public open class Client(private val clientInfo: Implementation, options: Client | |
| * @param transport The transport to use for communication with the server. | ||
| * @throws IllegalStateException If the server's protocol version is not supported. | ||
| */ | ||
| @OptIn(ExperimentalMcpApi::class) | ||
| @OptIn(ExperimentalMcpApi::class, ExperimentalUuidApi::class) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check experimental UUID in kotlin 2.3 |
||
| override suspend fun connect(transport: Transport) { | ||
| super.connect(transport) | ||
| val partition = cachePartition.ifEmpty { Uuid.random().toString() } | ||
| connectionCachePartition.value = partition | ||
|
|
||
| try { | ||
| if (!negotiateRequestScoped(transport)) { | ||
|
|
@@ -489,7 +497,7 @@ public open class Client(private val clientInfo: Implementation, options: Client | |
| ) { | ||
| return fetch() | ||
| } | ||
| val key = CacheKey(method = method, paramsKey = paramsKey, partition = cachePartition) | ||
| val key = CacheKey(method = method, paramsKey = paramsKey, partition = connectionCachePartition.value) | ||
| if (mode == CacheMode.Use) { | ||
| store.get(key)?.takeIf { Clock.System.now() < it.expiresAt }?.let { | ||
| @Suppress("UNCHECKED_CAST") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Kdoc should be shortly
because it's not readable and I don't undestantand how to use this parameter