Class: NeverBlock::Pool::FiberPool

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

Overview

Author

Mohammad A. Ali ([email protected])

Copyright

Copyright © 2008 eSpace, Inc.

License

Distributes under the same terms as Ruby

A pool of initialized fibers It does not grow in size or create transient fibers It will queue code blocks when needed (if all its fibers are busy)

This class is particulary useful when you use the fibers to connect to evented back ends. It also does not generate transient objects and thus saves memory.

Example: fiber_pool = NeverBlock::Pool::FiberPool.new(150)

loop do

fiber_pool.spawn do
  #fiber body goes here 
end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count = 50) ⇒ 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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/never_block/pool/fiber_pool.rb', line 33

def initialize(count = 50)
  @fibers,@busy_fibers,@queue = [],{},[]
  count.times do |i|
    fiber = Fiber.new do |block|
      loop do
        block.call
        # callbacks are called in a reverse order, much like c++ destructor
        Fiber.current[:callbacks].pop.call while Fiber.current[:callbacks].length > 0
        unless @queue.empty?
          block = @queue.shift
        else
          @busy_fibers.delete(Fiber.current.object_id)
          @fibers << Fiber.current
          block = Fiber.yield
        end
      end
    end
    fiber[:callbacks] = []
    fiber[:em_keys] = []
    fiber[:neverblock] = true
    @fibers << fiber
  end
end

Instance Attribute Details

#fibersObject (readonly)

gives access to the currently free fibers



28
29
30
# File 'lib/never_block/pool/fiber_pool.rb', line 28

def fibers
  @fibers
end

Instance Method Details

#spawn(evented = true, &block) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/never_block/pool/fiber_pool.rb', line 59

def spawn(evented = true, &block)
  if fiber = @fibers.shift
    fiber[:callbacks] = []
    @busy_fibers[fiber.object_id] = fiber
    fiber[:neverblock] = evented
    fiber.resume(block)
  else
    @queue << block
  end
  self # we are keen on hiding our queue
end