Class: Fibre::FiberPool
- Inherits:
-
Object
- Object
- Fibre::FiberPool
- Defined in:
- lib/fibre/fiber_pool.rb
Instance Attribute Summary collapse
-
#pool_queue_size ⇒ Object
readonly
Returns the value of attribute pool_queue_size.
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#reserved ⇒ Object
readonly
Returns the value of attribute reserved.
Instance Method Summary collapse
-
#checkout(&b) ⇒ Object
Borrow fiber from the pool and call the block inside.
-
#initialize(pool_size: DEFAULT_POOL_SIZE, pool_queue_size: DEFAULT_POOL_QUEUE_SIZE) ⇒ FiberPool
constructor
Initialize fiber’s pool.
Constructor Details
#initialize(pool_size: DEFAULT_POOL_SIZE, pool_queue_size: DEFAULT_POOL_QUEUE_SIZE) ⇒ FiberPool
Initialize fiber’s pool
22 23 24 25 26 27 28 |
# File 'lib/fibre/fiber_pool.rb', line 22 def initialize(pool_size: DEFAULT_POOL_SIZE, pool_queue_size: DEFAULT_POOL_QUEUE_SIZE) @pool_size = pool_size @pool_queue_size = pool_queue_size @reserved = {} @queue = [] @pool = @pool_size.times.collect { ::Fiber.new(&self.method(:fiber_entry)) } end |
Instance Attribute Details
#pool_queue_size ⇒ Object (readonly)
Returns the value of attribute pool_queue_size.
17 18 19 |
# File 'lib/fibre/fiber_pool.rb', line 17 def pool_queue_size @pool_queue_size end |
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
16 17 18 |
# File 'lib/fibre/fiber_pool.rb', line 16 def pool_size @pool_size end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
19 20 21 |
# File 'lib/fibre/fiber_pool.rb', line 19 def queue @queue end |
#reserved ⇒ Object (readonly)
Returns the value of attribute reserved.
18 19 20 |
# File 'lib/fibre/fiber_pool.rb', line 18 def reserved @reserved end |
Instance Method Details
#checkout(&b) ⇒ Object
Borrow fiber from the pool and call the block inside
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/fibre/fiber_pool.rb', line 31 def checkout(&b) spec = { block: b, parent: ::Fiber.current } if @pool.empty? raise "The fiber queue has been overflowed" if @queue.size > @pool_queue_size @queue.push spec return end @pool.shift.tap do |fiber| @reserved[fiber.object_id] = spec fiber.resume(spec) end self end |