griffin 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bcbf570b1ac6f1353cba627ce1b0aa0cd17372d13cb0e8c3f9bf39ade24823ff
4
- data.tar.gz: c7be8298c69e26601a8cc22bab7d31d1a5a7c08581cb0d22c000e0b393eec292
3
+ metadata.gz: 61289d32bbb5830ad446ea08b51604a4f4cf0340f59ec596dcc92c32cf29f7ac
4
+ data.tar.gz: dc61c80a41aa8c79294d88c9b2042b3e7babb728c108be7fe7a581e6c6324559
5
5
  SHA512:
6
- metadata.gz: 6e100c6375f4facf7bb50748f4d05aaa421fecd367b189378d4cf87adca6efb8cb1f91e03cae5e3dacb6703a285c0076f44a6e161cf23b1fca501ba084294814
7
- data.tar.gz: 3730cf11b197b612d62d54b53d4180ae6c8532b29a6a575bf02592341ab833fefb60484c236fe788655ca7b2b19bfc0d3cfca9f78a2cfd7ee25a035fc5f8f2dd
6
+ metadata.gz: c0629cb8f5fe58daa29b9c44d0e27cf37f29885e26bbc8fefb4a9aac624be4efed3436e98053fd554bcff1576fa0c9c4e26c313baf1e059ededae9ab0237b601
7
+ data.tar.gz: ea6576f3ab391cc2f632872375926c26e1f374006ee1d7b9f38be37bbe8af053ebdc208f933db309e95a262e1fa14272efcbf17b45941d07be0998fb16cc6f18
data/griffin.gemspec CHANGED
@@ -28,6 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rspec'
29
29
  spec.add_development_dependency 'rubocop'
30
30
 
31
- spec.add_dependency 'grpc_kit', '>= 0.2.1'
31
+ spec.add_dependency 'grpc_kit', '>= 0.3.0'
32
32
  spec.add_dependency 'serverengine', '~> 2.0.7'
33
33
  end
@@ -10,7 +10,11 @@ module Griffin
10
10
  def initialize
11
11
  @core = Griffin::Server.new(
12
12
  pool_size: config[:pool_size],
13
- interceptors: config[:interceptors]
13
+ min_pool_size: config[:min_pool_size],
14
+ max_pool_size: config[:max_pool_size],
15
+ min_connection_size: config[:min_connection_size],
16
+ max_connection_size: config[:max_connection_size],
17
+ interceptors: config[:interceptors],
14
18
  )
15
19
  end
16
20
 
@@ -8,7 +8,10 @@ module Griffin
8
8
  class Single
9
9
  def self.create(config)
10
10
  serv = Griffin::Server.new(
11
- pool_size: config[:pool_size],
11
+ min_pool_size: config[:min_pool_size],
12
+ max_pool_size: config[:max_pool_size],
13
+ min_connection_size: config[:min_connection_size],
14
+ max_connection_size: config[:max_connection_size],
12
15
  interceptors: config[:interceptors],
13
16
  )
14
17
  new(serv, config)
@@ -35,11 +35,15 @@ module Griffin
35
35
  end
36
36
  end
37
37
 
38
- # @param pool_size [Integer] Worker thread size
38
+ # @param min_pool_size [Integer] Worker thread mininum size
39
+ # @param max_pool_size [Integer] Worker thread maximun size
40
+ # @param min_connection_size [Integer] Maximun connection of TCP
41
+ # @param max_connection_size [Integer] Minimum connection of TCP
39
42
  # @param interceptors [Array<GrpcKit::GRPC::ServerInterceptor>] list of interceptors
40
- def initialize(pool_size:, interceptors: [], **opts)
41
- @worker_size = pool_size
42
- @server = GrpcKit::Server.new(interceptors: interceptors)
43
+ def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], **opts)
44
+ @min_connection_size = min_connection_size
45
+ @max_connection_size = max_connection_size
46
+ @server = GrpcKit::Server.new(interceptors: interceptors, min_pool_size: min_pool_size, max_pool_size: max_pool_size)
43
47
  @opts = opts
44
48
  @status = :run
45
49
  @worker_id = 0
@@ -65,7 +69,7 @@ module Griffin
65
69
  def run(sock, blocking: true)
