Class: OverSIP::FiberPool

Inherits:
Object
  • Object
show all
Defined in:
lib/oversip/fiber_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(count = 100) ⇒ FiberPool

Prepare a list of fibers that are able to run different blocks of code every time. Once a fiber is done with its block, it attempts to fetch another one from the queue.



9
10
11
12
13
14
15
# File 'lib/oversip/fiber_pool.rb', line 9

def initialize count = 100
  @fibers,@busy_fibers,@queue = [],{},[]

  count.times do |i|
    add_fiber()
  end
end

Instance Method Details

#spawn(&block) ⇒ Object

If there is an available fiber use it, otherwise, leave it to linger in a queue.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oversip/fiber_pool.rb', line 38

def spawn &block
  # resurrect dead fibers
  @busy_fibers.values.reject(&:alive?).each do |f|
    @busy_fibers.delete f.object_id
    add_fiber()
  end

  if (fiber = @fibers.shift)
    @busy_fibers[fiber.object_id] = fiber
    fiber.resume block
  else
    @queue << block
  end

  fiber
end