Class: ActiveMatrix::ClientPool::HomeserverPool
- Inherits:
-
Object
- Object
- ActiveMatrix::ClientPool::HomeserverPool
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
#homeserver ⇒ Object
Returns the value of attribute homeserver.
72
73
74
|
# File 'lib/active_matrix/client_pool.rb', line 72
def homeserver
@homeserver
end
|
#max_size ⇒ Object
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
|
#timeout ⇒ Object
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_count ⇒ Object
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
if client_valid?(client)
@available << client
else
logger.debug "Discarding invalid client for #{@homeserver}"
end
@condition.signal
end
end
|
#checkout ⇒ Object
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
client = find_available_client
client = create_client(**) if client.nil? && @in_use.size < @max_size
while client.nil?
@condition.wait(@mutex, 1)
client = find_available_client
end
@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
(@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_count ⇒ Object
135
136
137
|
# File 'lib/active_matrix/client_pool.rb', line 135
def in_use_count
@mutex.synchronize { @in_use.size }
end
|
#size ⇒ Object
127
128
129
|
# File 'lib/active_matrix/client_pool.rb', line 127
def size
@mutex.synchronize { @available.size + @in_use.size }
end
|