Class: VectorMCP::Transport::HttpStream::SessionManager Private

Inherits:
BaseSessionManager show all
Defined in:
lib/vector_mcp/transport/http_stream/session_manager.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages HTTP stream sessions with automatic cleanup and thread safety. Extends BaseSessionManager with HTTP streaming-specific functionality.

Handles:

  • Session creation and lifecycle management

  • Thread-safe session storage using concurrent-ruby

  • Automatic session cleanup based on timeout

  • Session context integration with VectorMCP::Session

  • HTTP streaming connection management

Defined Under Namespace

Classes: Session

Instance Attribute Summary

Attributes inherited from BaseSessionManager

#logger, #session_timeout, #transport

Instance Method Summary collapse

Methods inherited from BaseSessionManager

#active_session_ids, #broadcast_message, #cleanup_all_sessions, #find_sessions, #get_session, #get_session_metadata, #initialize, #session_count, #session_metadata_updated?, #session_terminated?, #sessions?

Constructor Details

This class inherits a constructor from VectorMCP::Transport::BaseSessionManager

Instance Method Details

#create_minimal_session_context(session_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a minimal session context for each session (no caching to prevent contamination)



105
106
107
108
109
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 105

def create_minimal_session_context(session_id)
  # Create a new minimal context for each session to prevent cross-session contamination
  minimal_context = VectorMCP::RequestContext.minimal("http_stream")
  VectorMCP::Session.new(@transport.server, @transport, id: session_id, request_context: minimal_context)
end

#create_session(session_id = nil, rack_env = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Optimized session creation with reduced object allocation and faster context creation



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 55

def create_session(session_id = nil, rack_env = nil)
  session_id ||= generate_session_id
  now = Time.now

  # Optimize session context creation - use cached minimal context when rack_env is nil
  session_context = if rack_env
                      create_session_with_context(session_id, rack_env)
                    else
                      create_minimal_session_context(session_id)
                    end

  # Pre-allocate metadata hash for better performance
   = { streaming_connection: nil }

  # Create internal session record with streaming connection metadata
  session = Session.new(session_id, session_context, now, now, )

  @sessions[session_id] = session

  logger.info { "Session created: #{session_id}" }
  session
end

#create_session_with_context(session_id, rack_env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a VectorMCP::Session with proper request context from Rack environment



99
100
101
102
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 99

def create_session_with_context(session_id, rack_env)
  request_context = VectorMCP::RequestContext.from_rack_env(rack_env, "http_stream")
  VectorMCP::Session.new(@transport.server, @transport, id: session_id, request_context: request_context)
end

#get_or_create_session(session_id = nil, rack_env = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override to add rack_env support



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 79

def get_or_create_session(session_id = nil, rack_env = nil)
  if session_id
    session = get_session(session_id)
    if session
      # Update existing session context if rack_env is provided
      if rack_env
        request_context = VectorMCP::RequestContext.from_rack_env(rack_env, "http_stream")
        session.context.request_context = request_context
      end
      return session
    end

    # If session_id was provided but not found, create with that ID
    return create_session(session_id, rack_env)
  end

  create_session(nil, rack_env)
end

#remove_streaming_connection(session) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Removes streaming connection from a session.

Parameters:

  • session (Session)

    The session to remove streaming from



141
142
143
144
145
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 141

def remove_streaming_connection(session)
  session.streaming_connection = nil
  session.touch!
  logger.debug { "Streaming connection removed: #{session.id}" }
end

#set_streaming_connection(session, connection) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Associates a streaming connection with a session.

Parameters:

  • session (Session)

    The session to associate with

  • connection (Object)

    The streaming connection object



131
132
133
134
135
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 131

def set_streaming_connection(session, connection)
  session.streaming_connection = connection
  session.touch!
  logger.debug { "Streaming connection associated: #{session.id}" }
end

#terminate_session(session_id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Terminates a session by ID.

rubocop:disable Naming/PredicateMethod

Parameters:

  • session_id (String)

    The session ID to terminate

Returns:

  • (Boolean)

    True if session was found and terminated



116
117
118
119
120
121
122
123
# File 'lib/vector_mcp/transport/http_stream/session_manager.rb', line 116

def terminate_session(session_id)
  session = @sessions.delete(session_id)
  return false unless session

  on_session_terminated(session)
  logger.info { "Session terminated: #{session_id}" }
  true
end