Class: ActiveMatrix::ClientPool::HomeserverPool

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

Overview

Pool for a specific homeserver

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger, #logger=

Constructor Details

#initialize(homeserver, max_size:, timeout:) ⇒ HomeserverPool

Returns a new instance of HomeserverPool.



74
75
76
77
78
79
80
81
82
# File 'lib/active_matrix/client_pool.rb', line 74

def initialize(homeserver, max_size:, timeout:)
  @homeserver = homeserver
  @max_size = max_size
  @timeout = timeout
  @available = []
  @in_use = {}
  @mutex = Mutex.new
  @condition = ConditionVariable.new
end

Instance Attribute Details

#homeserverObject (readonly)

Returns the value of attribute homeserver.



72
73
74
# File 'lib/active_matrix/client_pool.rb', line 72

def homeserver
  @homeserver
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



72
73
74
# File 'lib/active_matrix/client_pool.rb', line 72

def max_size
  @max_size
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



72
73
74
# File 'lib/active_matrix/client_pool.rb', line 72

def timeout
  @timeout
end

Instance Method Details

#available_countObject



131
132
133
# File 'lib/active_matrix/client_pool.rb', line 131

def available_count
  @mutex.synchronize { @available.size }
end

#checkin(client) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_matrix/client_pool.rb', line 109

def checkin(client)
  @mutex.synchronize do
    entry = @in_use.delete(client.object_id)
    return unless entry

    # Add back to available pool if still valid
    if client_valid?(client)
      @available << client
    else
      # Client is no longer valid, don't return to pool
      logger.debug "Discarding invalid client for #{@homeserver}"
    end

    # Signal waiting threads
    @condition.signal
  end
end

#checkoutObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_matrix/client_pool.rb', line 84

def checkout(**)
  @mutex.synchronize do
    # Try to find an available client
    client = find_available_client

    # Create new client if needed and pool not full
    client = create_client(**) if client.nil? && @in_use.size < @max_size

    # Wait for a client if pool is full
    while client.nil?
      @condition.wait(@mutex, 1)
      client = find_available_client
    end

    # Mark as in use
    @available.delete(client)
    @in_use[client.object_id] = {
      client: client,
      checked_out_at: Time.current
    }

    client
  end
end

#clear!Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/active_matrix/client_pool.rb', line 139

def clear!
  @mutex.synchronize do
    # Stop all clients
    (@available + @in_use.values.map { |e| e[:client] }).each do |client|
      client.stop_listener_thread if client.listening?
      client.logout if client.logged_in?
    rescue StandardError => e
      logger.error "Error cleaning up client: #{e.message}"
    end

    @available.clear
    @in_use.clear
  end
end

#in_use_countObject



135
136
137
# File 'lib/active_matrix/client_pool.rb', line 135

def in_use_count
  @mutex.synchronize { @in_use.size }
end

#sizeObject



127
128
129
# File 'lib/active_matrix/client_pool.rb', line 127

def size
  @mutex.synchronize { @available.size + @in_use.size }
end