griffin 0.1.2 → 0.1.3

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: c1b530676513eca565d67b7f033c68e7fec613b9232bd3cc1f10654c832bcfe0
4
- data.tar.gz: c14bf3300ef1fd84eb5ecafd7b02d6bf51e7e279f624f1e7a1f35d2dd072e416
3
+ metadata.gz: 7f4d42dab0788aed394458a38fbcf88f9d5ebbe008ae97075c8df717d7022d4e
4
+ data.tar.gz: 1f5c7a121e48f18e2e3a16a9f2769b4f38ae7f4a4ce7649c3257263b306ba968
5
5
  SHA512:
6
- metadata.gz: 7b1da06d15ca41a14040742dff811b7b37f570a2d0c2951104676618be4063513bcadc610bae082e8662a487a76d6fa41617eb0cf24d64839a2231837d4e10c2
7
- data.tar.gz: '039db6747145dbf7d9a635a0a2b9909c650af9d5fec03ec8aa0802ce7791390fcf26c89332da3cfa87531b22fd3c22b0712a72d25299972335771778fe0e9ac3'
6
+ metadata.gz: 34ab89ab90c27fb09ea40071c60c1bcd019fee2089034444bacf7531f38ae8ec61277fa9adf8440cd0a53eb11a9685ee026a2a44656c3b70d8602820e876df87
7
+ data.tar.gz: 19958ae78aa855bd8994636d3e4fdbd478af80868af31d14141dde8b71017095ef49584db8f8259bff2f32452b23355d65bf6e0a1f226e21f6d95ae0e1c3ba73
@@ -21,8 +21,9 @@ class LoggingInterceptor < GRPC::ServerInterceptor
21
21
  yield(LoggingStream.new(call))
22
22
  end
23
23
 
24
- def bidi_streamer(**)
25
- yield
24
+ def bidi_streamer(call: nil, method: nil, **)
25
+ GrpcKit.logger.info("Started request method=#{method.name}, service_name=#{method.receiver.class.service_name}")
26
+ yield(LoggingStream.new(call))
26
27
  end
27
28
 
28
29
  class LoggingStream
@@ -31,13 +32,13 @@ class LoggingInterceptor < GRPC::ServerInterceptor
31
32
  end
32
33
 
33
34
  def send_msg(msg, **opt)
34
- GrpcKit.logger.info("logging interceptor send #{msg}")
35
+ GrpcKit.logger.info("logging interceptor send #{msg.inspect}")
35
36
  @stream.send_msg(msg, opt)
36
37
  end
37
38
 
38
39
  def recv(**opt)
39
40
  @stream.recv(**opt).tap do |v|
40
- GrpcKit.logger.info("logging interceptor recv #{v}")
41
+ GrpcKit.logger.info("logging interceptor recv #{v.inspect}")
41
42
  end
42
43
  end
43
44
  end
@@ -4,6 +4,7 @@
4
4
 
5
5
  $LOAD_PATH.unshift File.expand_path('./examples/routeguide')
6
6
 
7
+ require 'griffin'
7
8
  require 'grpc_kit'
8
9
  require 'pry'
9
10
  require 'json'
@@ -11,6 +12,8 @@ require 'logger'
11
12
  require 'routeguide_services_pb'
12
13
 
13
14
  RESOURCE_PATH = './examples/routeguide/routeguide.json'
15
+ HOST = 'localhost'
16
+ PORT = 50051
14
17
 
15
18
  $logger = Logger.new(STDOUT)
16
19
 
@@ -22,7 +25,7 @@ def get_feature(stub)
22
25
  ]
23
26
 
24
27
  points.each do |pt|
25
- feature = stub.get_feature(pt, metadata: { 'metadata' => 'data1' })
28
+ feature = stub.get_feature(pt)
26
29
  if feature.name == ''
27
30
  $logger.info("Found nothing at #{feature.inspect}")
28
31
  else
