Class: VectorMCP::Transport::BaseSessionManager Abstract
- Inherits:
-
Object
- Object
- VectorMCP::Transport::BaseSessionManager
- Defined in:
- lib/vector_mcp/transport/base_session_manager.rb
Overview
Subclass and implement transport-specific methods
Base session manager providing unified session lifecycle management across all transports. This abstract base class defines the standard interface that all transport session managers should implement, ensuring consistent session handling regardless of transport type.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#session_timeout ⇒ Object
readonly
Returns the value of attribute session_timeout.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Instance Method Summary collapse
-
#active_session_ids ⇒ Array<String>
Gets all active session IDs.
-
#cleanup_all_sessions ⇒ void
Cleans up all sessions and stops the cleanup timer.
-
#create_session(session_id = nil) ⇒ VectorMCP::Session
Creates a new session.
-
#find_sessions(criteria = {}) ⇒ Array<Session>
Finds sessions matching criteria.
-
#get_or_create_session(session_id = nil) ⇒ VectorMCP::Session?
Gets an existing session or creates a new one.
-
#get_session(session_id) ⇒ VectorMCP::Session?
Gets an existing session by ID.
-
#get_session_metadata(session_id) ⇒ Hash?
Gets session metadata.
-
#initialize(transport, session_timeout = 300) ⇒ BaseSessionManager
constructor
Initializes a new session manager.
-
#session_count ⇒ Integer
Gets the current number of active sessions.
-
#session_metadata_updated?(session_id, metadata) ⇒ Boolean
Updates session metadata.
-
#session_terminated?(session_id) ⇒ Boolean
Terminates a session by ID.
-
#sessions? ⇒ Boolean
Checks if any sessions exist.
Constructor Details
#initialize(transport, session_timeout = 300) ⇒ BaseSessionManager
Initializes a new session manager.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 20 def initialize(transport, session_timeout = 300) @transport = transport @session_timeout = session_timeout @logger = transport.logger @sessions = Concurrent::Hash.new @cleanup_timer = nil start_cleanup_timer if auto_cleanup_enabled? logger.debug { "#{self.class.name} initialized with session_timeout: #{session_timeout}" } end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
14 15 16 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14 def logger @logger end |
#session_timeout ⇒ Object (readonly)
Returns the value of attribute session_timeout.
14 15 16 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14 def session_timeout @session_timeout end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
14 15 16 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 14 def transport @transport end |
Instance Method Details
#active_session_ids ⇒ Array<String>
Gets all active session IDs.
100 101 102 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 100 def active_session_ids @sessions.keys end |
#cleanup_all_sessions ⇒ void
This method returns an undefined value.
Cleans up all sessions and stops the cleanup timer.
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 114 def cleanup_all_sessions logger.info { "Cleaning up all sessions: #{@sessions.size}" } @sessions.each_value do |session| on_session_terminated(session) end @sessions.clear stop_cleanup_timer end |
#create_session(session_id = nil) ⇒ VectorMCP::Session
Creates a new session.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 64 def create_session(session_id = nil) session_id ||= generate_session_id session = VectorMCP::Session.new(@transport.server, @transport, id: session_id) session..merge!() @sessions[session_id] = session logger.info { "Session created: #{session_id}" } on_session_created(session) session end |
#find_sessions(criteria = {}) ⇒ Array<Session>
Finds sessions matching criteria.
154 155 156 157 158 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 154 def find_sessions(criteria = {}) @sessions.values.select do |session| matches_criteria?(session, criteria) end end |
#get_or_create_session(session_id = nil) ⇒ VectorMCP::Session?
Gets an existing session or creates a new one. When a session_id is provided but not found (expired or unknown), returns nil instead of creating a session with the client-supplied ID — session IDs are always server-generated (prevents session fixation).
52 53 54 55 56 57 58 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 52 def get_or_create_session(session_id = nil) if session_id get_session(session_id) else create_session end end |
#get_session(session_id) ⇒ VectorMCP::Session?
Gets an existing session by ID.
35 36 37 38 39 40 41 42 43 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 35 def get_session(session_id) return nil unless session_id session = @sessions[session_id] return nil unless session && !session.expired?(@session_timeout) session.touch! session end |
#get_session_metadata(session_id) ⇒ Hash?
Gets session metadata.
143 144 145 146 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 143 def (session_id) session = @sessions[session_id] session&. end |
#session_count ⇒ Integer
Gets the current number of active sessions.
93 94 95 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 93 def session_count @sessions.size end |
#session_metadata_updated?(session_id, metadata) ⇒ Boolean
Updates session metadata.
130 131 132 133 134 135 136 137 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 130 def (session_id, ) session = @sessions[session_id] return false unless session session..merge!() session.touch! true end |
#session_terminated?(session_id) ⇒ Boolean
Terminates a session by ID.
81 82 83 84 85 86 87 88 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 81 def session_terminated?(session_id) session = @sessions.delete(session_id) return false unless session on_session_terminated(session) logger.info { "Session terminated: #{session_id}" } true end |
#sessions? ⇒ Boolean
Checks if any sessions exist.
107 108 109 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 107 def sessions? !@sessions.empty? end |