griffin 0.1.3 → 0.1.4

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: 7f4d42dab0788aed394458a38fbcf88f9d5ebbe008ae97075c8df717d7022d4e
4
- data.tar.gz: 1f5c7a121e48f18e2e3a16a9f2769b4f38ae7f4a4ce7649c3257263b306ba968
3
+ metadata.gz: 307fd7dcba526d6bb562bf97d4d7c31c0f82d1fdfb488de21a5fb1040cc6506e
4
+ data.tar.gz: 01a0b9ddfcd3486a9c1b378c11b22dc35856fd264f283c68e19f74973fe6f588
5
5
  SHA512:
6
- metadata.gz: 34ab89ab90c27fb09ea40071c60c1bcd019fee2089034444bacf7531f38ae8ec61277fa9adf8440cd0a53eb11a9685ee026a2a44656c3b70d8602820e876df87
7
- data.tar.gz: 19958ae78aa855bd8994636d3e4fdbd478af80868af31d14141dde8b71017095ef49584db8f8259bff2f32452b23355d65bf6e0a1f226e21f6d95ae0e1c3ba73
6
+ metadata.gz: 8780b448b45fb336c4bd7196e23e3901a64822fd0ed9661d8b2e59fb4d437feb2527cc1388b7162a2f927f37f229e36d6c5476bf2c5af17c407a38b298ab38b9
7
+ data.tar.gz: 6026c11481574d680a0b79295109da1405656aecd63424c3f228bbc765144d1019da666d3684d2efd8e1319c578df043607efff8d44e8a933f6a3b910a33f62f
@@ -1,24 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Style/GlobalVars
4
-
5
3
  $LOAD_PATH.unshift File.expand_path('./examples/routeguide')
6
4
 
7
5
  require 'griffin'
8
- require 'grpc_kit'
9
6
  require 'pry'
10
7
  require 'json'
11
- require 'logger'
12
8
  require 'routeguide_services_pb'
13
9
 
14
10
  RESOURCE_PATH = './examples/routeguide/routeguide.json'
15
11
  HOST = 'localhost'
16
12
  PORT = 50051
17
13
 
18
- $logger = Logger.new(STDOUT)
19
-
20
14
  def get_feature(stub)
21
- $logger.info('===== get_feature =====')
15
+ GRPC.logger.info('===== get_feature =====')
22
16
  points = [
23
17
  Routeguide::Point.new(latitude: 409_146_138, longitude: -746_188_906),
24
18
  Routeguide::Point.new(latitude: 0, longitude: 0)
@@ -27,15 +21,15 @@ def get_feature(stub)
27
21
  points.each do |pt|
28
22
  feature = stub.get_feature(pt)
29
23
  if feature.name == ''
30
- $logger.info("Found nothing at #{feature.inspect}")
24
+ GRPC.logger.info("Found nothing at #{feature.inspect}")
31
25
  else
32
- $logger.info("Found '#{feature.name}' at #{feature.location.inspect}")
26
+ GRPC.logger.info("Found '#{feature.name}' at #{feature.location.inspect}")
33
27
  end
34
28
  end
35
29
  end
36
30
 
37
31
  def list_features(stub)
38
- $logger.info('===== list_features =====')
32
+ GRPC.logger.info('===== list_features =====')
39
33
  rect = Routeguide::Rectangle.new(
40
34
  lo: Routeguide::Point.new(latitude: 400_000_000, longitude: -750_000_000),
41
35
  hi: Routeguide::Point.new(latitude: 420_000_000, longitude: -730_000_000),
@@ -45,12 +39,12 @@ def list_features(stub)
45
39
 
46
40
  loop do
47
41
  r = stream.recv
48
- $logger.info("Found #{r.name} at #{r.location.inspect}")
42
+ GRPC.logger.info("Found #{r.name} at #{r.location.inspect}")
49
43
  end
50
44
  end
51
45
 
52
46
  def record_route(stub, size)
53
- $logger.info('===== record_route =====')
47
+ GRPC.logger.info('===== record_route =====')
54
48
 
55
49
  features = File.open(RESOURCE_PATH) do |f|
56
50
  JSON.parse(f.read)
@@ -80,14 +74,14 @@ ROUTE_CHAT_NOTES = [
80
74
  ].freeze
81
75
 
82
76
  def route_chat(stub)
83
- $logger.info('===== route_chat =====')
77
+ GRPC.logger.info('===== route_chat =====')
84
78
 
85
79
  call = stub.route_chat({})
86
80
 
87
81
  t = Thread.new do
88
82
  loop do
89
83
  rn = call.recv
90
- $logger.info("Got message #{rn.message} at point point(#{rn.location.latitude}, #{rn.location.longitude})")
84
+ GRPC.logger.info("Got message #{rn.message} at point point(#{rn.location.latitude}, #{rn.location.longitude})")
91
85
  end
92
86
  end
93
87
 
@@ -116,5 +110,3 @@ get_feature(stub)
116
110
  list_features(stub)
117
111
  record_route(stub, 10)
118
112
  route_chat(stub)
119
-
120
- # rubocop:enable Style/GlobalVars
@@ -6,13 +6,11 @@ require 'griffin'
6
6
  require 'pry'
7
7
  require 'json'
8
8
  require 'routeguide_services_pb'
9
- require 'logger'
10
9
 
11
10
  class Server < Routeguide::RouteGuide::Service
12
11
  RESOURCE_PATH = './examples/routeguide/routeguide.json'
13
12
 
14
13
  def initialize
15
- @logger = Logger.new(STDOUT)
16
14
  File.open(RESOURCE_PATH) do |f|
17
15
  features = JSON.parse(f.read)
18
16
  @features = Hash[features.map { |x| [x['location'], x['name']] }]
@@ -23,12 +21,12 @@ class Server < Routeguide::RouteGuide::Service
23
21
 
24
22
  def get_feature(point, ctx)
25
23
  name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '')
26
- @logger.info("Point longitude=#{point.longitude}, latitude=#{point.latitude}, metadata=#{ctx.metadata}")
24
+ GRPC.logger.info("Point longitude=#{point.longitude}, latitude=#{point.latitude}, metadata=#{ctx.metadata}")
27
25
  Routeguide::Feature.new(location: point, name: name)
28
26
  end
29
27
 
30
28
  def list_features(rect, stream)
31
- @logger.info('===== list_features =====')
29
+ GRPC.logger.info('===== list_features =====')
32
30
 
33
31
  @features.each do |location, name|
34
32
  if name.nil? || name == '' || !in_range(location, rect)
@@ -37,13 +35,13 @@ class Server < Routeguide::RouteGuide::Service
37
35
 
38
36
  pt = Routeguide::Point.new(location)
39
37
  resp = Routeguide::Feature.new(location: pt, name: name)
40
- @logger.info(resp)
38
+ GRPC.logger.info(resp)
41
39
  stream.send_msg(resp)
42
40
  end
43
41
  end
44
42
 
45
43
  def record_route(stream)
46
- @logger.info('===== record_route =====')
44
+ GRPC.logger.info('===== record_route =====')
47
45
  distance = 0
48
46
  count = 0
49
47
  features = 0
@@ -52,7 +50,7 @@ class Server < Routeguide::RouteGuide::Service
52
50
 
53
51
  loop do
54
52
  point = stream.recv # XXX: raise StopIteration
55
- @logger.info(point)
53
+ GRPC.logger.info(point)
56
54
 
57
55
  count += 1
58
56
  name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '')
