Class: CleoQualityReview::ConcurrentExecutor

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

Overview

Runs independent, blocking work items across a bounded pool of threads.

The pool is sized to the available processor count by default, so it naturally expands or contracts with the host. When there are more work items than workers, the surplus waits on an internal queue and is picked up as workers free up. Results are returned in the same order as the input items.

Suited to I/O-bound work such as shelling out to external tools: while a worker thread blocks on a subprocess, Ruby releases the GIL so other workers make real progress.

Instance Method Summary collapse

Constructor Details

#initialize(max_workers: nil) ⇒ ConcurrentExecutor

Returns a new instance of ConcurrentExecutor.

Parameters:

  • max_workers (Integer, nil) (defaults to: nil)

    explicit worker cap, or nil to use configuration



21
22
23
24
25
26
27
# File 'lib/cleo_quality_review/concurrent_executor.rb', line 21

def initialize(max_workers: nil)
  @max_workers = if max_workers
                   Configuration.max_concurrency_limit(max_workers)
                 else
                   Configuration.max_concurrency
                 end
end

Instance Method Details

#map(items) {|item| ... } ⇒ Array

Map over items concurrently, preserving input order.

Parameters:

  • items (Array)

    work items to process

Yields:

  • (item)

    the work performed for each item

Returns:

  • (Array)

    results aligned with items



34
35
36
37
38
39
# File 'lib/cleo_quality_review/concurrent_executor.rb', line 34

def map(items, &block)
  return [] if items.empty?
  return items.map(&block) if serial?(items.size)

  process(items, &block)
end