Class: ToggleCraft::ConnectionPool
- Inherits:
-
Object
- Object
- ToggleCraft::ConnectionPool
- Defined in:
- lib/togglecraft/connection_pool.rb
Overview
Connection pool manager for SSE connections Ensures that multiple SDK instances can share the same SSE connection
Class Method Summary collapse
-
.clear(force: false) ⇒ void
Clear all connections.
-
.connection_count ⇒ Integer
Get the number of active connections.
-
.connections ⇒ Concurrent::Map
Get the connections map.
-
.generate_pool_key(config) ⇒ String
Generate a pool key from configuration.
-
.get_connection(config) ⇒ SharedSSEConnection?
Get or create a shared connection.
-
.get_connection_by_key(pool_key) ⇒ SharedSSEConnection?
Get connection by pool key (for testing/debugging).
-
.has_connection?(pool_key) ⇒ Boolean
Check if a specific pool key has an active connection.
-
.set_debug(enabled) ⇒ void
Enable or disable debug logging.
-
.stats ⇒ Hash
Get statistics about the connection pool.
-
.total_client_count ⇒ Integer
Get the total number of clients across all connections.
Class Method Details
.clear(force: false) ⇒ void
This method returns an undefined value.
Clear all connections
82 83 84 85 |
# File 'lib/togglecraft/connection_pool.rb', line 82 def clear(force: false) connections.each_value(&:force_disconnect) if force connections.clear end |
.connection_count ⇒ Integer
Get the number of active connections
98 99 100 |
# File 'lib/togglecraft/connection_pool.rb', line 98 def connection_count connections.size end |
.connections ⇒ Concurrent::Map
Get the connections map
10 11 12 |
# File 'lib/togglecraft/connection_pool.rb', line 10 def connections @connections ||= Concurrent::Map.new end |
.generate_pool_key(config) ⇒ String
Generate a pool key from configuration
51 52 53 54 55 56 57 58 59 |
# File 'lib/togglecraft/connection_pool.rb', line 51 def generate_pool_key(config) # Allow custom pool key for advanced users return config[:connection_pool_key] if config[:connection_pool_key] # Generate key from connection parameters url = config[:url] || 'https://sse.togglecraft.io' sdk_key = config[:sdk_key] "#{url}:#{sdk_key}" end |
.get_connection(config) ⇒ SharedSSEConnection?
Get or create a shared connection
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/togglecraft/connection_pool.rb', line 17 def get_connection(config) # If connection sharing is disabled, return nil to create a dedicated connection unless config[:share_connection] log 'Connection sharing disabled for this client' return nil end pool_key = generate_pool_key(config) log "Getting connection for pool key: #{pool_key}" # Check if connection already exists connection = connections[pool_key] if connection log "Reusing existing connection for #{pool_key} (#{connection.client_count} clients)" else # Create new shared SSE connection log "Creating new shared SSE connection for #{pool_key}" connection = SharedSSEConnection.new(config, pool_key) connections[pool_key] = connection # Set up cleanup when connection has no more clients connection.on_empty do log "Removing empty connection for #{pool_key}" connections.delete(pool_key) end end connection end |
.get_connection_by_key(pool_key) ⇒ SharedSSEConnection?
Get connection by pool key (for testing/debugging)
118 119 120 |
# File 'lib/togglecraft/connection_pool.rb', line 118 def get_connection_by_key(pool_key) connections[pool_key] end |
.has_connection?(pool_key) ⇒ Boolean
Check if a specific pool key has an active connection
111 112 113 |
# File 'lib/togglecraft/connection_pool.rb', line 111 def has_connection?(pool_key) connections.key?(pool_key) end |
.set_debug(enabled) ⇒ void
This method returns an undefined value.
Enable or disable debug logging
90 91 92 93 94 |
# File 'lib/togglecraft/connection_pool.rb', line 90 def set_debug(enabled) @debug = enabled # Also enable debug on existing connections connections.each_value { |conn| conn.set_debug(enabled) } end |
.stats ⇒ Hash
Get statistics about the connection pool
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/togglecraft/connection_pool.rb', line 63 def stats conn_stats = [] connections.each do |key, conn| conn_stats << { key: key, client_count: conn.client_count, connected: conn.connected? } end { total_connections: connections.size, connections: conn_stats } end |
.total_client_count ⇒ Integer
Get the total number of clients across all connections
104 105 106 |
# File 'lib/togglecraft/connection_pool.rb', line 104 def total_client_count connections.values.sum(&:client_count) end |