Class: Async::Idler
- Inherits:
-
Object
- Object
- Async::Idler
- Defined in:
- lib/async/idler.rb
Instance Method Summary collapse
- #async(*arguments, parent: (@parent or Task.current), **options, &block) ⇒ Object
-
#initialize(maximum_load = 0.8, backoff: 0.01, parent: nil) ⇒ Idler
constructor
A new instance of Idler.
- #wait ⇒ Object
Constructor Details
#initialize(maximum_load = 0.8, backoff: 0.01, parent: nil) ⇒ Idler
Returns a new instance of Idler.
8 9 10 11 12 |
# File 'lib/async/idler.rb', line 8 def initialize(maximum_load = 0.8, backoff: 0.01, parent: nil) @maximum_load = maximum_load @backoff = backoff @parent = parent end |
Instance Method Details
#async(*arguments, parent: (@parent or Task.current), **options, &block) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/async/idler.rb', line 14 def async(*arguments, parent: (@parent or Task.current), **, &block) wait # It is crucial that we optimistically execute the child task, so that we prevent a tight loop invoking this method from consuming all available resources. parent.async(*arguments, **, &block) end |
#wait ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/async/idler.rb', line 21 def wait scheduler = Fiber.scheduler backoff = nil while true load = scheduler.load break if load < @maximum_load if backoff sleep(backoff) backoff *= 2.0 else scheduler.yield backoff = @backoff end end end |