Sha256: 577f4801bcf466cd67d5dc6868eb716b2c199f7bdf4c1d617e30b97a7645e5bf

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  # This class validates list of proxies.
  # Each proxy is validated using <code>ProxyFetcher::ProxyValidator</code>.
  class ProxyListValidator
    # @!attribute [r] proxies
    #   @return [Array<ProxyFetcher::Proxy>] Source array of proxies
    attr_reader :proxies
    # @!attribute [r] valid_proxies
    #   @return [Array<ProxyFetcher::Proxy>] Array of valid proxies after validation
    attr_reader :valid_proxies

    # @param [Array<ProxyFetcher::Proxy>] *proxies
    #   Any number of <code>ProxyFetcher::Proxy</code> to validate
    def initialize(*proxies)
      @proxies = proxies.flatten
    end

    # Performs validation
    #
    # @return [Array<ProxyFetcher::Proxy>]
    #   list of valid proxies
    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
proxy_fetcher-0.17.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.16.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.15.1 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.15.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.14.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.13.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.12.0 lib/proxy_fetcher/utils/proxy_list_validator.rb
proxy_fetcher-0.11.0 lib/proxy_fetcher/utils/proxy_list_validator.rb