@@ -47,6 +50,8 @@ def list_features(stub)
47
50
  end
48
51
 
49
52
  def record_route(stub, size)
53
+ $logger.info('===== record_route =====')
54
+
50
55
  features = File.open(RESOURCE_PATH) do |f|
51
56
  JSON.parse(f.read)
52
57
  end
@@ -65,6 +70,36 @@ def record_route(stub, size)
65
70
  puts "summary: #{resp[0].inspect}"
66
71
  end
67
72
 
73
+ ROUTE_CHAT_NOTES = [
74
+ Routeguide::RouteNote.new(message: 'First message', location: Routeguide::Point.new(latitude: 0, longitude: 1)),
75
+ Routeguide::RouteNote.new(message: 'Second message', location: Routeguide::Point.new(latitude: 0, longitude: 2)),
76
+ Routeguide::RouteNote.new(message: 'Third message', location: Routeguide::Point.new(latitude: 0, longitude: 3)),
77
+ Routeguide::RouteNote.new(message: 'Fourth message', location: Routeguide::Point.new(latitude: 0, longitude: 1)),
78
+ Routeguide::RouteNote.new(message: 'Fifth message', location: Routeguide::Point.new(latitude: 0, longitude: 2)),
79
+ Routeguide::RouteNote.new(message: 'Sixth message', location: Routeguide::Point.new(latitude: 0, longitude: 3)),
80
+ ].freeze
81
+
82
+ def route_chat(stub)
83
+ $logger.info('===== route_chat =====')
84
+
85
+ call = stub.route_chat({})
86
+
87
+ t = Thread.new do
88
+ loop do
89
+ rn = call.recv
90
+ $logger.info("Got message #{rn.message} at point point(#{rn.location.latitude}, #{rn.location.longitude})")
91
+ end
92
+ end
93
+
94
+ ROUTE_CHAT_NOTES.each do |rcn|
95
+ call.send_msg(rcn)
96
+ sleep 1
97
+ end
98
+
99
+ call.close_and_send
100
+ t.join
101
+ end
102
+
68
103
  opts = {}
69
104
 
70
105
  if ENV['GRPC_INTERCEPTOR']
@@ -74,10 +109,12 @@ elsif ENV['GRPC_TIMEOUT']
74
109
  opts[:timeout] = Integer(ENV['GRPC_TIMEOUT'])
75
110
  end
76
111
 
77
- stub = Routeguide::RouteGuide::Stub.new('localhost', 50051, **opts)
112
+ sock = TCPSocket.new(HOST, PORT)
113
+ stub = Routeguide::RouteGuide::Stub.new(sock, **opts)
78
114
 
79
115
  get_feature(stub)
80
116
  list_features(stub)
81
117
  record_route(stub, 10)
118
+ route_chat(stub)
82
119
 
83
120
  # rubocop:enable Style/GlobalVars
@@ -2,7 +2,7 @@
2
2
 
3
3
  $LOAD_PATH.unshift File.expand_path('./examples/routeguide')
4
4
 
5
- require 'grpc_kit'
5
+ require 'griffin'
6
6
  require 'pry'
7
7
  require 'json'
8
8
  require 'routeguide_services_pb'
@@ -17,6 +17,8 @@ class Server < Routeguide::RouteGuide::Service
17
17
  features = JSON.parse(f.read)
18
18
  @features = Hash[features.map { |x| [x['location'], x['name']] }]
19
19
  end
20
+
21
+ @route_notes = Hash.new { |h, k| h[k] = [] }
20
22
  end
21
23
 
22
24
  def get_feature(point, ctx)
@@ -70,6 +72,21 @@ class Server < Routeguide::RouteGuide::Service
70
72
  )
71
73
  end
72
74
 
