rb_thread_pool 0.0.0 → 0.0.1full
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/core/base.rb +63 -0
- data/lib/core/rb_daemon.rb +67 -0
- data/lib/core/rb_elastic.rb +39 -0
- data/lib/core/rb_thread.rb +42 -0
- data/lib/rb_thread_pool.rb +11 -29
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42b6d5142478f31a6f209cf6bd7cce82316eeb66
|
4
|
+
data.tar.gz: c3193d2037273006897636e22c58ac3d46f272ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b87abf11418ada1132d1c371772eee0b20ed91d95ad7293ad4d7f101acb9775edc25178a285c029a8a953154110dd6d8b1e9d679a4a9b5ac7bb4ffd16dc765be
|
7
|
+
data.tar.gz: 30181c7ba375dddf41644d1b5a097164cc63498a03b45e957524f2bc9c9bcbaa8a5fb1a491c1f6f851e26a1ab62545f8ea4ca599350063ae36478c61de1161ca
|
data/lib/core/base.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'core/rb_thread'
|
2
|
+
require 'core/rb_daemon'
|
3
|
+
require 'core/rb_elastic'
|
4
|
+
|
5
|
+
module RBThreadPool
|
6
|
+
class Base
|
7
|
+
|
8
|
+
attr_accessor :queue, :mutex, :th_pool, :th_elastic_pool,
|
9
|
+
:config, :thread_manager, :elastic
|
10
|
+
|
11
|
+
def initialize(config = {})
|
12
|
+
@queue = Queue.new
|
13
|
+
@mutex = Mutex.new
|
14
|
+
@th_pool = [] # common pool
|
15
|
+
@th_elastic_pool = [] # elastic pool, if queue become empty, elastic thread will destroy itself
|
16
|
+
@config = config.dup
|
17
|
+
@thread_manager = RBThreadManage.new(@queue, @config[:ex_quit] || false, @config[:logger])
|
18
|
+
@elastic_manager = RBThreadElastic.new(@queue, @config[:ex_quit] || false, @config[:logger])
|
19
|
+
@min = @config[:min] || 5
|
20
|
+
@max = @config[:max] || 10
|
21
|
+
@elastic_amount = @max - @min
|
22
|
+
# generate elastic thread when size over alert_line
|
23
|
+
@queue_limit = @config[:limit] || 100
|
24
|
+
@alert_line = (@queue_limit / 2).floor
|
25
|
+
# Daemon Thread
|
26
|
+
@daemon = RBThreadDaemon.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
def start!
|
30
|
+
@daemon.run! # run daemon to protect the thread poll
|
31
|
+
generate_threads(@min) # generate common thread
|
32
|
+
end
|
33
|
+
|
34
|
+
# def join_all
|
35
|
+
# Thread.list.each do |th|
|
36
|
+
# p th
|
37
|
+
# end
|
38
|
+
# @th_pool.each(&:join)
|
39
|
+
# end
|
40
|
+
|
41
|
+
def push(obj)
|
42
|
+
@mutex.synchronize{ @th_elastic_pool << @elastic_manager.fork } if
|
43
|
+
@queue.size > @alert_line and @th_elastic_pool.size < @elastic_amount
|
44
|
+
raise Exception, 'queue was full' if @queue.size >= @queue_limit
|
45
|
+
@queue.push(obj)
|
46
|
+
end
|
47
|
+
alias << push
|
48
|
+
|
49
|
+
def add(&blk)
|
50
|
+
push(blk)
|
51
|
+
end
|
52
|
+
alias pushb add
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def generate_threads(num)
|
57
|
+
num.times do
|
58
|
+
@mutex.synchronize {@th_pool << @thread_manager.fork}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
|
2
|
+
module RBThreadPool
|
3
|
+
|
4
|
+
class RBThreadDaemon
|
5
|
+
|
6
|
+
attr_accessor :condition
|
7
|
+
|
8
|
+
def initialize(base)
|
9
|
+
@mutex = base.mutex
|
10
|
+
@interval = base.config[:interval] || 20
|
11
|
+
@pool = base.th_pool
|
12
|
+
@elastic = base.elastic
|
13
|
+
@base_control = base
|
14
|
+
@condition = true
|
15
|
+
end
|
16
|
+
|
17
|
+
def run!
|
18
|
+
return generate_daemon
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def generate_daemon
|
24
|
+
return Thread.new do
|
25
|
+
Thread.current.name = 'RbThr:Daemon' if Thread.respond_to?(:name=)
|
26
|
+
interval = @interval
|
27
|
+
mutex = @mutex
|
28
|
+
pool = @pool
|
29
|
+
elastic = @elastic
|
30
|
+
while @condition
|
31
|
+
sleep(interval)
|
32
|
+
puts 'daemon wakeup'
|
33
|
+
mutex.synchronize { clean_common pool }
|
34
|
+
Thread.pass
|
35
|
+
mutex.synchronize { clean elastic } unless elastic.nil? || elastic.empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def clean_common(pool)
|
41
|
+
deads = clean(pool)
|
42
|
+
revive_common_thread deads if deads > 0
|
43
|
+
end
|
44
|
+
|
45
|
+
def clean(pool)
|
46
|
+
size = pool.size
|
47
|
+
count_dead = 0
|
48
|
+
size.times do |idx|
|
49
|
+
unless pool[idx].alive?
|
50
|
+
pool[idx] = nil
|
51
|
+
count_dead += 1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
return count_dead if count_dead == 0
|
55
|
+
pool.compact!
|
56
|
+
count_dead
|
57
|
+
end
|
58
|
+
|
59
|
+
def revive_common_thread(num)
|
60
|
+
manager = @base_control.thread_manager
|
61
|
+
pool = @pool
|
62
|
+
num.times do |idx|
|
63
|
+
pool << manager.fork
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module RBThreadPool
|
2
|
+
class RBThreadElastic
|
3
|
+
def initialize(queue,ex_quit = false , log = nil)
|
4
|
+
@queue = queue
|
5
|
+
@log = log
|
6
|
+
@ex_quit = ex_quit
|
7
|
+
@condition = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def fork
|
11
|
+
return generate_elastic_thread
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def generate_elastic_thread
|
17
|
+
return Thread.new do
|
18
|
+
Thread.current.name = "RbThr:Elastic #{Thread.current.to_s}" if Thread.respond_to?(:name=)
|
19
|
+
queue = @queue
|
20
|
+
while @condition
|
21
|
+
begin
|
22
|
+
task = queue.pop(true)
|
23
|
+
Thread.current.exit if task.nil?
|
24
|
+
task.call
|
25
|
+
rescue Interrupt
|
26
|
+
task.call unless task.nil?
|
27
|
+
Thread.current.exit
|
28
|
+
rescue => ex
|
29
|
+
raise Exception, ex.message if @log.nil?
|
30
|
+
@log.error("ElasticThread:: msg: #{ex.message}, backtrace: #{ex.backtrace}")
|
31
|
+
Thread.current.exit if @ex_quit
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
module RBThreadPool
|
3
|
+
class RBThreadManage
|
4
|
+
|
5
|
+
attr_accessor :while_condi
|
6
|
+
|
7
|
+
def initialize(queue, ex_quit = false, log = nil)
|
8
|
+
@queue = queue
|
9
|
+
@log = log
|
10
|
+
@ex_quit = ex_quit
|
11
|
+
@while_condi = true
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def fork
|
16
|
+
return generate_thread
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def generate_thread
|
23
|
+
return Thread.new do
|
24
|
+
Thread.current.name = "RbThr:Common #{Thread.current.to_s}" if Thread.respond_to?(:name=)
|
25
|
+
queue = @queue
|
26
|
+
begin
|
27
|
+
while @while_condi
|
28
|
+
task = queue.pop
|
29
|
+
task.call
|
30
|
+
end
|
31
|
+
rescue Interrupt
|
32
|
+
task.call unless task.nil?
|
33
|
+
Thread.current.exit
|
34
|
+
rescue => ex
|
35
|
+
raise Exception, ex.message if @log.nil?
|
36
|
+
@log.error("CommonThread:: msg: #{ex.message}, backtrace: #{ex.backtrace}")
|
37
|
+
Thread.current.exit if @ex_quit
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/rb_thread_pool.rb
CHANGED
@@ -2,35 +2,17 @@ $LOAD_PATH.unshift(__dir__)
|
|
2
2
|
require 'core/base'
|
3
3
|
|
4
4
|
|
5
|
-
#
|
6
|
-
# #
|
7
|
-
#
|
8
|
-
# #
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
5
|
+
# pool = RBThreadPool::Base.new
|
6
|
+
# # # #
|
7
|
+
# pool.start!
|
8
|
+
# # # #
|
9
|
+
# 80.times do |index|
|
10
|
+
# pool.pushb do
|
11
|
+
# raise Exception if index == 3
|
12
|
+
# puts index
|
13
|
+
# sleep(1)
|
14
|
+
# end
|
14
15
|
# end
|
15
|
-
# end
|
16
|
-
# #
|
17
16
|
#
|
18
17
|
#
|
19
|
-
#
|
20
|
-
# p th
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# pool.join_all
|
24
|
-
|
25
|
-
#<Thread:0x007f8da10bc328 run>
|
26
|
-
#<Thread:0x007f8da2855cf0@/Users/nick/rubyproject/thread_pool/core/rb_daemon.rb:23 sleep>
|
27
|
-
#<Thread:0x007f8da2855bd8@/Users/nick/rubyproject/thread_pool/core/rb_thread.rb:23 sleep>
|
28
|
-
#<Thread:0x007f8da2855a98@/Users/nick/rubyproject/thread_pool/core/rb_thread.rb:23 sleep>
|
29
|
-
#<Thread:0x007f8da2855958@/Users/nick/rubyproject/thread_pool/core/rb_thread.rb:23 sleep>
|
30
|
-
#<Thread:0x007f8da2855818@/Users/nick/rubyproject/thread_pool/core/rb_thread.rb:23 sleep>
|
31
|
-
#<Thread:0x007f8da28556d8@/Users/nick/rubyproject/thread_pool/core/rb_thread.rb:23 sleep>
|
32
|
-
#<Thread:0x007f8da2854490@/Users/nick/rubyproject/thread_pool/core/rb_elastic.rb:17 sleep>
|
33
|
-
#<Thread:0x007f8da2854300@/Users/nick/rubyproject/thread_pool/core/rb_elastic.rb:17 sleep>
|
34
|
-
#<Thread:0x007f8da2854198@/Users/nick/rubyproject/thread_pool/core/rb_elastic.rb:17 sleep>
|
35
|
-
#<Thread:0x007f8da2854008@/Users/nick/rubyproject/thread_pool/core/rb_elastic.rb:17 sleep>
|
36
|
-
#<Thread:0x007f8da3067e20@/Users/nick/rubyproject/thread_pool/core/rb_elastic.rb:17 sleep>
|
18
|
+
# sleep
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb_thread_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1full
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick An
|
@@ -16,6 +16,10 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- lib/core/base.rb
|
20
|
+
- lib/core/rb_daemon.rb
|
21
|
+
- lib/core/rb_elastic.rb
|
22
|
+
- lib/core/rb_thread.rb
|
19
23
|
- lib/rb_thread_pool.rb
|
20
24
|
homepage: https://github.com/nickoan/rb-thread-pool
|
21
25
|
licenses:
|
@@ -32,9 +36,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
32
36
|
version: '0'
|
33
37
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
38
|
requirements:
|
35
|
-
- - "
|
39
|
+
- - ">"
|
36
40
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
41
|
+
version: 1.3.1
|
38
42
|
requirements: []
|
39
43
|
rubyforge_project:
|
40
44
|
rubygems_version: 2.6.12
|