Class: ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/wayback_machine_downloader.rb

Constant Summary collapse

MAX_AGE =
300
CLEANUP_INTERVAL =
60
DEFAULT_TIMEOUT =
30
MAX_RETRIES =
3

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



22
23
24
25
26
27
# File 'lib/wayback_machine_downloader.rb', line 22

def initialize(size)
  @size = size
  @pool = Concurrent::Map.new
  @creation_times = Concurrent::Map.new
  @cleanup_thread = schedule_cleanup
end

Instance Method Details

#shutdownObject



38
39
40
41
42
43
# File 'lib/wayback_machine_downloader.rb', line 38

def shutdown
  @cleanup_thread&.exit
  @pool.each_value { |conn| conn.finish if conn&.started? }
  @pool.clear
  @creation_times.clear
end

#with_connection(&block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/wayback_machine_downloader.rb', line 29

def with_connection(&block)
  conn = acquire_connection
  begin
    yield conn
  ensure
    release_connection(conn)
  end
end