75
+ def route_chat(call)
76
+ loop do
77
+ rn = call.recv
78
+ @logger.info("route_note location=#{rn.location.inspect}, message=#{rn.message}")
79
+ key = "#{rn.location.latitude} #{rn.location.longitude}"
80
+ saved_msgs = @route_notes[key]
81
+ @route_notes[key] << rn.message
82
+
83
+ saved_msgs.each do |m|
84
+ n = Routeguide::RouteNote.new(location: rn.location, message: m)
85
+ call.send_msg(n)
86
+ end
87
+ end
88
+ end
89
+
73
90
  private
74
91
 
75
92
  COORD_FACTOR = 1e7
@@ -99,20 +116,27 @@ class Server < Routeguide::RouteGuide::Service
99
116
  end
100
117
  end
101
118
 
102
- sock = TCPServer.new(50051)
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'
103
123
 
104
- opts = {}
124
+ interceptors = [
125
+ Griffin::Interceptors::Server::FilteredPayloadInterceptor.new,
126
+ Griffin::Interceptors::Server::LoggingInterceptor.new,
127
+ Griffin::Interceptors::Server::XRequestIdInterceptor.new,
128
+ ]
105
129
 
106
- if ENV['GRPC_INTERCEPTOR']
107
- require_relative 'interceptors/server_logging_interceptor'
108
- opts[:interceptors] = [LoggingInterceptor.new]
109
- end
130
+ Griffin::Server.configure do |c|
131
+ c.bind '127.0.0.1'
132
+
133
+ c.port 50051
110
134
 
111
- server = GrpcKit::Server.new(**opts)
112
- server.handle(Server.new)
113
- server.run
135
+ c.services Server.new
114
136
 
115
- loop do
116
- conn = sock.accept
117
- server.session_start(conn)
137
+ c.interceptors interceptors
138
+
139
+ c.workers 2
118
140
  end
141
+
142
+ Griffin::Server.run
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  end
21
21
  spec.bindir = 'exe'
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
- spec.require_paths = ['lib']
23
+ spec.require_paths = ['lib', 'pb']
24
24
 
25
25
  spec.add_development_dependency 'bundler'
26
26
  spec.add_development_dependency 'rake'
@@ -28,6 +28,6 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rubocop'
29
29
 
30
30
  spec.add_dependency 'connection_pool', '~> 2.2.2'
31
- spec.add_dependency 'grpc_kit', '~> 0.1.5'
31
+ spec.add_dependency 'grpc_kit', '~> 0.1.10'
32
32
  spec.add_dependency 'serverengine', '~> 2.0.7'
33
33
  end
@@ -8,7 +8,10 @@ module Griffin
8
8
  attr_reader :core, :listener
9
9
 
10
10
  def initialize
11
- @core = Griffin::Server.new
11
+ @core = Griffin::Server.new(
12
+ pool_size: config[:pool_size],
13
+ interceptors: config[:interceptors]
14
+ )
12
15
  end
13
16
 
14
17
  def before_run
@@ -7,7 +7,11 @@ module Griffin
7
7
  module Engine
8
8
  class Single
9
9
  def self.create(config)
10
- new(Griffin::Server.new, config)
10
+ serv = Griffin::Server.new(
11
+ pool_size: config[:pool_size],
12
+ interceptors: config[:interceptors],
13
+ )
14
+ new(serv, config)
11
15
  end
12
16
 
13
17
  def initialize(server, config)
@@ -8,7 +8,6 @@ require 'griffin/thread_pool'
8
8
 
9
9
  module Griffin
10
10
  class Server
11
- DEFAULT_WORKER_SIZE = 10
12
11
  DEFAULT_BACKLOG_SIZE = 1024
13
12
 
14
13
  GRACEFUL_SHUTDOWN = '0'
@@ -29,18 +28,21 @@ module Griffin
29
28
  end
30
29
  end
31
30
 
