Module: Async::Enumerable::ConcurrencyBounder Private

Defined in:
lib/async/enumerable/concurrency_bounder.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Provides bounded concurrency control for async operations. See docs/reference/concurrency_bounder.md for detailed documentation.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
# File 'lib/async/enumerable/concurrency_bounder.rb', line 9

def self.included(base) = base.include(Configurable)

Instance Method Details

#__async_enumerable_bounded_concurrency(early_termination: false, limit: nil) {|barrier| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Executes block with bounded concurrency.

Parameters:

  • early_termination (Boolean) (defaults to: false)

    Support early stop

Yields:

  • (barrier)

    Barrier for spawning async tasks



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/async/enumerable/concurrency_bounder.rb', line 14

def __async_enumerable_bounded_concurrency(early_termination: false, limit: nil, &block)
  Sync do |parent|
    limit ||= __async_enumerable_config.max_fibers
    semaphore = Async::Semaphore.new(limit, parent:)
    barrier = Async::Barrier.new(parent: semaphore)

    # Yield the barrier for task spawning
    yield barrier

    # Wait for all tasks to complete (or early termination)
    if early_termination
      begin
        barrier.wait
      rescue Async::Stop
        # Expected when barrier.stop is called for early termination
      end
    else
      barrier.wait
    end
  end
end