Skip to content

Commit

Permalink
Merge pull request #200 from noir-cr/features/improve-file-analyzer
Browse files Browse the repository at this point in the history
Add fiber in FileAnalyzer
  • Loading branch information
hahwul authored Dec 16, 2023
2 parents 9d924a5 + 8b310b8 commit 0d88661
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/models/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Analyzer
@is_debug : Bool
@is_color : Bool
@is_log : Bool
@options : Hash(Symbol, String)

def initialize(options : Hash(Symbol, String))
@base_path = options[:base]
Expand All @@ -22,6 +23,7 @@ class Analyzer
@is_debug = str_to_bool(options[:debug])
@is_color = str_to_bool(options[:color])
@is_log = str_to_bool(options[:nolog])
@options = options

@logger = NoirLogger.new @is_debug, @is_color, @is_log
end
Expand Down Expand Up @@ -53,22 +55,35 @@ class FileAnalyzer < Analyzer
end

def analyze
begin
Dir.glob("#{base_path}/**/*") do |path|
next if File.directory?(path)
@@hooks.each do |hook|
file_results = hook.call(path, @url)
if !file_results.nil?
file_results.each do |file_result|
@result << file_result
channel = Channel(String).new
spawn do
Dir.glob("#{base_path}/**/*") do |file|
channel.send(file)
end
end

@options[:concurrency].to_i.times do
spawn do
loop do
begin
path = channel.receive
next if File.directory?(path)
@@hooks.each do |hook|
file_results = hook.call(path, @url)
if !file_results.nil?
file_results.each do |file_result|
@result << file_result
end
end
end
rescue e
logger.debug e
end
end
end
rescue e
logger.debug e
end

Fiber.yield
@result
end
end

0 comments on commit 0d88661

Please sign in to comment.