@@ -75,7 +73,7 @@ class Server < Routeguide::RouteGuide::Service
75
73
  def route_chat(call)
76
74
  loop do
77
75
  rn = call.recv
78
- @logger.info("route_note location=#{rn.location.inspect}, message=#{rn.message}")
76
+ GRPC.logger.info("route_note location=#{rn.location.inspect}, message=#{rn.message}")
79
77
  key = "#{rn.location.latitude} #{rn.location.longitude}"
80
78
  saved_msgs = @route_notes[key]
81
79
  @route_notes[key] << rn.message
@@ -116,17 +114,6 @@ class Server < Routeguide::RouteGuide::Service
116
114
  end
117
115
  end
118
116
 
119
- # require 'griffin/interceptors/server/payload_interceptor'
120
- require 'griffin/interceptors/server/filtered_payload_interceptor'
121
- require 'griffin/interceptors/server/logging_interceptor'
122
- require 'griffin/interceptors/server/x_request_id_interceptor'
123
-
124
- interceptors = [
125
- Griffin::Interceptors::Server::FilteredPayloadInterceptor.new,
126
- Griffin::Interceptors::Server::LoggingInterceptor.new,
127
- Griffin::Interceptors::Server::XRequestIdInterceptor.new,
128
- ]
129
-
130
117
  Griffin::Server.configure do |c|
131
118
  c.bind '127.0.0.1'
132
119
 
@@ -134,8 +121,6 @@ Griffin::Server.configure do |c|
134
121
 
135
122
  c.services Server.new
136
123
 
137
- c.interceptors interceptors
138
-
139
124
  c.workers 2
140
125
  end
141
126
 
data/griffin.gemspec CHANGED
@@ -23,11 +23,12 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ['lib', 'pb']
24
24
 
25
25
  spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'pry-byebug'
26
27
  spec.add_development_dependency 'rake'
27
28
  spec.add_development_dependency 'rspec'
28
29
  spec.add_development_dependency 'rubocop'
29
30
 
30
31
  spec.add_dependency 'connection_pool', '~> 2.2.2'
31
- spec.add_dependency 'grpc_kit', '~> 0.1.10'
32
+ spec.add_dependency 'grpc_kit', '~> 0.1.12'
32
33
  spec.add_dependency 'serverengine', '~> 2.0.7'
33
34
  end
@@ -9,7 +9,7 @@ require 'griffin/engine/worker'
9
9
  module Griffin
