-
Notifications
You must be signed in to change notification settings - Fork 126
feat: Add pluggable storage backend for session management #1989
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
JAORMX
wants to merge
4
commits into
main
Choose a base branch
from
feat/session-storage-pluggable
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.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
525d5d9
feat: Add pluggable storage backend for session management
JAORMX c1e3fcd
Address PR feedback: fix race condition and encapsulation issues
JAORMX a8ebd91
Fix incorrect string conversion in test session ID generation
JAORMX 695f279
Address PR feedback: improve session storage interface design
JAORMX 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package session | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// The following serialization functions are prepared for Phase 4 (Redis/Valkey implementation) | ||
// They are currently unused but will be needed when implementing distributed storage backends. | ||
|
||
// sessionData is the JSON representation of a session. | ||
// This structure is used for serializing sessions to/from storage backends. | ||
// nolint:unused // Will be used in Phase 4 for Redis/Valkey storage | ||
type sessionData struct { | ||
ID string `json:"id"` | ||
Type SessionType `json:"type"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
Data json.RawMessage `json:"data,omitempty"` | ||
Metadata map[string]string `json:"metadata,omitempty"` | ||
} | ||
|
||
// serializeSession converts a Session to its JSON representation. | ||
// nolint:unused // Will be used in Phase 4 for Redis/Valkey storage | ||
func serializeSession(s Session) ([]byte, error) { | ||
if s == nil { | ||
return nil, fmt.Errorf("cannot serialize nil session") | ||
} | ||
|
||
data := sessionData{ | ||
ID: s.ID(), | ||
Type: s.Type(), | ||
CreatedAt: s.CreatedAt(), | ||
UpdatedAt: s.UpdatedAt(), | ||
Metadata: s.GetMetadata(), | ||
} | ||
|
||
// Handle session-specific data | ||
if sessionData := s.GetData(); sessionData != nil { | ||
jsonData, err := json.Marshal(sessionData) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal session data: %w", err) | ||
} | ||
data.Data = jsonData | ||
} | ||
|
||
return json.Marshal(data) | ||
} | ||
|
||
// deserializeSession reconstructs a Session from its JSON representation. | ||
// It creates the appropriate session type based on the Type field. | ||
// nolint:unused // Will be used in Phase 4 for Redis/Valkey storage | ||
func deserializeSession(data []byte) (Session, error) { | ||
if len(data) == 0 { | ||
return nil, fmt.Errorf("cannot deserialize empty data") | ||
} | ||
|
||
var sd sessionData | ||
if err := json.Unmarshal(data, &sd); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal session data: %w", err) | ||
} | ||
|
||
// Create appropriate session type using existing constructors | ||
var session Session | ||
switch sd.Type { | ||
case SessionTypeSSE: | ||
// Use existing NewSSESession constructor | ||
sseSession := NewSSESession(sd.ID) | ||
// Update timestamps to match stored values | ||
sseSession.setTimestamps(sd.CreatedAt, sd.UpdatedAt) | ||
// Restore metadata | ||
sseSession.setMetadataMap(sd.Metadata) | ||
// Note: SSE channels and client info will be recreated when reconnected | ||
session = sseSession | ||
|
||
case SessionTypeStreamable: | ||
// Use existing NewStreamableSession constructor | ||
sess := NewStreamableSession(sd.ID) | ||
streamSession, ok := sess.(*StreamableSession) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to create StreamableSession") | ||
} | ||
// Update timestamps to match stored values | ||
streamSession.setTimestamps(sd.CreatedAt, sd.UpdatedAt) | ||
// Restore metadata | ||
streamSession.setMetadataMap(sd.Metadata) | ||
session = streamSession | ||
|
||
case SessionTypeMCP: | ||
fallthrough | ||
default: | ||
// Use existing NewTypedProxySession constructor | ||
proxySession := NewTypedProxySession(sd.ID, sd.Type) | ||
// Update timestamps to match stored values | ||
proxySession.setTimestamps(sd.CreatedAt, sd.UpdatedAt) | ||
// Restore metadata | ||
proxySession.setMetadataMap(sd.Metadata) | ||
session = proxySession | ||
} | ||
|
||
// Restore session-specific data if present | ||
if len(sd.Data) > 0 { | ||
// For now, we store the raw JSON. Session-specific implementations | ||
// can unmarshal this as needed. | ||
session.SetData(sd.Data) | ||
} | ||
|
||
return session, nil | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.