Class: Async::Idler
- Inherits:
- 
      Object
      
        - Object
- Async::Idler
 
- Defined in:
- lib/async/idler.rb
Overview
A load balancing mechanism that can be used process work when the system is idle.
Instance Method Summary collapse
- 
  
    
      #async(*arguments, parent: (@parent or Task.current), **options, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Wait until the system is idle, then execute the given block in a new task. 
- 
  
    
      #initialize(maximum_load = 0.8, backoff: 0.01, parent: nil)  ⇒ Idler 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Create a new idler. 
- 
  
    
      #wait  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Wait until the system is idle, according to the maximum load specified. 
Constructor Details
#initialize(maximum_load = 0.8, backoff: 0.01, parent: nil) ⇒ Idler
Create a new idler.
| 16 17 18 19 20 | # File 'lib/async/idler.rb', line 16 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
Wait until the system is idle, then execute the given block in a new task.
| 30 31 32 33 34 35 | # File 'lib/async/idler.rb', line 30 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
Wait until the system is idle, according to the maximum load specified.
If the scheduler is overloaded, this method will sleep for an exponentially increasing amount of time.
| 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | # File 'lib/async/idler.rb', line 40 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 |