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

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

Instance Method Summary collapse

Instance Method Details

#rescue_throttled_by(throttle, *args, &block) ⇒ Future

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



191
192
193
# File 'lib-edge/concurrent/edge/throttle.rb', line 191

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

#then_throttled_by(throttle, *args, &block) ⇒ Future

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:

See Also:



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

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