66
70
  @socks << sock
67
71
 
68
- @thread_pool = Griffin::ThreadPool.new(@worker_size) do |conn|
72
+ @thread_pool = Griffin::ThreadPool.new(min: @min_connection_size, max: @max_connection_size) do |conn|
69
73
  @server.run(conn)
70
74
  end
71
75
 
@@ -113,11 +117,13 @@ module Griffin
113
117
  Griffin.logger.info("Shutting down sever(id=#{@worker_id}) forcibly...")
114
118
 
115
119
  @status = :halt
116
- @server.graceful_shutdown
120
+ @server.force_shutdown
117
121
  true
118
122
  when GRACEFUL_SHUTDOWN
119
123
  Griffin.logger.info("Shutting down sever(id=#{@worker_id}) gracefully...")
124
+
120
125
  @status = :stop
126
+ @server.graceful_shutdown
121
127
  true
122
128
  end
123
129
  end
@@ -7,12 +7,15 @@ module Griffin
7
7
  # Users can't change these values
8
8
  SERVERENGIEN_FIXED_CONFIGS = %i[daemonize worker_type worker_process_name].freeze
9
9
 
10
- # The default size of thread pool
11
- DEFAULT_POOL_SIZE = 10
10
+ # The default size of thread pool TCP Connection
11
+ DEFAULT_POOL_SIZE = 30
12
+ DEFAULT_CONNECTION_SIZE = 3
12
13
 
13
14
  GRIFFIN_CONFIGS = [
14
- # The size of thread pool
15
- :pool_size
15
+ :max_pool_size,
16
+ :min_pool_size,
17
+ :max_connection_size,
18
+ :min_connection_size,
16
19
  ].freeze
17
20
 
18
21
  GRPC_CONFIGS = %i[services interceptors].freeze
@@ -31,7 +34,10 @@ module Griffin
31
34
  workers: 1,
32
35
  bind: '0.0.0.0',
33
36
  port: 50051,
34
- pool_size: DEFAULT_POOL_SIZE,
37
+ max_pool_size: DEFAULT_POOL_SIZE,
38
+ min_pool_size: DEFAULT_POOL_SIZE,
39
+ max_connection_size: DEFAULT_CONNECTION_SIZE,
40
+ min_connection_size: DEFAULT_CONNECTION_SIZE,
35
41
  interceptors: [],
36
42
  services: [],
37
43
  }.freeze
@@ -40,7 +46,7 @@ module Griffin
40
46
  @opts = DEFAULT_SERVER_CONFIG.dup
41
47
  end
42
48
 
43
- (SERVERENGINE_PRIMITIVE_CONFIGS + GRIFFIN_CONFIGS).each do |name|
49
+ (SERVERENGINE_PRIMITIVE_CONFIGS).each do |name|
44
50
  define_method(name) do |value|
45
51
  @opts[name] = value
46
52
  end
@@ -52,6 +58,15 @@ module Griffin
52
58
  end
53
59
  end
54
60
 
61
+ def pool_size(min, max)
62
+ @opts[:min_pool_size] = Integer(min)
63
+ @opts[:max_pool_size] = Integer(max)
64
+ end
65
+
66
+ def connection_size(min, max)
67
+ @opts[:min_connection_size] = Integer(min)
68
+ @opts[:max_connection_size] = Integer(max)
69
+ end
55
70
  def interceptors(*value)
56
71
  @opts[:interceptors].concat(value).flatten!
57
72
  end
@@ -1,25 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'griffin/counting_semaphore'
3
+ require 'grpc_kit/thread_pool/auto_trimmer'
4
4
 
5
5
  module Griffin
6
6
  class ThreadPool
7
- DEFAULT_POOL_SIZE = 20
8
- DEFAULT_QUEUE_SIZE = 512
7
+ DEFAULT_MAX = 5
8
+ DEFAULT_MIN = 1
9
+ QUEUE_SIZE = 128
9
10
 
10
- def initialize(pool_size = DEFAULT_POOL_SIZE, queue_size: DEFAULT_QUEUE_SIZE, &block)
11
- @pool_size = pool_size
12
- @queue_size = queue_size
11
+ def initialize(interval: 60, max: DEFAULT_MAX, min: DEFAULT_MIN, &block)
12
+ @max_pool_size = max
13
+ @min_pool_size = min
13
14
  @block = block
