class RBThreadPool::Base

Attributes

config[RW]
daemon[R]
elastic[RW]
mutex[RW]
queue[RW]
th_elastic_pool[RW]
th_pool[RW]
thread_manager[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/core/base.rb, line 13
def initialize(config = {})
  @queue = Queue.new
  @mutex = Mutex.new
  @th_pool = [] # common pool
  @th_elastic_pool = [] # elastic pool, if queue become empty, elastic thread will destroy itself
  @config = config.dup
  @thread_manager = RBThreadManage.new(@queue, @config[:ex_quit] || false, @config[:logger])
  @elastic_manager = RBThreadElastic.new(@queue, @config[:ex_quit] || false, @config[:logger])
  @min = @config[:min] || 5
  @max = @config[:max] || 10
  @elastic_amount = @max - @min
  # generate elastic thread when size over alert_line
  @queue_limit = @config[:limit] || 100
  @alert_line = (@queue_limit / 2).floor
  # Daemon Thread manager
  @daemon = RBThreadDaemon.new(@mutex, @th_pool, @th_elastic_pool, @thread_manager, @config)
end

Public Instance Methods

<<(obj)
Alias for: push
add(&blk) click to toggle source

add with block

# File lib/core/base.rb, line 47
def add(&blk)
  push(blk)
end
Also aliased as: pushb
daemon_th_inspect() click to toggle source
# File lib/core/base.rb, line 53
def daemon_th_inspect
  @daemon.daemon_th.inspect
end
push(obj) click to toggle source

add proc

# File lib/core/base.rb, line 37
def push(obj)
  @mutex.synchronize{ @th_elastic_pool << @elastic_manager.fork } if
      @queue.size > @alert_line and @th_elastic_pool.size < @elastic_amount
  raise Exception, 'queue was full' if @queue.size >= @queue_limit
  @queue.push(obj)
end
Also aliased as: <<
pushb(&blk)
Alias for: add
start!() click to toggle source
# File lib/core/base.rb, line 31
def start!
  @daemon.run! # run daemon to protect the thread poll
  generate_threads(@min) # generate common thread
end

Private Instance Methods

generate_threads(num) click to toggle source
# File lib/core/base.rb, line 59
def generate_threads(num)
  num.times do
    @mutex.synchronize {@th_pool << @thread_manager.fork}
  end
end