-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathdemo_server
executable file
·96 lines (89 loc) · 3.44 KB
/
demo_server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env ruby
# frozen_string_literal: true
# Copyright (c) 2017-present, BigCommerce Pty. Ltd. All rights reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# use this file to spin up a test grpc server based on the files in support/grpc_server.rb
#
require 'rubygems'
require 'bundler'
Bundler.setup
require 'active_support/all'
require 'gruf'
$LOAD_PATH.unshift File.expand_path('pb', __dir__)
require File.realpath("#{File.dirname(__FILE__)}/support/grpc.rb")
require File.realpath("#{File.dirname(__FILE__)}/support/serializers/proto.rb")
class FakeStats
def increment(k)
Gruf.logger.debug "statsd.increment: #{k}"
end
def timing(k, t)
Gruf.logger.debug "statsd.timing: #{k} -> #{t}"
end
end
class DemoHook < Gruf::Hooks::Base
def before_server_start(server:)
raise StandardError, 'Failed in before_server_start hook' if ENV.fetch('FAIL_IN_BEFORE_START', 0).to_i.positive?
Gruf.logger.info 'Executing before server start hook...'
end
def after_server_stop(server:)
Gruf.logger.info 'Executing after server stop hook...'
end
end
Gruf.configure do |c|
c.server_binding_url = 'localhost:8001'
c.error_serializer = Serializers::Proto
c.backtrace_on_error = true
c.health_check_enabled = ::ENV.fetch('GRUF_HEALTH_CHECK_ENABLED', 1).to_i.positive?
c.interceptors.use(
Gruf::Interceptors::Instrumentation::Statsd,
client: FakeStats.new,
prefix: 'demo'
)
c.interceptors.use(
Gruf::Interceptors::Authentication::Basic,
credentials: [{
username: ENV.fetch('AUTH_USERNAME', 'grpc'),
password: ENV.fetch('AUTH_PASSWORD', 'magic')
}]
)
c.interceptors.use(
Gruf::Interceptors::Instrumentation::RequestLogging::Interceptor,
formatter: :plain,
log_parameters: true
)
c.interceptors.use(Gruf::Interceptors::ActiveRecord::ConnectionReset)
c.event_listener_proc = lambda do |event|
case event
when :thread_pool_exhausted
Gruf.logger.error('gRPC thread pool exhausted!')
else
# noop
end
end
c.rpc_server_options = {
pool_size: ENV.fetch('POOL_SIZE', 30).to_i, # 30 threads
pool_keep_alive: ENV.fetch('POOL_KEEP_ALIVE', 1).to_i, # 1s
poll_period: ENV.fetch('DEFAULT_POLL_PERIOD', 1).to_i, # 1s
}
c.hooks.use(DemoHook)
end
Gruf.logger = Logger.new($stdout)
Gruf.logger.level = Logger::Severity::DEBUG
Gruf.grpc_logger = Logger.new($stdout)
Gruf.grpc_logger.level = Logger::Severity::INFO
Gruf.controllers_path = "#{__dir__}/pb"
cli = Gruf::Cli::Executor.new
cli.run