Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize rubocop settings and format all files #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
AllCops:
Exclude:
- '**/**/*_pb.rb' # auto-generated
- "vendor/**/*"
- 'pb/grpc/health/checker.rb' # copied
- 'vendor/**/*'
DisplayCopNames: true
TargetRubyVersion: 2.5
TargetRubyVersion: 3.0
NewCops: enable

Style/AndOr:
EnforcedStyle: conditionals
Expand Down Expand Up @@ -42,7 +44,7 @@ Style/WhileUntilModifier:
Enabled: false

Naming/PredicateName:
NamePrefixBlacklist:
ForbiddenPrefixes:
- "is_"
- "have_"
NamePrefix:
Expand Down Expand Up @@ -76,7 +78,7 @@ Metrics/AbcSize:
Metrics/CyclomaticComplexity:
Max: 10

Metrics/LineLength:
Layout/LineLength:
Max: 160

Metrics/MethodLength:
Expand All @@ -90,3 +92,6 @@ Metrics/ClassLength:

Metrics/BlockLength:
Max: 40

Lint/DuplicateBranch:
IgnoreConstantBranches: true
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ source 'https://rubygems.org'

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'bundler'
gem 'pry-byebug'
gem 'rake'
gem 'rspec'
gem 'rubocop'

# Specify your gem's dependencies in griffin.gemspec
gemspec
6 changes: 3 additions & 3 deletions examples/interceptors/call_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class CallStream < GrpcKit::Call
delegate %i[send_msg recv] => :@inner

# @params call [GrpcKit::Call]
def initialize(inner)
def initialize(inner) # rubocop:disable Lint/MissingSuper
@inner = inner
end

def each
loop { yield(recv) }
end

def method_missing(name, *args, &block)
@inner.public_send(name, *args, &block)
def method_missing(...) # rubocop:disable Style/MissingRespondToMissing
@inner.public_send(...)
end
end
6 changes: 3 additions & 3 deletions examples/routeguide_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class Server < Routeguide::RouteGuide::Service
RESOURCE_PATH = './examples/routeguide/routeguide.json'

def initialize
def initialize # rubocop:disable Lint/MissingSuper
File.open(RESOURCE_PATH) do |f|
features = JSON.parse(f.read)
@features = Hash[features.map { |x| [x['location'], x['name']] }]
@features = features.to_h { |x| [x['location'], x['name']] }
end

@route_notes = Hash.new { |h, k| h[k] = [] }
Expand Down Expand Up @@ -98,7 +98,7 @@ def calculate_distance(point_a, point_b)

delta_lat = lat_a - lat_b
delta_lon = lon_a - lon_b
a = Math.sin(delta_lat / 2)**2 + Math.cos(lat_a) * Math.cos(lat_b) + Math.sin(delta_lon / 2)**2
a = (Math.sin(delta_lat / 2)**2) + (Math.cos(lat_a) * Math.cos(lat_b)) + (Math.sin(delta_lon / 2)**2)
(2 * RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))).to_i
end

Expand Down
9 changes: 3 additions & 6 deletions griffin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ Gem::Specification.new do |spec|
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib', 'pb']
spec.require_paths = %w[lib pb]

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'
spec.metadata['rubygems_mfa_required'] = 'true'
spec.required_ruby_version = Gem::Requirement.new('>= 3.0')

spec.add_dependency 'grpc_kit', '>= 0.5.0'
spec.add_dependency 'serverengine', '~> 2.0.7'
Expand Down
2 changes: 1 addition & 1 deletion lib/griffin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

module Griffin
def self.logger
@logger ||= Logger.new(STDOUT, level: :info)
@logger ||= Logger.new($stdout, level: :info)
end

def self.logger=(logger)
Expand Down
4 changes: 2 additions & 2 deletions lib/griffin/engine/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def install_signal_handlers
st.trap(ServerEngine::Signals::GRACEFUL_RESTART) { |s| w.stop(s) }
st.trap(ServerEngine::Signals::IMMEDIATE_RESTART, 'SIG_DFL')

st.trap(ServerEngine::Signals::RELOAD) {
st.trap(ServerEngine::Signals::RELOAD) do
w.logger.reopen!
w.reload
}
end
st.trap(ServerEngine::Signals::DETACH) { |s| w.stop(s) }

