Module: Concurrent::Promises::Future::ThrottleIntegration — Concurrent

Class List Method List File List

# Module: Concurrent::Promises::Future::ThrottleIntegration

Included in:
  Concurrent::Promises::Future
Defined in:
  lib/concurrent/edge/throttle.rb

## Instance Method Summary (collapse)

- - (Promises::Future) rescue_throttled_by(throttle, *args, &block)

  Behaves as #rescue but the it is throttled.

- - (Promises::Future) then_throttled_by(throttle, *args, &block)

  Behaves as #then but the it is throttled.

## Instance Method Details

### - (`Promises::Future`) rescue_throttled_by(throttle, *args, &block)

Behaves as Concurrent::Promises::Future#rescue but the it is throttled.

Returns:

- (`Promises::Future`)

See Also:

- Concurrent::Promises::Future#rescue

    184
    185
    186
    # File 'lib/concurrent/edge/throttle.rb', line 184

    def rescue_throttled_by(throttle, *args, &block)
      throttled_by(throttle) { |trigger| trigger.rescue(*args, &block) }
    end

### - (`Promises::Future`) then_throttled_by(throttle, *args, &block)

Behaves as Concurrent::Promises::Future#then but the it is throttled.

Examples:

    data     = (1..5).to_a
    db       = data.reduce({}) { |h, v| h.update v => v.to_s }
    max_two  = Throttle.new 2

    futures = data.map do |data|
      Promises.future(data) do |data|
        # un-throttled, concurrency level equal data.size
        data + 1
      end.then_throttled_by(max_two, db) do |v, db|
        # throttled, only 2 tasks executed at the same time
        # e.g. limiting access to db
        db[v]
      end
    end

    futures.map(&:value!) # => [2, 3, 4, 5, nil]

Returns:

- (`Promises::Future`)

See Also:

- Concurrent::Promises::Future#then

    177
    178
    179
    # File 'lib/concurrent/edge/throttle.rb', line 177

    def then_throttled_by(throttle, *args, &block)
      throttled_by(throttle) { |trigger| trigger.then(*args, &block) }
    end