Class: RBThreadPool::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/core/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/core/base.rb', line 8

def config
  @config
end

#daemonObject (readonly)

Returns the value of attribute daemon.



11
12
13
# File 'lib/core/base.rb', line 11

def daemon
  @daemon
end

#elasticObject

Returns the value of attribute elastic.



8
9
10
# File 'lib/core/base.rb', line 8

def elastic
  @elastic
end

#mutexObject

Returns the value of attribute mutex.



8
9
10
# File 'lib/core/base.rb', line 8

def mutex
  @mutex
end

#queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/core/base.rb', line 8

def queue
  @queue
end

#th_elastic_poolObject

Returns the value of attribute th_elastic_pool.



8
9
10
# File 'lib/core/base.rb', line 8

def th_elastic_pool
  @th_elastic_pool
end

#th_poolObject

Returns the value of attribute th_pool.



8
9
10
# File 'lib/core/base.rb', line 8

def th_pool
  @th_pool
end

#thread_managerObject

Returns the value of attribute thread_manager.



8
9
10
# File 'lib/core/base.rb', line 8

def thread_manager
  @thread_manager
end

Instance Method Details

#add(&blk) ⇒ Object Also known as: pushb

add with block



47
48
49
# File 'lib/core/base.rb', line 47

def add(&blk)
  push(blk)
end

#daemon_th_inspectObject



53
54
55
# File 'lib/core/base.rb', line 53

def daemon_th_inspect
  @daemon.daemon_th.inspect
end

#push(obj) ⇒ Object Also known as: <<

add proc

Raises:



37
38
39
40
41
42
# 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

#start!Object



31
32
33
34
# 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