Skip to content

Commit

Permalink
Use Concurrent.usable_processor_count when it is available (#2339)
Browse files Browse the repository at this point in the history
* Use Concurrent.usable_processor_count when it is available

It's a new API introduced in concurrent-ruby 1.3.1, which works better
in the container environment.

ruby-concurrency/concurrent-ruby#1038

Since there are gems like sorbet-runtime that still use older versions
of concurrent-ruby, we can't directly bump concurrent-ruby's requirement,
but need to check if the method is available before calling it.

* Update changelog
  • Loading branch information
st0012 authored Jul 9, 2024
1 parent 8e62859 commit 142ec2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Internal

- Use Concurrent.usable_processor_count when it is available ([#2339](https://github.com/getsentry/sentry-ruby/pull/2339))

## 5.18.1

### Bug Fixes
Expand Down
10 changes: 9 additions & 1 deletion sentry-ruby/lib/sentry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def add_post_initialization_callback(&block)
def initialize
self.app_dirs_pattern = nil
self.debug = false
self.background_worker_threads = (Concurrent.processor_count / 2.0).ceil
self.background_worker_threads = (processor_count / 2.0).ceil
self.background_worker_max_queue = BackgroundWorker::DEFAULT_MAX_QUEUE
self.backtrace_cleanup_callback = nil
self.max_breadcrumbs = BreadcrumbBuffer::DEFAULT_SIZE
Expand Down Expand Up @@ -654,5 +654,13 @@ def run_post_initialization_callbacks
instance_eval(&hook)
end
end

def processor_count
if Concurrent.respond_to?(:usable_processor_count)
Concurrent.usable_processor_count
else
Concurrent.processor_count
end
end
end
end
4 changes: 2 additions & 2 deletions sentry-ruby/spec/sentry/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

describe "#background_worker_threads" do
it "sets to have of the processors count" do
allow(Concurrent).to receive(:processor_count).and_return(8)
allow_any_instance_of(Sentry::Configuration).to receive(:processor_count).and_return(8)
expect(subject.background_worker_threads).to eq(4)
end

it "sets to 1 with only 1 processor" do
allow(Concurrent).to receive(:processor_count).and_return(1)
allow_any_instance_of(Sentry::Configuration).to receive(:processor_count).and_return(1)
expect(subject.background_worker_threads).to eq(1)
end
end
Expand Down

0 comments on commit 142ec2f

Please sign in to comment.