class ProxyFetcher::ProxyListValidator

This class validates list of proxies. Each proxy is validated using ProxyFetcher::ProxyValidator.

Attributes

proxies[R]

@!attribute [r] proxies

@return [Array<ProxyFetcher::Proxy>] Source array of proxies
valid_proxies[R]

@!attribute [r] valid_proxies

@return [Array<ProxyFetcher::Proxy>] Array of valid proxies after validation

Public Class Methods

new(*proxies) click to toggle source

@param [Array<ProxyFetcher::Proxy>] *proxies

Any number of <code>ProxyFetcher::Proxy</code> to validate
# File lib/proxy_fetcher/utils/proxy_list_validator.rb, line 16
def initialize(*proxies)
  @proxies = proxies.flatten
end

Public Instance Methods

validate() click to toggle source

Performs validation

@return [Array<ProxyFetcher::Proxy>]

list of valid proxies
# File lib/proxy_fetcher/utils/proxy_list_validator.rb, line 24
def validate
  target_proxies = @proxies.dup
  target_proxies_lock = Mutex.new
  connectable_proxies = []
  connectable_proxies_lock = Mutex.new
  threads = []

  ProxyFetcher.config.pool_size.times do
    threads << Thread.new do
      loop do
        proxy = target_proxies_lock.synchronize { target_proxies.shift }
        break unless proxy

        if proxy.connectable?
          connectable_proxies_lock.synchronize { connectable_proxies << proxy }
        end
      end
    end
  end

  threads.each(&:join)

  @valid_proxies = connectable_proxies
end