Class: ActiveMatrix::ClientPool

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

Overview

Manages a pool of Matrix client connections for efficiency

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.



12
13
14
15
16
# File 'lib/active_matrix/client_pool.rb', line 12

def initialize
  @pools = Concurrent::Hash.new
  @config = ActiveMatrix.config
  @mutex = Mutex.new
end

Instance Method Details

#checkin(client) ⇒ Object

Return a client to the pool



27
28
29
30
31
# File 'lib/active_matrix/client_pool.rb', line 27

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

#clear!Object

Clear all pools



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

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



19
20
21
22
23
24
# File 'lib/active_matrix/client_pool.rb', line 19

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

#shutdownObject

Shutdown all pools



54
55
56
# File 'lib/active_matrix/client_pool.rb', line 54

def shutdown
  clear!
end

#statsObject

Get pool statistics



34
35
36
37
38
39
40
41
42
43
# File 'lib/active_matrix/client_pool.rb', line 34

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