14
15
  @shutdown = false
15
- @semaphore = Griffin::CountingSemaphore.new(queue_size)
16
- @tasks = Queue.new
16
+ @tasks = SizedQueue.new(QUEUE_SIZE)
17
17
 
18
18
  @spawned = 0
19
19
  @workers = []
20
20
  @mutex = Mutex.new
21
+ @waiting = 0
21
22
 
22
- @pool_size.times { spawn_thread }
23
+ @min_pool_size.times { spawn_thread }
24
+ @auto_trimmer = GrpcKit::ThreadPool::AutoTrimmer.new(self, interval: interval + rand(10)).tap(&:start!)
23
25
  end
24
26
 
25
27
  def schedule(task, &block)
@@ -32,25 +34,31 @@ module Griffin
32
34
  end
33
35
 
34
36
  # TODO: blocking now..
35
- @semaphore.wait
36
37
  @tasks.push(block || task)
37
38
 
38
- @mutex.synchronize do
39
- if @spawned < @pool_size
40
- spawn_thread
41
- end
39
+ if @mutex.synchronize { (@waiting < @tasks.size) && (@spawned < @max_pool_size) }
40
+ spawn_thread
42
41
  end
43
42
  end
44
43
 
45
44
  def shutdown
46
45
  @shutdown = true
47
- @pool_size.times { @tasks.push(nil) }
46
+ @max_pool_size.times { @tasks.push(nil) }
47
+ @auto_trimmer.stop
48
48
  until @workers.empty?
49
- Griffin.logger.debug("#{@pool_size - @spawned} worker thread(s) shutdowned, waiting #{@spawned}")
49
+ Griffin.logger.debug("Shutdown waiting #{@waiting} workers")
50
50
  sleep 1
51
51
  end
52
52
  end
53
53
 
54
+ # For GrpcKit::ThreadPool::AutoTrimmer
55
+ def trim(force = false)
56
+ if @mutex.synchronize { (force || (@waiting > 0)) && (@spawned > @min_pool_size) }
57
+ GrpcKit.logger.info("Trim worker! Next worker size #{@spawned - 1}")
58
+ @tasks.push(nil)
59
+ end
60
+ end
61
+
54
62
  private
55
63
 
56
64
  def spawn_thread
@@ -64,7 +72,9 @@ module Griffin
64
72
  break
65
73
  end
66
74
 
75
+ @mutex.synchronize { @waiting += 1 }
67
76
  task = @tasks.pop
77
+ @mutex.synchronize { @waiting -= 1 }
68
78
  if task.nil?
69
79
  break
70
80
  end
@@ -73,8 +83,6 @@ module Griffin
73
83
  @block.call(task)
74
84
  rescue Exception => e # rubocop:disable Lint/RescueException
75
85
  Griffin.logger.error("An error occured on top level in worker #{Thread.current.name}: #{e.message} (#{e.class})\n #{Thread.current.backtrace.join("\n")} ")
76
- ensure
77
- @semaphore.signal
78
86
  end
79
87
  end
80
88
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Griffin
4
- VERSION = '0.1.9'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: griffin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Iwama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-17 00:00:00.000000000 Z
11
+ date: 2019-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 0.2.1
89
+ version: 0.3.0
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: 0.2.1
96
+ version: 0.3.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: serverengine
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +142,6 @@ files:
142
142
  - examples/routeguide_server.rb
143
143
  - griffin.gemspec
144
144
  - lib/griffin.rb
145
- - lib/griffin/counting_semaphore.rb
146
145
  - lib/griffin/engine.rb
147
146
  - lib/griffin/engine/server.rb
148
147
  - lib/griffin/engine/single.rb
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Griffin
4
- class CountingSemaphore
5
- def initialize(size)
6
- @size = size
7
- @queue = Queue.new
8
- @size.times { @queue.push(0) }
9
- end
10
-
11
- def wait
12
- @queue.pop
13
- end
14
-
15
- def signal
16
- @queue.push(0)
17
- end
18
- end
19
- end