32
- def initialize(worker_size: DEFAULT_WORKER_SIZE, **opts)
33
- @worker_size = worker_size
34
- @server = GrpcKit::Server.new
31
+ # @param pool_size [Integer] Worker thread size
32
+ # @param interceptors [Array<GrpcKit::GRPC::ServerInterceptor>] list of interceptors
33
+ def initialize(pool_size:, interceptors: [], **opts)
34
+ @worker_size = pool_size
35
+ @server = GrpcKit::Server.new(interceptors: interceptors)
35
36
  @opts = opts
36
37
  @status = :run
37
38
  @worker_id = 0
38
39
  end
39
40
 
40
41
  def handle(handler)
41
- @server.handle(handler)
42
- handler.class.rpc_descs.each do |path, _|
43
- Griffin.logger.debug("Handle #{path}")
42
+ klass = handler.is_a?(Class) ? handler : handler.class
43
+ @server.handle(klass)
44
+ klass.rpc_descs.each_key do |path|
45
+ Griffin.logger.info("Handle #{path}")
44
46
  end
45
47
  end
46
48
 
@@ -6,7 +6,15 @@ module Griffin
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
9
- GRIFFIN_CONFIGS = %i[thread_pool].freeze
9
+
10
+ # The default size of thread pool
11
+ DEFAULT_POOL_SIZE = 10
12
+
13
+ GRIFFIN_CONFIGS = [
14
+ # The size of thread pool
15
+ :pool_size
16
+ ].freeze
17
+
10
18
  GRPC_CONFIGS = %i[services interceptors].freeze
11
19
 
12
20
  ServerConfig = Struct.new(*(SERVERENGINE_PRIMITIVE_CONFIGS + SERVERENGINE_BLOCK_CONFIGS + SERVERENGIEN_FIXED_CONFIGS + GRIFFIN_CONFIGS + GRPC_CONFIGS)) do
@@ -23,6 +31,8 @@ module Griffin
23
31
  workers: 1,
24
32
  bind: '0.0.0.0',
25
33
  port: 50051,
34
+ pool_size: DEFAULT_POOL_SIZE,
35
+ interceptors: [],
26
36
  }.freeze
27
37
 
28
38
  def initialize
@@ -41,8 +51,8 @@ module Griffin
41
51
  end
42
52
  end
43
53
 
44
- def services(*serv)
45
- @opts[:services] = serv
54
+ def services(serv, *rest)
55
+ @opts[:services] = Array(serv) + rest
46
56
  end
47
57
 
