Class: ActiveMatrix::ClientPool

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/active_matrix/client_pool.rb

Overview

Manages Matrix client connections per homeserver with rate limiting.

NOTE: Despite the name, this is not a traditional connection pool. Each agent gets a dedicated long-lived client. The “pool” provides:

  • Semaphore-based rate limiting on client creation per homeserver

  • Tracking of active clients for health monitoring

Clients are NOT returned to the pool after use - they remain active for the agent’s lifetime. The semaphore prevents too many agents from connecting to a single homeserver simultaneously.

Defined Under Namespace

Classes: HomeserverPool

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initializeClientPool

Returns a new instance of ClientPool.



24
25
26
27
28
# File 'lib/active_matrix/client_pool.rb', line 24

def initialize
  @pools = {}
  @config = ActiveMatrix.config
  @mutex = Mutex.new
end

Instance Method Details

#checkin(client) ⇒ Object

Return a client to the pool



37
38
39
40
41
# File 'lib/active_matrix/client_pool.rb', line 37

def checkin(client)
  homeserver = client.homeserver
  pool = @pools[homeserver]
  pool&.checkin(client)
end

#clear!Object

Clear all pools



56
57
58
59
60
61
# File 'lib/active_matrix/client_pool.rb', line 56

def clear!
  @mutex.synchronize do
    @pools.each_value(&:clear!)
    @pools.clear
  end
end

#get_client(homeserver) ⇒ Object

Get or create a client for a homeserver



31
32
33
34
# File 'lib/active_matrix/client_pool.rb', line 31

def get_client(homeserver, **)
  pool = @mutex.synchronize { get_or_create_pool(homeserver) }
  pool.checkout(**)
end

#shutdownObject

Shutdown all pools



64
65
66
# File 'lib/active_matrix/client_pool.rb', line 64

def shutdown
  clear!
end

#statsObject

Get pool statistics



44
45
46
47
48
49
50
51
52
53
# File 'lib/active_matrix/client_pool.rb', line 44

def stats
  @pools.map do |homeserver, pool|
    {
      homeserver: homeserver,
      size: pool.size,
      available: pool.available_count,
      in_use: pool.in_use_count
    }
  end
end