-
Notifications
You must be signed in to change notification settings - Fork 601
Add DistributedCacheEventStreamStore
#1136
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
Draft
MackinnonBuck
wants to merge
15
commits into
main
Choose a base branch
from
mbuck/distributed-sse-event-stream-store
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f69fc61
Add distributed SSE event stream store
MackinnonBuck e344927
Small fixes and test improvements
MackinnonBuck f70bd12
Adjust for latest changes to `ISseEventStreamStore`
MackinnonBuck 984df24
Clean up tests, throw on expired cache entries
MackinnonBuck 80b2cb4
Add logging
MackinnonBuck bcbf247
Store retry interval
MackinnonBuck 66083ea
Use span-based APIs for event ID parsing
MackinnonBuck 17867ee
Add shorter timeout on `Client_CanResumeUnsolicitedMessageStream_Afte…
MackinnonBuck f59c0e0
Use longer timeout in test
MackinnonBuck 1fae1f6
Use versioned cache keys
MackinnonBuck e2d3ba0
Fix flaky test
MackinnonBuck 114feac
Amend test fix
MackinnonBuck 76c064a
Update log message
MackinnonBuck 0a0bc54
Lengthen unusually short test timeouts
MackinnonBuck 3238d65
Remove redundant CTS
MackinnonBuck 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
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
118 changes: 118 additions & 0 deletions
118
src/ModelContextProtocol.Core/Server/DistributedCacheEventIdFormatter.cs
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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // This is a shared source file included in both ModelContextProtocol.Core and the test project. | ||
| // Do not reference symbols internal to the core project, as they won't be available in tests. | ||
|
|
||
| #if NET | ||
| using System.Buffers; | ||
| using System.Buffers.Text; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| #endif | ||
| using System.Text; | ||
|
|
||
| namespace ModelContextProtocol.Server; | ||
|
|
||
| /// <summary> | ||
| /// Provides methods for formatting and parsing event IDs used by <see cref="DistributedCacheEventStreamStore"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Event IDs are formatted as "{base64(sessionId)}:{base64(streamId)}:{sequence}". | ||
| /// </remarks> | ||
| internal static class DistributedCacheEventIdFormatter | ||
| { | ||
| private const char Separator = ':'; | ||
|
|
||
| /// <summary> | ||
| /// Formats session ID, stream ID, and sequence number into an event ID string. | ||
| /// </summary> | ||
| public static string Format(string sessionId, string streamId, long sequence) | ||
| { | ||
| // Base64-encode session and stream IDs so the event ID can be parsed | ||
| // even if the original IDs contain the ':' separator character | ||
| var sessionBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(sessionId)); | ||
| var streamBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(streamId)); | ||
|
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. Separate from this PR, we should really add Base64 overloads that handle this without the intermediate byte[]. I will follow up. |
||
| return $"{sessionBase64}{Separator}{streamBase64}{Separator}{sequence}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to parse an event ID into its component parts. | ||
| /// </summary> | ||
| public static bool TryParse(string eventId, out string sessionId, out string streamId, out long sequence) | ||
| { | ||
| sessionId = string.Empty; | ||
| streamId = string.Empty; | ||
| sequence = 0; | ||
|
|
||
| #if NET | ||
| ReadOnlySpan<char> eventIdSpan = eventId.AsSpan(); | ||
| Span<Range> partRanges = stackalloc Range[4]; | ||
| int rangeCount = eventIdSpan.Split(partRanges, Separator); | ||
| if (rangeCount != 3) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| ReadOnlySpan<char> sessionBase64 = eventIdSpan[partRanges[0]]; | ||
| ReadOnlySpan<char> streamBase64 = eventIdSpan[partRanges[1]]; | ||
| ReadOnlySpan<char> sequenceSpan = eventIdSpan[partRanges[2]]; | ||
|
|
||
| if (!TryDecodeBase64ToString(sessionBase64, out sessionId!) || | ||
| !TryDecodeBase64ToString(streamBase64, out streamId!)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return long.TryParse(sequenceSpan, out sequence); | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| #else | ||
| var parts = eventId.Split(Separator); | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (parts.Length != 3) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| sessionId = Encoding.UTF8.GetString(Convert.FromBase64String(parts[0])); | ||
| streamId = Encoding.UTF8.GetString(Convert.FromBase64String(parts[1])); | ||
| return long.TryParse(parts[2], out sequence); | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| #if NET | ||
| private static bool TryDecodeBase64ToString(ReadOnlySpan<char> base64Chars, [NotNullWhen(true)] out string? result) | ||
| { | ||
| // Use a single buffer: base64 chars are ASCII (1:1 with UTF8 bytes), | ||
| // and decoded data is always smaller than encoded, so we can decode in-place. | ||
| int bufferLength = base64Chars.Length; | ||
| Span<byte> buffer = bufferLength <= 256 | ||
| ? stackalloc byte[bufferLength] | ||
| : new byte[bufferLength]; | ||
|
|
||
| Encoding.UTF8.GetBytes(base64Chars, buffer); | ||
|
|
||
| OperationStatus status = Base64.DecodeFromUtf8InPlace(buffer, out int bytesWritten); | ||
| if (status != OperationStatus.Done) | ||
| { | ||
| result = null; | ||
| return false; | ||
| } | ||
|
|
||
| result = Encoding.UTF8.GetString(buffer[..bytesWritten]); | ||
| return true; | ||
| } | ||
| #endif | ||
| } | ||
Oops, something went wrong.
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.
Is ModelContextProtocol.Core the right assembly for this, or should it instead live in ModelContextProtocol or ModelContextProtocol.AspNetCore?
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.
It could probably live in
ModelContextProtocol.AspNetCorefor the sake of minimizing dependencies in the.Coreproject.