48
58
  def build
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Griffin
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -0,0 +1,76 @@
1
+ # Copyright 2015 gRPC authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'grpc'
16
+ require 'grpc/health/v1/health_services_pb'
17
+ require 'thread'
18
+
19
+ module Grpc
20
+ # Health contains classes and modules that support providing a health check
21
+ # service.
22
+ module Health
23
+ # Checker is implementation of the schema-specified health checking service.
24
+ class Checker < V1::Health::Service
25
+ StatusCodes = GRPC::Core::StatusCodes
26
+ HealthCheckResponse = V1::HealthCheckResponse
27
+
28
+ # Initializes the statuses of participating services
29
+ def initialize
30
+ @statuses = {}
31
+ @status_mutex = Mutex.new # guards access to @statuses
32
+ end
33
+
34
+ # Implements the rpc IDL API method
35
+ def check(req, _call)
36
+ status = nil
37
+ @status_mutex.synchronize do
38
+ status = @statuses["#{req.service}"]
39
+ end
40
+ if status.nil?
41
+ fail GRPC::BadStatus.new_status_exception(StatusCodes::NOT_FOUND)
42
+ end
43
+ HealthCheckResponse.new(status: status)
44
+ end
45
+
46
+ # Adds the health status for a given service.
47
+ def add_status(service, status)
48
+ @status_mutex.synchronize { @statuses["#{service}"] = status }
49
+ end
50
+
51
+ # Adds given health status for all given services
52
+ def set_status_for_services(status, *services)
53
+ @status_mutex.synchronize do
54
+ services.each { |service| @statuses["#{service}"] = status }
55
+ end
56
+ end
57
+
58
+ # Adds health status for each service given within hash
59
+ def add_statuses(service_statuses = {})
60
+ @status_mutex.synchronize do
61
+ service_statuses.each_pair { |service, status| @statuses["#{service}"] = status }
62
+ end
63
+ end
64
+
65
+ # Clears the status for the given service.
66
+ def clear_status(service)
67
+ @status_mutex.synchronize { @statuses.delete("#{service}") }
68
+ end
69
+
70
+ # Clears alls the statuses.
71
+ def clear_all
72
+ @status_mutex.synchronize { @statuses = {} }
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,28 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # source: grpc/health/v1/health.proto
3
+
4
+ require 'google/protobuf'
5
+
6
+ Google::Protobuf::DescriptorPool.generated_pool.build do
7
+ add_message "grpc.health.v1.HealthCheckRequest" do
8
+ optional :service, :string, 1
9
+ end
10
+ add_message "grpc.health.v1.HealthCheckResponse" do
11
+ optional :status, :enum, 1, "grpc.health.v1.HealthCheckResponse.ServingStatus"
12
+ end
13
+ add_enum "grpc.health.v1.HealthCheckResponse.ServingStatus" do
14
+ value :UNKNOWN, 0
15
+ value :SERVING, 1
16
+ value :NOT_SERVING, 2
17
+ end
18
+ end
19
+
20
+ module Grpc
21
+ module Health
22
+ module V1
23
+ HealthCheckRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1.HealthCheckRequest").msgclass
24
+ HealthCheckResponse = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1.HealthCheckResponse").msgclass
25
+ HealthCheckResponse::ServingStatus = Google::Protobuf::DescriptorPool.generated_pool.lookup("grpc.health.v1.HealthCheckResponse.ServingStatus").enummodule
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,44 @@
1
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
2
+ # Source: grpc/health/v1/health.proto for package 'grpc.health.v1'
3
+ # Original file comments:
4
+ # Copyright 2015 The gRPC Authors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ # The canonical version of this proto can be found at
19
+ # https://github.com/grpc/grpc-proto/blob/master/grpc/health/v1/health.proto
20
+ #
21
+
22
+ require 'grpc'
23
+ require 'grpc/health/v1/health_pb'
24
+
25
+ module Grpc
26
+ module Health
27
+ module V1
28
+ module Health
29
+ class Service
30
+
31
+ include GRPC::GenericService
32
+
33
+ self.marshal_class_method = :encode
34
+ self.unmarshal_class_method = :decode
35
+ self.service_name = 'grpc.health.v1.Health'
36
+
37
+ rpc :Check, HealthCheckRequest, HealthCheckResponse
38
+ end
39
+
40
+ Stub = Service.rpc_stub_class
41
+ end
42
+ end
43
+ end
44
+ 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.2
4
+ version: 0.1.3
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-05 00:00:00.000000000 Z
11
+ date: 2018-11-27 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.1.5
89
+ version: 0.1.10
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.1.5
96
+ version: 0.1.10
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: serverengine
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -154,6 +154,9 @@ files:
154
154
  - lib/griffin/server_config_builder.rb
155
155
  - lib/griffin/thread_pool.rb
156
156
  - lib/griffin/version.rb
157
+ - pb/grpc/health/checker.rb
158
+ - pb/grpc/health/v1/health_pb.rb
159
+ - pb/grpc/health/v1/health_services_pb.rb
157
160
  homepage: https://github.com/ganmacs/griffin
158
161
  licenses:
159
162
  - MIT
@@ -162,6 +165,7 @@ post_install_message:
162
165
  rdoc_options: []
163
166
  require_paths:
164
167
  - lib
168
+ - pb
165
169
  required_ruby_version: !ruby/object:Gem::Requirement
166
170
  requirements:
167
171
  - - ">="