verify: add concurrency

This commit is contained in:
Felix Yan 2022-11-03 22:58:47 +02:00
parent 04a0ef1441
commit e58c262ec7
1 changed files with 11 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'colorize' require 'colorize'
require 'concurrent'
require 'ipaddr' require 'ipaddr'
require 'public_suffix' require 'public_suffix'
require 'resolv' require 'resolv'
@ -18,7 +19,7 @@ class ChinaListVerify
@whitelist = load_list whitelist_file @whitelist = load_list whitelist_file
@blacklist = load_list blacklist_file @blacklist = load_list blacklist_file
@cdnlist = load_list cdnlist_file @cdnlist = load_list cdnlist_file
@tld_ns = {} @tld_ns = Concurrent::Hash.new
begin begin
@chnroutes = load_list(chnroutes_file).map { |line| IPAddr.new line } @chnroutes = load_list(chnroutes_file).map { |line| IPAddr.new line }
@ -191,10 +192,6 @@ class ChinaListVerify
end end
end end
def check_domain_quiet(domain, **kwargs)
check_domain(domain, **kwargs)
end
def check_domain_verbose(domain, show_green: false, **kwargs) def check_domain_verbose(domain, show_green: false, **kwargs)
check_domain(domain, **kwargs) do |result, message| check_domain(domain, **kwargs) do |result, message|
if result == true if result == true
@ -207,18 +204,25 @@ class ChinaListVerify
end end
end end
def check_domain_list(domain_list, sample: 30, show_green: False) def check_domain_list(domain_list, sample: 30, show_green: false, jobs: Concurrent.processor_count)
domains = load_list domain_list domains = load_list domain_list
if sample > 0 if sample > 0
domains = domains.sample(sample) domains = domains.sample(sample)
else else
domains.shuffle! domains.shuffle!
end end
pool = Concurrent::FixedThreadPool.new(jobs)
domains.each do |domain| domains.each do |domain|
check_domain_verbose(domain, show_green: show_green) pool.post do
if check_domain_verbose(domain, show_green: show_green)
yield domain if block_given?
end end
end end
end end
pool.shutdown
pool.wait_for_termination
end
end
if __FILE__ == $0 if __FILE__ == $0
require 'optparse' require 'optparse'