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

Terminate Riemann::Tools::HttpCheck threads between tests #284

Merged
merged 2 commits into from
Jan 22, 2024
Merged
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
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in riemann-tools.gemspec
gemspec

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
# XXX: Needed for Ruby 2.6 compatibility
#
# With Ruby 2.6 an older version of rakup is installed that cause other gems
# to be installed with a legacy version.
#
# Because rakup is only needed when using rack 3, we can just ignore this
# with Ruby 2.6.
gem 'rackup'
end
28 changes: 26 additions & 2 deletions lib/riemann/tools/http_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def initialize
@resolve_queue = Queue.new
@work_queue = Queue.new

@resolvers = []
@workers = []

opts[:resolvers].times do
Thread.new do
@resolvers << Thread.new do
loop do
uri = @resolve_queue.pop
Thread.exit unless uri

host = uri.host

addresses = Resolv::DNS.new.getaddresses(host)
Expand All @@ -59,9 +64,11 @@ def initialize
end

opts[:workers].times do
Thread.new do
@workers << Thread.new do
loop do
uri, addresses = @work_queue.pop
Thread.exit unless uri

test_uri_addresses(uri, addresses)
end
end
Expand All @@ -70,6 +77,23 @@ def initialize
super
end

# Under normal operation, we have a single instance of this class for the
# lifetime of the process. But when testing, we create a new instance
# for each test, each with its resolvers and worker threads. The test
# process may end-up with a lot of running threads, hitting the OS limit
# of max threads by process and being unable to create more thread:
#
# ThreadError: can't create Thread: Resource temporarily unavailable
#
# To avoid this situation, we provide this method.
def shutdown
@resolve_queue.close
@resolvers.map(&:join)

@work_queue.close
@workers.map(&:join)
end

def tick
report(
service: 'riemann http-check resolvers utilization',
Expand Down
12 changes: 11 additions & 1 deletion spec/riemann/tools/http_check_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# frozen_string_literal: true

require 'openssl'
require 'rack/handler/webrick'
begin
require 'rackup/handler/webrick'
rescue LoadError
# XXX: Needed for Ruby 2.6 compatibility
# Moved to the rackup gem in recent versions
require 'rack/handler/webrick'
end
require 'sinatra/base'
require 'webrick'
require 'webrick/https'
Expand Down Expand Up @@ -110,6 +116,10 @@ def protected!
@server&.shutdown
end

after(:each) do
subject.shutdown
end

let(:test_webserver_port) { @server.config[:Port] }

let(:reported_uri) { uri }
Expand Down