verify: add concurrency
This commit is contained in:
parent
04a0ef1441
commit
e58c262ec7
18
verify.rb
18
verify.rb
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/ruby
|
||||
|
||||
require 'colorize'
|
||||
require 'concurrent'
|
||||
require 'ipaddr'
|
||||
require 'public_suffix'
|
||||
require 'resolv'
|
||||
|
@ -18,7 +19,7 @@ class ChinaListVerify
|
|||
@whitelist = load_list whitelist_file
|
||||
@blacklist = load_list blacklist_file
|
||||
@cdnlist = load_list cdnlist_file
|
||||
@tld_ns = {}
|
||||
@tld_ns = Concurrent::Hash.new
|
||||
|
||||
begin
|
||||
@chnroutes = load_list(chnroutes_file).map { |line| IPAddr.new line }
|
||||
|
@ -191,10 +192,6 @@ class ChinaListVerify
|
|||
end
|
||||
end
|
||||
|
||||
def check_domain_quiet(domain, **kwargs)
|
||||
check_domain(domain, **kwargs)
|
||||
end
|
||||
|
||||
def check_domain_verbose(domain, show_green: false, **kwargs)
|
||||
check_domain(domain, **kwargs) do |result, message|
|
||||
if result == true
|
||||
|
@ -207,17 +204,24 @@ class ChinaListVerify
|
|||
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
|
||||
if sample > 0
|
||||
domains = domains.sample(sample)
|
||||
else
|
||||
domains.shuffle!
|
||||
end
|
||||
pool = Concurrent::FixedThreadPool.new(jobs)
|
||||
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
|
||||
pool.shutdown
|
||||
pool.wait_for_termination
|
||||
end
|
||||
end
|
||||
|
||||
if __FILE__ == $0
|
||||
|
|
Loading…
Reference in New Issue