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
HttpStream::SessionManager, SseSessionManager, StdioSessionManager
Defined Under Namespace
Classes: Session
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.
-
#broadcast_message(message) ⇒ Integer
Broadcasts a message to all sessions that support messaging.
-
#cleanup_all_sessions ⇒ void
Cleans up all sessions and stops the cleanup timer.
-
#create_session(session_id = nil) ⇒ Session
Creates a new session.
-
#find_sessions(criteria = {}) ⇒ Array<Session>
Finds sessions matching criteria.
-
#get_or_create_session(session_id = nil) ⇒ Session
Gets an existing session or creates a new one.
-
#get_session(session_id) ⇒ 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.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 35 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.
29 30 31 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29 def logger @logger end |
#session_timeout ⇒ Object (readonly)
Returns the value of attribute session_timeout.
29 30 31 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29 def session_timeout @session_timeout end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
29 30 31 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 29 def transport @transport end |
Instance Method Details
#active_session_ids ⇒ Array<String>
Gets all active session IDs.
126 127 128 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 126 def active_session_ids @sessions.keys end |
#broadcast_message(message) ⇒ Integer
Broadcasts a message to all sessions that support messaging.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 190 def () count = 0 @sessions.each_value do |session| next unless (session) count += 1 if (session, ) end # Message broadcasted to recipients count end |
#cleanup_all_sessions ⇒ void
This method returns an undefined value.
Cleans up all sessions and stops the cleanup timer.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 140 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) ⇒ Session
Creates a new session.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 80 def create_session(session_id = nil) session_id ||= generate_session_id now = Time.now # Create VectorMCP session context session_context = VectorMCP::Session.new(@transport.server, @transport, id: session_id) # Create internal session record with transport-specific metadata session = Session.new( session_id, session_context, now, now, ) @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.
180 181 182 183 184 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 180 def find_sessions(criteria = {}) @sessions.values.select do |session| matches_criteria?(session, criteria) end end |
#get_or_create_session(session_id = nil) ⇒ Session
Gets an existing session or creates a new one.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 64 def get_or_create_session(session_id = nil) if session_id session = get_session(session_id) return session if session # If session_id was provided but not found, create with that ID return create_session(session_id) end create_session end |
#get_session(session_id) ⇒ Session?
Gets an existing session by ID.
50 51 52 53 54 55 56 57 58 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 50 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.
169 170 171 172 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 169 def (session_id) session = @sessions[session_id] session&. end |
#session_count ⇒ Integer
Gets the current number of active sessions.
119 120 121 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 119 def session_count @sessions.size end |
#session_metadata_updated?(session_id, metadata) ⇒ Boolean
Updates session metadata.
156 157 158 159 160 161 162 163 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 156 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.
107 108 109 110 111 112 113 114 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 107 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.
133 134 135 |
# File 'lib/vector_mcp/transport/base_session_manager.rb', line 133 def sessions? !@sessions.empty? end |