st.trap(ServerEngine::Signals::DUMP) { w.dump }
Expand Down
4 changes: 2 additions & 2 deletions lib/griffin/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def create(config)
def logdev_from_config(config)
case c = config[:log]
when nil # default
STDERR
$stderr
when '-'
STDOUT
$stdout
else
c
end
Expand Down
9 changes: 5 additions & 4 deletions lib/griffin/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def config_builder
# @param settings [Array<DS9::Settings,Integer>] list of HTTP2-Settings headers
# @param max_receive_message_size [Integer, nil] Specify the default maximum size of inbound message in bytes. Default to 4MB.
# @param max_send_message_size [Integer, nil] Specify the default maximum size of outbound message in bytes. Default to 4MB.
def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], shutdown_timeout: 30, settings: [], max_receive_message_size: nil, max_send_message_size: nil, **opts)
def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connection_size:, interceptors: [], shutdown_timeout: 30, settings: [],
max_receive_message_size: nil, max_send_message_size: nil, **opts)
@min_connection_size = min_connection_size
@max_connection_size = max_connection_size
@server = GrpcKit::Server.new(
Expand All @@ -55,7 +56,7 @@ def initialize(min_pool_size:, max_pool_size:, min_connection_size:, max_connect
max_pool_size: max_pool_size,
max_receive_message_size: max_receive_message_size,
max_send_message_size: max_send_message_size,
settings: settings
settings: settings,
)
@opts = opts
@status = :run
Expand Down Expand Up @@ -104,8 +105,8 @@ def handle_server
io = IO.select(@socks, [], [])

io[0].each do |sock|
if sock == @command
break if handle_command
if sock == @command && handle_command
break
end

# @thread_pool.schedule could block and accepted socket would be timeout.
Expand Down
19 changes: 9 additions & 10 deletions lib/griffin/server_config_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class ServerConfigBuilder
DEFAULT_POOL_SIZE = 20
DEFAULT_CONNECTION_SIZE = 3

GRIFFIN_CONFIGS = [
:max_pool_size,
:min_pool_size,
:max_connection_size,
:min_connection_size,
:max_receive_message_size,
:max_send_message_size,
:http2_settings,
GRIFFIN_CONFIGS = %i[
max_pool_size
min_pool_size
max_connection_size
min_connection_size
max_receive_message_size
max_send_message_size
http2_settings
].freeze

GRPC_CONFIGS = %i[services interceptors].freeze
Expand Down Expand Up @@ -50,7 +50,7 @@ def initialize
@opts = DEFAULT_SERVER_CONFIG.dup
end

(SERVERENGINE_PRIMITIVE_CONFIGS).each do |name|
SERVERENGINE_PRIMITIVE_CONFIGS.each do |name|
define_method(name) do |value|
@opts[name] = value
end
Expand Down Expand Up @@ -92,7 +92,6 @@ def max_send_message_size(value)
@opts[:max_send_message_size] = Integer(value)
end


def build
c = ServerConfig.new
@opts.each do |name, value|
Expand Down
6 changes: 4 additions & 2 deletions lib/griffin/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def shutdown
end

# For GrpcKit::ThreadPool::AutoTrimmer
def trim(force = false)
# TODO: Change interface to use a keyword argument.
def trim(force = false) # rubocop:disable Style/OptionalBooleanParameter
if @mutex.synchronize { (force || (@waiting > 0)) && (@spawned > @min_pool_size) }
GrpcKit.logger.info("Trim worker! Next worker size #{@spawned - 1}")
@tasks.push(nil)
Expand Down Expand Up @@ -86,7 +87,8 @@ def spawn_thread
begin
@block.call(task)
rescue Exception => e # rubocop:disable Lint/RescueException
Griffin.logger.error("An error occured on top level in worker #{Thread.current.name}: #{e.message} (#{e.class})\n #{Thread.current.backtrace.join("\n")} ")
backtrace = Thread.current.backtrace.join("\n")
Griffin.logger.error("An error occured on top level in worker #{Thread.current.name}: #{e.message} (#{e.class})\n #{backtrace} ")
end
end

Expand Down