Class: S3Light::ConnectionsManager::Threaded

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-light/connections_manager/threaded.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, concurrency) ⇒ Threaded

Returns a new instance of Threaded.



6
7
8
9
10
# File 'lib/s3-light/connections_manager/threaded.rb', line 6

def initialize(client, concurrency)
  @client = client
  @thread_pool = Concurrent::FixedThreadPool.new(concurrency)
  @connections = Concurrent::Array.new
end

Instance Method Details

#closeObject



25
26
27
28
# File 'lib/s3-light/connections_manager/threaded.rb', line 25

def close
  @connections.each(&:close)
  @connections = Concurrent::Array.new
end

#wait_to_finishObject



30
31
32
33
# File 'lib/s3-light/connections_manager/threaded.rb', line 30

def wait_to_finish
  @thread_pool.shutdown
  @thread_pool.wait_for_termination
end

#with_connectionObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/s3-light/connections_manager/threaded.rb', line 12

def with_connection
  @thread_pool.post do
    existing_connection = Thread.current[:connection]
    yield existing_connection if existing_connection

    new_connection = create_new_connection
    @connections << new_connection
    Thread.current[:connection] = new_connection

    yield new_connection
  end
end