Class: FiberPool
- Inherits:
-
Object
- Object
- FiberPool
- Defined in:
- lib/fiberpool/fiberpool.rb
Instance Attribute Summary collapse
-
#fibers ⇒ Object
Returns the value of attribute fibers.
-
#pool_fiber ⇒ Object
Returns the value of attribute pool_fiber.
-
#pool_size ⇒ Object
Returns the value of attribute pool_size.
Class Method Summary collapse
Instance Method Summary collapse
- #add(&block) ⇒ Object
- #add_to_pool(fiber) ⇒ Object
- #drain ⇒ Object
- #fibers_in_use ⇒ Object
- #fibers_left_to_process? ⇒ Boolean
-
#initialize(pool_size = 10) ⇒ FiberPool
constructor
A new instance of FiberPool.
- #over_capacity? ⇒ Boolean
- #remove_fiber_from_pool(fiber) ⇒ Object
- #wait_for_free_pool_space ⇒ Object
- #wait_for_next_complete_fiber ⇒ Object
Constructor Details
#initialize(pool_size = 10) ⇒ FiberPool
Returns a new instance of FiberPool.
5 6 7 8 9 |
# File 'lib/fiberpool/fiberpool.rb', line 5 def initialize(pool_size=10) self.pool_size = pool_size self.fibers = [] self.pool_fiber = Fiber.current end |
Instance Attribute Details
#fibers ⇒ Object
Returns the value of attribute fibers.
3 4 5 |
# File 'lib/fiberpool/fiberpool.rb', line 3 def fibers @fibers end |
#pool_fiber ⇒ Object
Returns the value of attribute pool_fiber.
3 4 5 |
# File 'lib/fiberpool/fiberpool.rb', line 3 def pool_fiber @pool_fiber end |
#pool_size ⇒ Object
Returns the value of attribute pool_size.
3 4 5 |
# File 'lib/fiberpool/fiberpool.rb', line 3 def pool_size @pool_size end |
Class Method Details
.start(pool_size = 10, finished_callback = nil, &block) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/fiberpool/fiberpool.rb', line 11 def self.start(pool_size=10, finished_callback=nil, &block) Fiber.new do pool = FiberPool.new(pool_size) yield pool pool.drain finished_callback.call() if finished_callback end.resume end |
Instance Method Details
#add(&block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fiberpool/fiberpool.rb', line 20 def add(&block) fiber = Fiber.new do f = Fiber.current completion_callback = proc do pool_fiber.transfer(f) end yield completion_callback end add_to_pool(fiber) end |
#add_to_pool(fiber) ⇒ Object
31 32 33 34 35 |
# File 'lib/fiberpool/fiberpool.rb', line 31 def add_to_pool(fiber) wait_for_free_pool_space if over_capacity? fibers << fiber remove_fiber_from_pool fiber.resume end |
#drain ⇒ Object
61 62 63 |
# File 'lib/fiberpool/fiberpool.rb', line 61 def drain wait_for_free_pool_space while fibers_left_to_process? end |
#fibers_in_use ⇒ Object
49 50 51 |
# File 'lib/fiberpool/fiberpool.rb', line 49 def fibers_in_use fibers.size end |
#fibers_left_to_process? ⇒ Boolean
53 54 55 |
# File 'lib/fiberpool/fiberpool.rb', line 53 def fibers_left_to_process? fibers_in_use > 0 end |
#over_capacity? ⇒ Boolean
45 46 47 |
# File 'lib/fiberpool/fiberpool.rb', line 45 def over_capacity? fibers_in_use >= pool_size end |
#remove_fiber_from_pool(fiber) ⇒ Object
57 58 59 |
# File 'lib/fiberpool/fiberpool.rb', line 57 def remove_fiber_from_pool(fiber) fibers.delete(fiber) end |
#wait_for_free_pool_space ⇒ Object
37 38 39 |
# File 'lib/fiberpool/fiberpool.rb', line 37 def wait_for_free_pool_space remove_fiber_from_pool(wait_for_next_complete_fiber) end |
#wait_for_next_complete_fiber ⇒ Object
41 42 43 |
# File 'lib/fiberpool/fiberpool.rb', line 41 def wait_for_next_complete_fiber Fiber.yield end |