ampedxx-yrpc 0.1.4

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.
@@ -0,0 +1,12 @@
1
+ module Yrpc
2
+ module Error
3
+ class ResponseError < StandardError
4
+ attr_reader :code
5
+
6
+ def initialize(code, message)
7
+ super(message)
8
+ @code = code
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module Yrpc
2
+
3
+ class Executor
4
+
5
+
6
+ def run
7
+ server = Yrpc::Server.new(Yrpc.server_options)
8
+ Yrpc.services.each { |service| server.add_service(service) }
9
+ server.grpc_server.run
10
+ end
11
+
12
+ def auto_load_all
13
+
14
+ end
15
+
16
+
17
+ end
18
+
19
+ module Ruler
20
+ def self.to_underscore(string)
21
+ string.gsub(/::/, '/').
22
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
23
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
24
+ tr("-", "_").
25
+ downcase
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,13 @@
1
+ module Yrpc
2
+ module AutoLoading
3
+ def Object.const_missing name
4
+ $LOAD_PATH << File.join("./","app","controllers") unless $LOAD_PATH.include?("./app/controllers")
5
+ $LOAD_PATH << File.join("./","app","models") unless $LOAD_PATH.include?("./app/models")
6
+ require Yrpc::Ruler.to_underscore(name.to_s)
7
+ klass = Object.const_get(name)
8
+ klass
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,26 @@
1
+ module Yrpc
2
+ module Interceptors
3
+ module ActiveRecord
4
+ class ResetConnection
5
+
6
+ def invoke
7
+ if enabled? && !::ActiveRecord::Base.connection.active?
8
+ ::ActiveRecord::Base.establish_connection
9
+ end
10
+ yield
11
+ ensure
12
+ ::ActiveRecord::Base.clear_active_connections! if enabled?
13
+ end
14
+
15
+ private
16
+
17
+ ##
18
+ # @return [Boolean] If AR is loaded, we can enable this hook safely
19
+ #
20
+ def enabled?
21
+ defined?(::ActiveRecord::Base)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ module Yrpc
2
+ module Interceptors
3
+ class Base
4
+
5
+ attr_reader :request
6
+
7
+ attr_reader :error
8
+
9
+ attr_reader :options
10
+
11
+ def initialize(request, error, options = {})
12
+ @request = request
13
+ @error = error
14
+ @options = options || {}
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,97 @@
1
+ # Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
5
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
6
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
7
+ #
8
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
9
+ # Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
13
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
14
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ #
16
+ module Yrpc
17
+ module Interceptors
18
+ ##
19
+ # Intercepts outbound client requests to provide a unified interface and request context
20
+ #
21
+ class ClientInterceptor < GRPC::ClientInterceptor
22
+
23
+
24
+ ##
25
+ # Handles interception of outbound calls. Implement this in your derivative interceptor implementation.
26
+ #
27
+ # @param [Yrpc::Outbound::RequestContext] request_context The context of the outbound request
28
+ # @return [Object] This method must return the response from the yielded block
29
+ #
30
+ def call(request_context:)
31
+ Yrpc.logger "Logging client interceptor for request: #{request_context.method}"
32
+ yield
33
+ end
34
+
35
+ ##
36
+ # Call the interceptor from the request_response call
37
+ #
38
+ # @param [Object] request The request being sent
39
+ # @param [GRPC::ActiveCall] call The GRPC ActiveCall object
40
+ # @param [Method] method The method being called
41
+ # @param [Hash] metadata A hash of outgoing metadata
42
+ # @return [Object] The response message
43
+ #
44
+ def request_response(request: nil, call: nil, method: nil, metadata: nil)
45
+ rc = Yrpc::Outbound::RequestContext.new(type: :request_response, requests: [request], call: call, method: method, metadata: metadata)
46
+ call(request_context: rc)
47
+ matadata= metadata.merge!(rc.metadata)
48
+ yield
49
+ end
50
+
51
+ ##
52
+ # Call the interceptor from the client_streamer call
53
+ #
54
+ # @param [Enumerable] requests An enumerable of requests being sent
55
+ # @param [GRPC::ActiveCall] call The GRPC ActiveCall object
56
+ # @param [Method] method The method being called
57
+ # @param [Hash] metadata A hash of outgoing metadata
58
+ # @return [Object] The response message
59
+ #
60
+ def client_streamer(requests: nil, call: nil, method: nil, metadata: nil)
61
+ rc = Yrpc::Outbound::RequestContext.new(type: :client_streamer, requests: requests, call: call, method: method, metadata: metadata)
62
+ call(request_context: rc)
63
+ matadata= metadata.merge!(rc.metadata)
64
+ yield
65
+ end
66
+
67
+ ##
68
+ # Call the interceptor from the server_streamer call
69
+ #
70
+ # @param [Object] request The request being sent
71
+ # @param [GRPC::ActiveCall] call The GRPC ActiveCall object
72
+ # @param [Method] method The method being called
73
+ # @param [Hash] metadata A hash of outgoing metadata
74
+ # @return [Object] The response message
75
+ #
76
+ def server_streamer(request: nil, call: nil, method: nil, metadata: nil)
77
+ rc = Yrpc::Outbound::RequestContext.new(type: :server_streamer, requests: [request], call: call, method: method, metadata: metadata)
78
+ call(request_context: rc)
79
+ yield
80
+ end
81
+
82
+ ##
83
+ # Call the interceptor from the bidi_streamer call
84
+ #
85
+ # @param [Enumerable] requests An enumerable of requests being sent
86
+ # @param [GRPC::ActiveCall] call The GRPC ActiveCall object
87
+ # @param [Method] method The method being called
88
+ # @param [Hash] metadata A hash of outgoing metadata
89
+ #
90
+ def bidi_streamer(requests: nil, call: nil, method: nil, metadata: nil)
91
+ rc = Yrpc::Outbound::RequestContext.new(type: :bidi_streamer, requests: requests, call: call, method: method, metadata: metadata)
92
+ call(request_context: rc)
93
+ yield
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,28 @@
1
+ module Yrpc
2
+ module Interceptors
3
+ class Registry
4
+
5
+ def initialize
6
+ @interceptors ||= []
7
+ end
8
+
9
+ def use(interceptor_class, options = {})
10
+ interceptors_mutex do
11
+ @interceptors << {
12
+ klass: interceptor_class,
13
+ options: options
14
+ }
15
+ end
16
+ end
17
+
18
+ #ruby 互斥锁,防止线程对资源进行竞争,导致资源不同步
19
+ def interceptors_mutex(&block)
20
+ @interceptors_mutex ||= begin
21
+ require 'monitor'
22
+ Monitor.new
23
+ end
24
+ @interceptors_mutex.synchronize(&block)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,69 @@
1
+ # Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
+ # documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
5
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
6
+ # persons to whom the Software is furnished to do so, subject to the following conditions:
7
+ #
8
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
9
+ # Software.
10
+ #
11
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12
+ # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
13
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
14
+ # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ #
16
+ module Yrpc
17
+ module Outbound
18
+ ##
19
+ # Encapsulates the context of an outbound client request
20
+ #
21
+ class RequestContext
22
+ # @var [Symbol]
23
+ attr_reader :type
24
+ # @var [Enumerable] requests
25
+ attr_reader :requests
26
+ # @var [GRPC::ActiveCall]
27
+ attr_reader :call
28
+ # @var [Method] method
29
+ attr_reader :method
30
+ # @var [Hash] metadata
31
+ attr_accessor :metadata
32
+
33
+ ##
34
+ # Initialize the new request context
35
+ #
36
+ # @param [Symbol] type The type of request
37
+ # @param [Enumerable] requests An enumerable of requests being sent
38
+ # @param [GRPC::ActiveCall] call The GRPC ActiveCall object
39
+ # @param [Method] method The method being called
40
+ # @param [Hash] metadata A hash of outgoing metadata
41
+ #
42
+ def initialize(type:, requests:, call:, method:, metadata:)
43
+ @type = type
44
+ @requests = requests
45
+ @call = call
46
+ @method = method
47
+ @metadata = metadata
48
+ end
49
+
50
+ ##
51
+ # Return the name of the method being called, e.g. GetThing
52
+ #
53
+ # @return [String]
54
+ #
55
+ def method_name
56
+ @method.to_s.split('/').last
57
+ end
58
+
59
+ ##
60
+ # Return the proper routing key for the request
61
+ #
62
+ # @return [String]
63
+ #
64
+ def route_key
65
+ @method[1..-1].underscore.tr('/', '.')
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,82 @@
1
+ module Yrpc
2
+ class Server
3
+
4
+ def initialize(options = {})
5
+ @options = options || {}
6
+ @interceptors = options.fetch(:interceptor_registry, Yrpc.interceptors)
7
+ @interceptors = Yrpc::Interceptors::Registry.new unless @interceptors.is_a?(Yrpc::Interceptors::Registry)
8
+ @services = []
9
+ @started = false
10
+ @stop_server = false
11
+ @stop_server_cv = ConditionVariable.new
12
+ @stop_server_mu = Monitor.new
13
+ @server_mu = Monitor.new
14
+ @hostname = options.fetch(:hostname, Yrpc.server_binding_url)
15
+ load_models
16
+ load_controllers
17
+ end
18
+
19
+ attr_reader :options
20
+
21
+ def controllers_path
22
+ options.fetch(:controllers_path, Yrpc.controllers_path)
23
+ end
24
+
25
+ def grpc_server
26
+ # 互斥锁 操作相同的资源
27
+ @server_mu.synchronize do
28
+ @server ||= begin
29
+ server = GRPC::RpcServer.new(@options)
30
+ @port = server.add_http2_port(@hostname, ssl_credentials)
31
+ @services.each do |service|
32
+ server.handle(service)
33
+ end
34
+ server
35
+ end
36
+ end
37
+ end
38
+
39
+ def add_service(service)
40
+ @services << service unless @services.include?(service)
41
+ end
42
+
43
+ def ssl_credentials
44
+ if options.fetch(:use_ssl, Yrpc.use_ssl)
45
+ private_key = File.read(options.fetch(:ssl_key_file, Yrpc.ssl_key_file))
46
+ cert_chain = File.read(options.fetch(:ssl_crt_file, Yrpc.ssl_crt_file))
47
+ certs = [nil, [{private_key: private_key, cert_chain: cert_chain}], false]
48
+ GRPC::Core::ServerCredentials.new(*certs)
49
+ else
50
+ :this_port_is_insecure
51
+ end
52
+ end
53
+
54
+ def load_controllers
55
+ return unless File.directory?(controllers_path)
56
+ path = File.realpath(controllers_path)
57
+ $LOAD_PATH.unshift(path)
58
+ p path
59
+ Dir["#{path}/**/*.rb"].each do |f|
60
+ # 把pb文件给exclue
61
+ next if f.include?('_pb')
62
+ Yrpc.logger.info "- Loading gRPC service file: #{f}"
63
+ load File.realpath(f)
64
+ end
65
+ end
66
+
67
+ def load_models
68
+ return unless File.directory?("app/models")
69
+ path = File.realpath("app/models")
70
+ $LOAD_PATH.unshift(path)
71
+ Dir["#{path}/**/*.rb"].each do |f|
72
+ # 把pb文件给exclue
73
+ next if f.include?('_pb')
74
+ Yrpc.logger.info "- Loading gRPC Model file: #{f}"
75
+ load File.realpath(f)
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+
82
+ end
@@ -0,0 +1,3 @@
1
+ module Yrpc
2
+ VERSION = "0.1.4"
3
+ end
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "yrpc/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ampedxx-yrpc"
7
+ spec.version = Yrpc::VERSION
8
+ spec.authors = ["icepoint0"]
9
+ spec.email = ["351711778@qq.com"]
10
+
11
+ spec.summary = "yprc"
12
+ spec.description = "yrpc"
13
+ spec.homepage = "http://ypr.com"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "bin"
25
+ spec.executables = ["yrpc-cli"]
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.11'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+
31
+ spec.add_runtime_dependency 'grpc', '~> 1.10'
32
+ spec.add_runtime_dependency 'grpc-tools', '~> 1.10'
33
+ spec.add_runtime_dependency 'activesupport', '~> 5.0'
34
+ spec.add_runtime_dependency 'slop', '~> 4.6'
35
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ampedxx-yrpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - icepoint0
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: grpc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: grpc-tools
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: slop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.6'
97
+ description: yrpc
98
+ email:
99
+ - 351711778@qq.com
100
+ executables:
101
+ - yrpc-cli
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".idea/.rakeTasks"
107
+ - ".idea/inspectionProfiles/Project_Default.xml"
108
+ - ".idea/misc.xml"
109
+ - ".idea/modules.xml"
110
+ - ".idea/vcs.xml"
111
+ - ".idea/workspace.xml"
112
+ - ".idea/yrpc.iml"
113
+ - ".rspec"
114
+ - ".travis.yml"
115
+ - CODE_OF_CONDUCT.md
116
+ - Gemfile
117
+ - Gemfile.lock
118
+ - LICENSE.txt
119
+ - README.md
120
+ - Rakefile
121
+ - bin/console
122
+ - bin/setup
123
+ - bin/yrpc-cli
124
+ - lib/yrpc.rb
125
+ - lib/yrpc/client.rb
126
+ - lib/yrpc/configuration.rb
127
+ - lib/yrpc/controllers/base.rb
128
+ - lib/yrpc/controllers/request.rb
129
+ - lib/yrpc/controllers/response.rb
130
+ - lib/yrpc/controllers/service_binder.rb
131
+ - lib/yrpc/error/response_error.rb
132
+ - lib/yrpc/executor/executor.rb
133
+ - lib/yrpc/helpers/autoload.rb
134
+ - lib/yrpc/interceptors/active_record.rb
135
+ - lib/yrpc/interceptors/base.rb
136
+ - lib/yrpc/interceptors/client_interceptor.rb
137
+ - lib/yrpc/interceptors/register.rb
138
+ - lib/yrpc/outbound/request_context.rb
139
+ - lib/yrpc/server.rb
140
+ - lib/yrpc/version.rb
141
+ - yrpc.gemspec
142
+ homepage: http://ypr.com
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.6.12
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: yprc
166
+ test_files: []