rb_thread_pool 0.0.1full → 0.0.2
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 +12 -9
- data/lib/core/rb_daemon.rb +14 -9
- data/lib/rb_thread_pool.rb +53 -11
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db3f5ce7c6233148e9f6426fdf026d9867357bd3
|
4
|
+
data.tar.gz: af2be7df55681f141ce60ba7d998235364be3981
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13ce6c1bf8788573c680db8ba7ee654f43a97230856ba3892e047d5e5c21303e92ab1335c4ce63264796ba08f82a06527a932cb8e4cbf2323a3ee89ce1414439
|
7
|
+
data.tar.gz: 539f933a4bdd0aa78a1f31556d5d52e9ae940e0b359ec6096435d8491697e3da330ce669d79738f39562481c47a5c175ef0eec219f69737a4cbd4ae737028c34
|
data/lib/core/base.rb
CHANGED
@@ -8,6 +8,8 @@ module RBThreadPool
|
|
8
8
|
attr_accessor :queue, :mutex, :th_pool, :th_elastic_pool,
|
9
9
|
:config, :thread_manager, :elastic
|
10
10
|
|
11
|
+
attr_reader :daemon
|
12
|
+
|
11
13
|
def initialize(config = {})
|
12
14
|
@queue = Queue.new
|
13
15
|
@mutex = Mutex.new
|
@@ -22,8 +24,8 @@ module RBThreadPool
|
|
22
24
|
# generate elastic thread when size over alert_line
|
23
25
|
@queue_limit = @config[:limit] || 100
|
24
26
|
@alert_line = (@queue_limit / 2).floor
|
25
|
-
# Daemon Thread
|
26
|
-
@daemon = RBThreadDaemon.new(
|
27
|
+
# Daemon Thread manager
|
28
|
+
@daemon = RBThreadDaemon.new(@mutex, @th_pool, @th_elastic_pool, @thread_manager, @config)
|
27
29
|
end
|
28
30
|
|
29
31
|
def start!
|
@@ -31,13 +33,7 @@ module RBThreadPool
|
|
31
33
|
generate_threads(@min) # generate common thread
|
32
34
|
end
|
33
35
|
|
34
|
-
#
|
35
|
-
# Thread.list.each do |th|
|
36
|
-
# p th
|
37
|
-
# end
|
38
|
-
# @th_pool.each(&:join)
|
39
|
-
# end
|
40
|
-
|
36
|
+
# add proc
|
41
37
|
def push(obj)
|
42
38
|
@mutex.synchronize{ @th_elastic_pool << @elastic_manager.fork } if
|
43
39
|
@queue.size > @alert_line and @th_elastic_pool.size < @elastic_amount
|
@@ -46,11 +42,18 @@ module RBThreadPool
|
|
46
42
|
end
|
47
43
|
alias << push
|
48
44
|
|
45
|
+
|
46
|
+
# add with block
|
49
47
|
def add(&blk)
|
50
48
|
push(blk)
|
51
49
|
end
|
52
50
|
alias pushb add
|
53
51
|
|
52
|
+
|
53
|
+
def daemon_th_inspect
|
54
|
+
@daemon.daemon_th.inspect
|
55
|
+
end
|
56
|
+
|
54
57
|
private
|
55
58
|
|
56
59
|
def generate_threads(num)
|
data/lib/core/rb_daemon.rb
CHANGED
@@ -5,31 +5,36 @@ module RBThreadPool
|
|
5
5
|
|
6
6
|
attr_accessor :condition
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
8
|
+
attr_reader :daemon_th
|
9
|
+
|
10
|
+
def initialize(mutex, common_pool, elastic, manager, config = {})
|
11
|
+
@mutex = mutex
|
12
|
+
@config = config
|
13
|
+
@interval = config[:interval] || 20
|
14
|
+
@pool = common_pool
|
15
|
+
@elastic = elastic
|
16
|
+
@manager = manager
|
14
17
|
@condition = true
|
18
|
+
@daemon_th = nil
|
15
19
|
end
|
16
20
|
|
17
21
|
def run!
|
18
|
-
|
22
|
+
@daemon_th = generate_daemon
|
19
23
|
end
|
20
24
|
|
21
25
|
private
|
22
26
|
|
23
27
|
def generate_daemon
|
28
|
+
|
24
29
|
return Thread.new do
|
25
30
|
Thread.current.name = 'RbThr:Daemon' if Thread.respond_to?(:name=)
|
31
|
+
Thread.abort_on_exception = true
|
26
32
|
interval = @interval
|
27
33
|
mutex = @mutex
|
28
34
|
pool = @pool
|
29
35
|
elastic = @elastic
|
30
36
|
while @condition
|
31
37
|
sleep(interval)
|
32
|
-
puts 'daemon wakeup'
|
33
38
|
mutex.synchronize { clean_common pool }
|
34
39
|
Thread.pass
|
35
40
|
mutex.synchronize { clean elastic } unless elastic.nil? || elastic.empty?
|
@@ -57,7 +62,7 @@ module RBThreadPool
|
|
57
62
|
end
|
58
63
|
|
59
64
|
def revive_common_thread(num)
|
60
|
-
manager = @
|
65
|
+
manager = @manager
|
61
66
|
pool = @pool
|
62
67
|
num.times do |idx|
|
63
68
|
pool << manager.fork
|
data/lib/rb_thread_pool.rb
CHANGED
@@ -1,18 +1,60 @@
|
|
1
1
|
$LOAD_PATH.unshift(__dir__)
|
2
2
|
require 'core/base'
|
3
3
|
|
4
|
+
# github: https://github.com/nickoan
|
4
5
|
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
6
|
+
|
7
|
+
#
|
8
|
+
# # generate thread pool
|
9
|
+
# pool = RBThreadPool::Base.new
|
10
|
+
# pool.start!
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# # add with block
|
14
|
+
# pool.add do
|
15
|
+
# sleep(2)
|
16
|
+
#
|
17
|
+
# puts "i'm the first adder"
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# # add with proc
|
22
|
+
# blk = proc {puts "i'm the second adder"}
|
23
|
+
# pool << blk
|
24
|
+
#
|
25
|
+
#
|
26
|
+
# # pushb is alias method for add
|
27
|
+
# 20.times do |idx|
|
28
|
+
# pool.pushb do
|
29
|
+
# sleep(0.5)
|
30
|
+
# puts "i'm in times at index #{idx}"
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
#
|
35
|
+
# p pool.daemon_th_inspect
|
36
|
+
#
|
37
|
+
# # add with block
|
38
|
+
# pool.add do
|
39
|
+
# sleep(2)
|
40
|
+
# puts "i'm the first adder"
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
#
|
44
|
+
# # add with proc
|
45
|
+
# blk = proc {puts "i'm the second adder"}
|
46
|
+
# pool << blk
|
47
|
+
#
|
48
|
+
#
|
49
|
+
# # pushb is alias method for add
|
50
|
+
# 20.times do |idx|
|
51
|
+
# pool.pushb do
|
52
|
+
# sleep(0.5)
|
53
|
+
# puts "i'm in times at index #{idx}"
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
16
57
|
#
|
58
|
+
# p pool.daemon_th_inspect
|
17
59
|
#
|
18
60
|
# 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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick An
|
@@ -36,9 +36,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
36
|
version: '0'
|
37
37
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - "
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
41
|
+
version: '0'
|
42
42
|
requirements: []
|
43
43
|
rubyforge_project:
|
44
44
|
rubygems_version: 2.6.12
|