Skip to content

Commit

Permalink
Validate value of CRYSTAL_WORKERS
Browse files Browse the repository at this point in the history
Only positive integer values are accepted or empty string (that switches back to the default number of workers). Any other value will make the program to crash.
  • Loading branch information
waj committed Aug 26, 2019
1 parent fbbefd1 commit f106d93
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/crystal/scheduler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ class Crystal::Scheduler
end

def self.init_workers
count = ENV["CRYSTAL_WORKERS"]?.try &.to_i || 4
@@workers = Array(Thread).new(count) do |i|
@@workers = Array(Thread).new(worker_count) do |i|
if i == 0
worker_loop = Fiber.new(name: "Worker Loop") { Thread.current.scheduler.run_loop }
Thread.current.scheduler.enqueue worker_loop
Expand All @@ -201,5 +200,24 @@ class Crystal::Scheduler
end
end
end

private def self.worker_count
env_workers = ENV["CRYSTAL_WORKERS"]?

if env_workers && !env_workers.empty?
workers = env_workers.to_i?
if !workers || workers < 1
LibC.dprintf 2, "FATAL: Invalid value for CRYSTAL_WORKERS: #{env_workers}\n"
exit 1
end

workers
else
# TODO: default worker count, currenlty hardcoded to 4 that seems to be something
# that is benefitial for many scenarios without adding too much contention.
# In the future we could use the number of cores or something associated to it.
4
end
end
{% end %}
end

0 comments on commit f106d93

Please sign in to comment.