Class: Faraday::HappyEyeballs::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/happy_eyeballs/connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



6
7
8
9
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 6

def initialize(options = {})
@options = options
@connections = {}
@connection_mutex = Mutex.new end

Instance Method Details

#cache_connection(connection_key, address) ⇒ Object



23
24
25
26
27
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 23

def cache_connection(connection_key, address)
   @connection_mutex.synchronize do
     @connections[connection_key] = {
address: address,
timestamp: Time.now } end end

#cleanup_expired_connectionsObject

Clean up expired connections



42
43
44
45
46
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 42

def cleanup_expired_connections # Clean up expired connections
   current_time = Time.now
   ttl = @options.fetch(:cache_ttl, 300)
   @connection_mutex.synchronize do
@connections.reject! do |_key, cached| current_time - cached[:timestamp] > ttl end end end

#clear_all_connectionsObject



40
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 40

def clear_all_connections() @connection_mutex.synchronize do @connections.clear end end

#clear_connection(connection_key) ⇒ Object



39
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 39

def clear_connection(connection_key) @connection_mutex.synchronize do @connections.delete(connection_key) end end

#connection_countObject



41
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 41

def connection_count() @connection_mutex.synchronize do @connections.size end end

#get_connection(connection_key) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 29

def get_connection(connection_key)
   @connection_mutex.synchronize do
cached = @connections[connection_key]
return nil unless cached
# Check TTL
if Time.now - cached[:timestamp] > @options.fetch(:cache_ttl, 300)
  @connections.delete(connection_key)
  return nil end
cached[:address] end end

#has_connection?(connection_key) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/faraday/happy_eyeballs/connection_pool.rb', line 11

def has_connection?(connection_key)
   @connection_mutex.synchronize do
cached = @connections[connection_key]
return false unless cached
# Check if cached connection is still valid
if Time.now - cached[:timestamp] > @options.fetch(:cache_ttl, 300)
  @connections.delete(connection_key)
  return false end
# Optionally test if the connection is still alive
if @options.fetch(:test_cached_connections, false) then return test_connection_alive(cached[:address]) end
true end end