autoupdater: improve concurrent implementation

This commit is contained in:
Felix Yan 2022-11-03 23:00:08 +02:00
parent e58c262ec7
commit 25d6cfe217
1 changed files with 30 additions and 35 deletions

View File

@ -1,13 +1,13 @@
#!/usr/bin/ruby
require 'concurrent'
require 'filelock'
require 'set'
require_relative 'verify'
$stdout.sync = true
echo_set = Set[]
tested = Set[]
queue = Queue.new
echo_set = Concurrent::Set[]
tested = Concurrent::Set[]
File.readlines("accelerated-domains.china.conf").each do |line|
line.chomp!
@ -16,11 +16,19 @@ File.readlines("accelerated-domains.china.conf").each do |line|
end
end
threads = []
ENV.fetch("JOBS", "1").to_i.times.each do
threads << Thread.new do
v = ChinaListVerify.new
while domain = queue.pop
pool = Concurrent::FixedThreadPool.new(ENV.fetch("JOBS", Concurrent.processor_count).to_i)
ARGF.each do |domain|
pool.post do
domain.chomp!.downcase!
next if domain.empty? or domain.end_with?('.arpa', '.cn', '.top')
if !echo_set.include? domain
puts "Trying to detect #{domain}"
echo_set << domain
end
begin
domain = PublicSuffix.domain(domain, ignore_private: true)
rescue PublicSuffix::DomainNotAllowed, PublicSuffix::DomainInvalid
@ -39,19 +47,6 @@ ENV.fetch("JOBS", "1").to_i.times.each do
end
end
end
end
ARGF.each do |domain|
domain.chomp!.downcase!
next if domain.empty? or domain.end_with?('.arpa', '.cn', '.top')
if !echo_set.include? domain
puts "Trying to detect #{domain}"
echo_set << domain
end
queue << domain
end
queue.close
threads.each(&:join)
pool.shutdown
pool.wait_for_termination