grpc_server 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +47 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/examples/server.rb +8 -0
- data/grpc_server.gemspec +28 -0
- data/lib/grpc_server.rb +24 -0
- data/lib/grpc_server/engine.rb +89 -0
- data/lib/grpc_server/version.rb +3 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8aae2f11b010914bcd6e84eea94e8eb737c315c5e149c8a2d895ebbafe6ccc28
|
4
|
+
data.tar.gz: 0dd0a8cc4921d8c669f6d69dd8ed86d8f2445fc739ff08a49dc2d1ae1e3011a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0d9d5e8232259bbfff5db29f3652f6492cd9202be0e6d1118267bb646b1e3abf657d68cbb5266dc55214cfe5b4ae09f4f7029aff972961f50f7b3a883d89d33
|
7
|
+
data.tar.gz: 5e892e677f953685d353bd34845ce9ae16eaeb18fd77626f0699d72f13fcb3c9709380672ed3a836111d9f94c45c991b6b0684168e1933f0dd27736e241b3a29
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at south37777@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grpc_server (0.0.1)
|
5
|
+
grpc
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.4.4)
|
12
|
+
google-protobuf (3.13.0)
|
13
|
+
googleapis-common-protos-types (1.0.5)
|
14
|
+
google-protobuf (~> 3.11)
|
15
|
+
grpc (1.31.1)
|
16
|
+
google-protobuf (~> 3.12)
|
17
|
+
googleapis-common-protos-types (~> 1.0)
|
18
|
+
method_source (1.0.0)
|
19
|
+
pry (0.13.1)
|
20
|
+
coderay (~> 1.1)
|
21
|
+
method_source (~> 1.0)
|
22
|
+
rake (12.3.3)
|
23
|
+
rspec (3.9.0)
|
24
|
+
rspec-core (~> 3.9.0)
|
25
|
+
rspec-expectations (~> 3.9.0)
|
26
|
+
rspec-mocks (~> 3.9.0)
|
27
|
+
rspec-core (3.9.2)
|
28
|
+
rspec-support (~> 3.9.3)
|
29
|
+
rspec-expectations (3.9.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.9.0)
|
32
|
+
rspec-mocks (3.9.1)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.9.0)
|
35
|
+
rspec-support (3.9.3)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
grpc_server!
|
42
|
+
pry
|
43
|
+
rake (~> 12.0)
|
44
|
+
rspec (~> 3.0)
|
45
|
+
|
46
|
+
BUNDLED WITH
|
47
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Nao Minami
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# GrpcServer
|
2
|
+
Simple utility class for using gRPC server.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'grpc_server'
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle install
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install grpc_server
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
By using `GrpcServer` class, you can run a gRPC server in the specified configuration.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "grpc_server"
|
25
|
+
|
26
|
+
server = GrpcServer.new(
|
27
|
+
port: 3000, # Listen port
|
28
|
+
threads: 30, # The size of the thread pool
|
29
|
+
interceptors: [...], # An array of GRPC::ServerInterceptor objects
|
30
|
+
signals: ["TERM"], # Signals for graceful shutdown
|
31
|
+
)
|
32
|
+
server.set_handler(XXX) # Set handler here
|
33
|
+
server.run # Start to run
|
34
|
+
```
|
35
|
+
|
36
|
+
## Example
|
37
|
+
By using [examples/server.rb](https://github.com/south37/grpc_server/blob/master/examples/server.rb), gRPC server start to listen on `0.0.0.0:50051`.
|
38
|
+
|
39
|
+
```console
|
40
|
+
$ bundle exec ruby examples/server.rb
|
41
|
+
I, [2020-08-20T20:31:16.470412 #26572] INFO -- : gRPC server starting...
|
42
|
+
I, [2020-08-20T20:31:16.470594 #26572] INFO -- : * Max threads: 30
|
43
|
+
I, [2020-08-20T20:31:16.470632 #26572] INFO -- : * Environment: unknown
|
44
|
+
I, [2020-08-20T20:31:16.470646 #26572] INFO -- : * Listening on tcp://0.0.0.0:50051
|
45
|
+
I, [2020-08-20T20:31:16.470654 #26572] INFO -- : Use Ctrl-C to stop
|
46
|
+
```
|
47
|
+
|
48
|
+
The server supports [GRPC Health Checking Protocol](https://github.com/grpc/grpc/blob/master/doc/health-checking.md), we can check it by using [grpc-health-probe](https://github.com/grpc-ecosystem/grpc-health-probe).
|
49
|
+
|
50
|
+
```console
|
51
|
+
$ grpc-health-probe -addr=:50051
|
52
|
+
status: SERVING
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/south37/grpc_server. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/south37/grpc_server/blob/master/CODE_OF_CONDUCT.md).
|
64
|
+
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
+
|
70
|
+
## Code of Conduct
|
71
|
+
|
72
|
+
Everyone interacting in the GrpcServer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/south37/grpc_server/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/examples/server.rb
ADDED
data/grpc_server.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/grpc_server/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "grpc_server"
|
5
|
+
spec.version = GrpcServer::VERSION
|
6
|
+
spec.authors = ["Nao Minami"]
|
7
|
+
spec.email = ["south37777@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Simple utility class for using gRPC server}
|
10
|
+
spec.description = %q{Simple utility class for using gRPC server}
|
11
|
+
spec.homepage = "https://github.com/south37/grpc_server"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/south37/grpc_server"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_runtime_dependency "grpc"
|
28
|
+
end
|
data/lib/grpc_server.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "grpc_server/engine"
|
2
|
+
require "grpc_server/version"
|
3
|
+
|
4
|
+
class GrpcServer
|
5
|
+
# @param [String] host
|
6
|
+
# @param [Integer] port
|
7
|
+
# @param [Integer] threads the size of the thread pool the server uses to
|
8
|
+
# run its threads. No more concurrent requests can be made than the
|
9
|
+
# size of the thread pool
|
10
|
+
# @param [String] env
|
11
|
+
# @param [Array<GRPC::ServerInterceptor>] interceptors An array of
|
12
|
+
# GRPC::ServerInterceptor objects that will be used for intercepting
|
13
|
+
# server handlers to provide extra functionality.
|
14
|
+
# @param [Array<String>] signals List of String representing signals that
|
15
|
+
# the user would like to send to the server for graceful shutdown
|
16
|
+
# @param [Logger] logger
|
17
|
+
def initialize(**kwargs)
|
18
|
+
@engine = Engine.new(**kwargs)
|
19
|
+
end
|
20
|
+
|
21
|
+
extend Forwardable
|
22
|
+
|
23
|
+
def_delegators :@engine, :set_handler, :run, :stop
|
24
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "grpc"
|
2
|
+
require "grpc/health/checker"
|
3
|
+
require "logger"
|
4
|
+
|
5
|
+
class GrpcServer
|
6
|
+
class Engine
|
7
|
+
DEFAULT_HOST = "0.0.0.0"
|
8
|
+
DEFAULT_PORT = 50051
|
9
|
+
DEFAULT_THREADS = 30
|
10
|
+
DEFAULT_ENV = "unknown"
|
11
|
+
DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS = [
|
12
|
+
"TERM",
|
13
|
+
"INT",
|
14
|
+
].freeze
|
15
|
+
|
16
|
+
# @param [String] host
|
17
|
+
# @param [Integer] port
|
18
|
+
# @param [Integer] threads the size of the thread pool the server uses to
|
19
|
+
# run its threads. No more concurrent requests can be made than the
|
20
|
+
# size of the thread pool
|
21
|
+
# @param [String] env
|
22
|
+
# @param [Array<GRPC::ServerInterceptor>] interceptors An array of
|
23
|
+
# GRPC::ServerInterceptor objects that will be used for intercepting
|
24
|
+
# server handlers to provide extra functionality.
|
25
|
+
# @param [Array<String>] signals List of String representing signals that
|
26
|
+
# the user would like to send to the server for graceful shutdown
|
27
|
+
# @param [Logger] logger
|
28
|
+
def initialize(host: DEFAULT_HOST,
|
29
|
+
port: DEFAULT_PORT,
|
30
|
+
threads: DEFAULT_THREADS,
|
31
|
+
env: DEFAULT_ENV,
|
32
|
+
interceptors: [],
|
33
|
+
signals: DEFAULT_GRACEFUL_SHUTDOWN_SIGNALS,
|
34
|
+
logger: nil)
|
35
|
+
@host = host
|
36
|
+
@port = port
|
37
|
+
@threads = threads
|
38
|
+
@env = env
|
39
|
+
@logger = logger || Logger.new($stdout)
|
40
|
+
@signals = signals
|
41
|
+
|
42
|
+
@server = GRPC::RpcServer.new(
|
43
|
+
pool_size: @threads,
|
44
|
+
interceptors: interceptors,
|
45
|
+
)
|
46
|
+
@server.add_http2_port("#{@host}:#{@port}", :this_port_is_insecure)
|
47
|
+
|
48
|
+
# Setup Health Cheker.
|
49
|
+
# cf. https://github.com/grpc/grpc/blob/master/doc/health-checking.md
|
50
|
+
@server.handle(health_checker)
|
51
|
+
register_service_to_health_checker!(service: "")
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param [GRPC::GenericService] service
|
55
|
+
def set_handler(service)
|
56
|
+
@server.handle(service)
|
57
|
+
register_service_to_health_checker!(service: service.service_name)
|
58
|
+
end
|
59
|
+
|
60
|
+
def run
|
61
|
+
@logger.info("gRPC server starting...")
|
62
|
+
@logger.info("* Max threads: #{@threads}")
|
63
|
+
@logger.info("* Environment: #{@env}")
|
64
|
+
@logger.info("* Listening on tcp://#{@host}:#{@port}")
|
65
|
+
@logger.info("Use Ctrl-C to stop")
|
66
|
+
@server.run_till_terminated_or_interrupted(@signals)
|
67
|
+
@logger.info("Exiting")
|
68
|
+
end
|
69
|
+
|
70
|
+
def stop
|
71
|
+
@server.stop
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
# @param [String] service
|
77
|
+
def register_service_to_health_checker!(service:)
|
78
|
+
health_checker.add_status(
|
79
|
+
service,
|
80
|
+
Grpc::Health::V1::HealthCheckResponse::ServingStatus::SERVING,
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
# @return [Grpc::Health::Checker]
|
85
|
+
def health_checker
|
86
|
+
@health_checker ||= Grpc::Health::Checker.new
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grpc_server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nao Minami
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grpc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Simple utility class for using gRPC server
|
28
|
+
email:
|
29
|
+
- south37777@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".travis.yml"
|
37
|
+
- CODE_OF_CONDUCT.md
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- examples/server.rb
|
46
|
+
- grpc_server.gemspec
|
47
|
+
- lib/grpc_server.rb
|
48
|
+
- lib/grpc_server/engine.rb
|
49
|
+
- lib/grpc_server/version.rb
|
50
|
+
homepage: https://github.com/south37/grpc_server
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/south37/grpc_server
|
55
|
+
source_code_uri: https://github.com/south37/grpc_server
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.3.0
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.0.3
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Simple utility class for using gRPC server
|
75
|
+
test_files: []
|