10
10
  module Engine
11
11
  def self.start(config, cluster: false)
12
- Griffin.logger = Griffin::Logger.create(config)
12
+ Griffin::Logger.setup(config)
13
13
 
14
14
  if cluster
15
15
  Griffin.logger.info("Griffin v#{Griffin::VERSION} starts as cluster mode")
@@ -3,8 +3,22 @@
3
3
  module Griffin
4
4
  class Logger
5
5
  class << self
6
+ def setup(config)
7
+ config[:logger] = Griffin::Logger.create(config)
8
+ Griffin.logger = config[:logger]
9
+
10
+ m = Module.new do
11
+ def logger
12
+ Griffin.logger
13
+ end
14
+ end
15
+ # Not to set a logger to `Grpckit.logger` since `Grpckit.logger` prints many HTTP2 layer logs
16
+ # If you want to print them, add environment variable `GRPC_KIT_LOGLEVEL=debug`
17
+ GrpcKit::GRPC.extend(m)
18
+ end
19
+
6
20
  def create(config)
7
- ServerEngine::DaemonLogger.new(logdev_from_config(config), config)
21
+ config[:logger] || ServerEngine::DaemonLogger.new(logdev_from_config(config), config)
8
22
  end
9
23
 
10
24
  def logdev_from_config(config)
@@ -14,9 +14,16 @@ module Griffin
14
14
  FORCIBLE_SHUTDOWN = '1'
15
15
 
16
16
  class << self
17
- def run
17
+ # @param bind [String]
18
+ # @param port [Integer,String]
19
+ def run(bind: nil, port: nil)
18
20
  c = config_builder.build
19
- Griffin::Engine.start(c, cluster: Integer(c[:workers]) > 1)
21
+ if c[:services].empty?
22
+ raise 'Required at least one service to handle reqeust'
23
+ end
24
+
25
+ opts = { bind: bind, port: port }.compact
26
+ Griffin::Engine.start(c.merge(opts), cluster: Integer(c[:workers]) > 1)
20
27
  end
21
28
 
22
29
  def configure
@@ -94,10 +101,8 @@ module Griffin
94
101
  end
95
102
  end
96
103
 
104
+ # Need Timeout?
97
105
  @thread_pool.shutdown
98
- # unless @sever.session_count == 0
99
- # end
100
-
101
106
  @command.close
102
107
  @signal.close
103
108
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Griffin
4
4
  class ServerConfigBuilder
5
- SERVERENGINE_PRIMITIVE_CONFIGS = %i[workers bind port log pid_path log_level].freeze
5
+ SERVERENGINE_PRIMITIVE_CONFIGS = %i[workers bind port log pid_path log_level logger].freeze
6
6
  SERVERENGINE_BLOCK_CONFIGS = %i[before_fork after_fork].freeze
7
7
  # Users can't change these values
8
8
  SERVERENGIEN_FIXED_CONFIGS = %i[daemonize worker_type worker_process_name].freeze
@@ -33,13 +33,14 @@ module Griffin
33
33
  port: 50051,
34
34
  pool_size: DEFAULT_POOL_SIZE,
35
35
  interceptors: [],
36
+ services: [],
36
37
  }.freeze
37
38
 
38
39
  def initialize
39
40
  @opts = DEFAULT_SERVER_CONFIG.dup
40
41
  end
41
42
 
42
- (SERVERENGINE_PRIMITIVE_CONFIGS + GRIFFIN_CONFIGS + [:interceptors]).each do |name|
43
+ (SERVERENGINE_PRIMITIVE_CONFIGS + GRIFFIN_CONFIGS).each do |name|
43
44
  define_method(name) do |value|
44
45
  @opts[name] = value
45
46
  end
@@ -51,8 +52,12 @@ module Griffin
51
52
  end
52
53
  end
53
54
 
54
- def services(serv, *rest)
55
- @opts[:services] = Array(serv) + rest
55
+ def interceptors(*value)
56
+ @opts[:interceptors].concat(value).flatten!
57
+ end
58
+
59
+ def services(*value)
60
+ @opts[:services].concat(value).flatten!
56
61
  end
57
62
 
58
63
  def build
@@ -45,7 +45,10 @@ module Griffin
45
45
  def shutdown
46
46
  @shutdown = true
47
47
  @pool_size.times { @tasks.push(nil) }
48
- sleep 1 until @workers.empty?
48
+ until @workers.empty?
49
+ Griffin.logger.debug("#{@pool_size - @spawned} worker thread(s) shutdowned, waiting #{@spawned}")
50
+ sleep 1
51
+ end
49
52
  end
50
53
 
51
54
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Griffin
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ganmacs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2018-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +100,14 @@ dependencies:
86
100
  requirements:
87
101
  - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: 0.1.10
103
+ version: 0.1.12
90
104
  type: :runtime
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: 0.1.10
110
+ version: 0.1.12
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: serverengine
99
113
  requirement: !ruby/object:Gem::Requirement