Class: McpInspector::Web::ConnectionPool
- Inherits:
-
Object
- Object
- McpInspector::Web::ConnectionPool
- Defined in:
- lib/mcp_inspector/web/connection_pool.rb
Instance Method Summary collapse
-
#cleanup_idle_connections ⇒ Object
Clean up idle connections.
-
#disconnect_all ⇒ Object
Disconnect all connections.
-
#get_or_create_connection(server_config) ⇒ Object
Get an existing connection or create a new one.
-
#initialize ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
-
#remove_connection(server_name) ⇒ Object
Remove a specific connection.
-
#with_connection(server_config) ⇒ Object
Get a connection for a server, reusing if available.
Constructor Details
#initialize ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
8 9 10 11 12 13 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 8 def initialize @connections = {} @mutex = Mutex.new @last_used = {} @max_idle_time = 300 # 5 minutes end |
Instance Method Details
#cleanup_idle_connections ⇒ Object
Clean up idle connections
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 70 def cleanup_idle_connections @mutex.synchronize do now = Time.now @connections.keys.each do |server_name| last_used = @last_used[server_name] if last_used && (now - last_used) > @max_idle_time remove_connection(server_name) end end end end |
#disconnect_all ⇒ Object
Disconnect all connections
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 85 def disconnect_all @mutex.synchronize do @connections.each do |server_name, client| begin client.disconnect rescue => e # Ignore disconnect errors end end @connections.clear @last_used.clear end end |
#get_or_create_connection(server_config) ⇒ Object
Get an existing connection or create a new one
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 27 def get_or_create_connection(server_config) @mutex.synchronize do server_name = server_config.name # Check if we have an existing connection if @connections[server_name] client = @connections[server_name] # Verify the connection is still alive if connection_alive?(client) update_last_used(server_name) return client else # Connection is dead, remove it remove_connection(server_name) end end # Create new connection client = create_connection(server_config) @connections[server_name] = client update_last_used(server_name) client end end |
#remove_connection(server_name) ⇒ Object
Remove a specific connection
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 55 def remove_connection(server_name) @mutex.synchronize do if @connections[server_name] begin @connections[server_name].disconnect rescue => e # Ignore disconnect errors end @connections.delete(server_name) @last_used.delete(server_name) end end end |
#with_connection(server_config) ⇒ Object
Get a connection for a server, reusing if available
16 17 18 19 20 21 22 23 24 |
# File 'lib/mcp_inspector/web/connection_pool.rb', line 16 def with_connection(server_config) client = get_or_create_connection(server_config) begin yield client ensure update_last_used(server_config.name) end end |